iOS base Control--button

Source: Internet
Author: User

One, drag control method

Drag to add a button Type custom, set the state config to be default, highlighted, selected respectively, and select background as the image name.

1 #import<UIKit/UIKit.h>2 3 @interfaceViewcontroller:uiviewcontroller4 5@property (nonatomic, strong) Iboutlet UIButton *btn;6 7 @end8 9 in the. m fileTen-(Ibaction) btnselected: (ID) Sender One { AUIButton *btn = (UIButton *) sender; -btn.selected =YES; - } the  -Complete the operation

Second, the handwriting code method

1 //macro Definition file2 #ifndef Globaldefine_h3 #defineGlobaldefine_h4 5 #definescreenwidth [[UIScreen Mainscreen] Bounds].size.width6 #definescreenheight [[UIScreen Mainscreen] Bounds].size.height7 8 #endif/* Globaldefine_h */9 Ten  One //View Controller. h file A #import<UIKit/UIKit.h> -  - @interfaceViewcontroller:uiviewcontroller the  -@property (nonatomic, strong) Iboutlet UIButton *btn; -  - @end +  - #import "ViewController.h" + #import "Globaldefine.h" A  at @interfaceViewcontroller () -  - @end -  - @implementationViewcontroller -  in- (void) Viewdidload - { to [Super Viewdidload]; +_BTN = [UIButton buttonwithtype:uibuttontypecustom];//Button State -_btn.frame = CGRectMake (ScreenWidth/2- -, ScreenHeight/2- -, -, -);//Set button position and size the[_btn setbackgroundimage:[uiimage imagenamed:@"Xmh.png"] forstate:uicontrolstatenormal]; *[_btn setbackgroundimage:[uiimage imagenamed:@"Wdh.png"] forstate:uicontrolstatehighlighted]; $[_btn setbackgroundimage:[uiimage imagenamed:@"Syh.png"] forstate:uicontrolstateselected];Panax Notoginseng [Self.view addsubview:_btn]; -      the      +[_btn addtarget:self Action: @selector (btnselected:) forcontrolevents:uicontroleventtouchupinside];//@selector (btnselected:) The colon is the _btn pass through . A } the- (void) btnselected: (UIButton *) btn + { -     if(btn.selected) $     { $btn.selected =NO; -     } -     Else the     { -btn.selected =YES;Wuyi     } the } -  Wu- (void) didreceivememorywarning { - [Super didreceivememorywarning]; About     //Dispose of any resources the can be recreated. $ } -  - @end

iOS base Control--button

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.