iOS Development-ui Learning-Create a button with code

Source: Internet
Author: User

Create a button using code in 5 steps, namely:

1, define a button, according to the definition of the location can be defined as local variables or global variables;

2, initialize the button, the general use of a rectangle initialization;

3, set other properties of the button control, such as background picture, or background color, or button display text and other properties;

4. Add the response event and write the function content of the response event;

5. Load the created control into the window.

Five steps for concrete operation see:

The specific code is as follows:

1UIButton *btn;2BTN = [[UIButton alloc]initwithframe:cgrectmake (0, -,418,760)];3UIImage *img = [UIImage imagenamed:@"fa.jpg"];4[BTN Settitle:@"SDFDSFD"Forstate:uicontrolstatenormal];5 [btn setbackgroundimage:img forstate:uicontrolstatenormal];6 [btn addtarget:self action: @selector (haha:) forcontrolevents:uicontroleventtouchupinside];7[Self.view ADDSUBVIEW:BTN];

Response Event code:

1 -(void) haha: (ID) sender{2     NSLog (@ "btn was clicked " ); 3 }

iOS Development-ui Learning-Create a button with code

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.