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