Objective-C: Place all uibutton child controls in the center of the parent view to dynamically create buttons.

Source: Internet
Author: User
For (Uiview * View In [Self. backgroundview subviews])
{
// [View removefromsuperview];
If ([[View superclass] description] isw.tostring: @" Uibutton " ])
{
Uibutton * BTN = (uibutton *) view;
BTN. titlelabel. Text = @" Dynamic title " ;
BTN. Center = BTN. superview. Center;
}
}

 

 

-(Ibaction) addbutton :( ID ) Sender {
Cgrect frame = cgrectmake ( 90 , 200 , 200 , 60 );
Uibutton * someaddbutton = [uibutton buttonwithtype: uibuttontyperoundedrect];
Someaddbutton. backgroundcolor = [uicolor clearcolor];
[Someaddbutton settitle: @" Add a button dynamically! " Forstate: uicontrolstatenormal];
Someaddbutton. Frame = frame;
[Someaddbutton addtarget: Self action: @ selector (somebuttonclicked) forcontrolevents: uicontroleventtouchupinside];
[Self. View addsubview: someaddbutton];

}

 

 

 

 

-( Void ) Somebuttonclicked {
Uialertview * Alert = [[uialertview alloc] initwithtitle: @" Prompt "
Message: @" You have clicked the dynamic button! "
Delegate : Self
Cancelbuttontitle: @" OK "
Otherbuttontitles: Nil];
[Alert show];

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.