The most commonly used controls in iOS UIButton, UISlider, Uiswitch, Uisegmentedcontrol by the Xib file drag generation is very simple, in fact, the implementation of code is the same simple, of course, the implementation of code can grasp more things.
The 4 types of controls mentioned in, UIButton buttons, uislider sliders, Uiswitch switches, Uisegmentedcontrol categories
First create an empty application project named Codecontrols
The AppDelegate.h and APPDELEGATE.M files are the same as The iOS code implementation in Hello World
MainViewController.h
[CPP]View Plaincopyprint?< param name= "allowfullscreen" value= "false" >
- <span style= " font-size:10px; " > #import <uikit/uikit.h>
-
- @interface mainviewcontroller : uiviewcontroller
-
- @property (strong, nonatomic) uibutton *mybtn;
- @property (strong, nonatomic) uislider *myslider;
Li class= "alt" > @property (strong, nonatomic) uiswitch *myswitch;
- @property (strong, nonatomic) uisegmentedcontrol *mysc;
-
- @end </span>
#import <UIKit/UIKit.h> @interface Mainviewcontroller:uiviewcontroller@property (Strong, nonatomic) UIButton * MYBTN, @property (Strong, nonatomic) UISlider *myslider; @property (Strong, nonatomic) Uiswitch *myswitch; @property ( Strong, nonatomic) Uisegmentedcontrol *MYSC; @end
Mainviewcontroller.m
[CPP]View Plaincopyprint?
- <span style= "font-size:10px;" > #import "MainViewController.h"
- @interface Mainviewcontroller ()
- @end
- @implementation Mainviewcontroller
- @synthesize MYBTN,MYSLIDER,MYSWITCH,MYSC;
- -(void) viewdidload
- {
- //Load UIView
- UIView *mainview = [[UIView alloc] Initwithframe:[[uiscreen Mainscreen] applicationframe]];
- Mainview.backgroundcolor = [Uicolor Whitecolor];
- Self.view = MainView;
- [MainView release];
- //Create a button
- UIButton *btn = [UIButton buttonwithtype:uibuttontyperoundedrect];
- Btn.frame = CGRectMake (100, 30, 57, 57);
- [BTN settitle:@"button" Forstate:uicontrolstatenormal];
- [btn Settitlecolor:[uicolor Blackcolor] forstate:uicontrolstatenormal];
- [Btn setbackgroundimage:[uiimage imagenamed:@"Icon.png"] forstate:uicontrolstatenormal];
- [Btn addtarget:self Action: @selector (OnClick:) forcontrolevents:uicontroleventtouchupinside];
- MYBTN = BTN;
- [Self.view ADDSUBVIEW:MYBTN];
- //Create a Slider slider button
- UISlider *slider = [[[[UISlider Alloc] Initwithframe:cgrectmake ([[]] autorelease];
- Slider.minimumvalue = 0.0f;
- Slider.maximumvalue = 100.0f;
- Slider.value = 50.0f;
- [Slider addtarget:self action: @selector (OnChange:) forcontrolevents:uicontroleventtouchupinside];
- Myslider = slider;
- [Self.view Addsubview:myslider];
- //Create a uiswitch switch button
- Uiswitch *sbtn = [[[[Uiswitch Alloc] Initwithframe:cgrectmake ((a) [[]] autorelease];
- [Sbtn addtarget:self Action: @selector (Onswitch:) forcontrolevents:uicontroleventtouchupinside];
- Myswitch = sbtn;
- [Self.view Addsubview:myswitch];
- //Create a Uisegmentedcontrol
- Nsarray *btnlist = [Nsarray arraywithobjects:@"left"@"center", @" Right ", nil);
- Uisegmentedcontrol *SC = [[[Uisegmentedcontrol alloc] initwithitems:btnlist] autorelease];
- Sc.frame = CGRectMake (50, 250, 200, 60);
- [SC addtarget:self action: @selector (OnSelect:) forcontrolevents:uicontroleventtouchupinside];
- MYSC = SC;
- [Self.view ADDSUBVIEW:MYSC];
- [Super Viewdidload];
- }
- //Click button to trigger
- -(void) OnClick: (ID *) Sender
- {
- }
- //Pull slider slider trigger
- -(void) OnChange: (ID *) Sender
- {
- }
- //SELECT Switch Trigger
- -(void) Onswitch: (ID *) Sender
- {
- }
- //select Uisegmentedcontrol trigger
- -(void) OnSelect: (ID *) Sender
- {
- }
- </span>
#import "MainViewController.h" @interface Mainviewcontroller () @end @implementation mainviewcontroller@synthesize mybtn,myslider,myswitch,mysc;-(void) viewdidload{//Load UIView UIView *mainview = [[UIView alloc] Initwithframe:[[uis Creen Mainscreen] applicationframe]; Mainview.backgroundcolor = [Uicolor Whitecolor]; Self.view = MainView; [MainView release]; Create a button UIButton *btn = [UIButton buttonwithtype:uibuttontyperoundedrect]; Btn.frame = CGRectMake (100, 30, 57, 57); [Btn settitle:@ "button" forstate:uicontrolstatenormal]; [btn Settitlecolor:[uicolor Blackcolor] forstate:uicontrolstatenormal]; [Btn setbackgroundimage:[uiimage imagenamed:@ "Icon.png"] forstate:uicontrolstatenormal]; [Btn addtarget:self Action: @selector (OnClick:) forcontrolevents:uicontroleventtouchupinside]; MYBTN = BTN; [Self.view ADDSUBVIEW:MYBTN]; Create a slider-stroke button UISlider *slider = [[[[UISlider Alloc] Initwithframe:cgrectmake (+, +, +)] AutorElease]; Slider.minimumvalue = 0.0f; Slider.maximumvalue = 100.0f; Slider.value = 50.0f; [Slider addtarget:self action: @selector (OnChange:) forcontrolevents:uicontroleventtouchupinside]; Myslider = slider; [Self.view Addsubview:myslider]; Create a uiswitch switch button uiswitch *sbtn = [[Uiswitch alloc] Initwithframe:cgrectmake ((a) autorelease]; [Sbtn addtarget:self Action: @selector (Onswitch:) forcontrolevents:uicontroleventtouchupinside]; Myswitch = sbtn; [Self.view Addsubview:myswitch]; Create a uisegmentedcontrol Nsarray *btnlist = [Nsarray arraywithobjects:@ "left" @ "center" @ "right", nil]; Uisegmentedcontrol *SC = [[[Uisegmentedcontrol alloc] initwithitems:btnlist] autorelease]; Sc.frame = CGRectMake (50, 250, 200, 60); [SC addtarget:self action: @selector (OnSelect:) forcontrolevents:uicontroleventtouchupinside]; MYSC = SC; [Self.view ADDSUBVIEW:MYSC]; [Super Viewdidload];} Click button Trigger-(void) OnClick: (ID *) Sender{}//Pull Slider slider trigger-(void) OnChange: (ID *) sender{}//Select Switch Trigger-(void) Onswitch: (ID *) sender{}//Select Uisegmentedcontr OL trigger-(void) OnSelect: (ID *) sender{}
There is no specific implementation method for writing a click on each control.
All touch events under the Uicnotrol Class
[CPP]View Plaincopyprint?
- Uicontroleventtouchdown
- Uicontroleventtouchdownrepeat
- Uicontroleventtouchdraginside
- Uicontroleventtouchdragoutside
- Uicontroleventtouchdragenter
- Uicontroleventtouchdragexit
- UIControlEventTouchUpInside
- Uicontroleventtouchupoutside
- Uicontroleventtouchcancel
- Uicontroleventvaluechanged
- Uicontroleventeditingdidbegin
- Uicontroleventeditingchanged
- Uicontroleventeditingdidend
- Uicontroleventeditingdidendonexit
- Uicontroleventalltouchevents
- Uicontroleventalleditingevents
- Uicontroleventapplicationreserved
- Uicontroleventsystemreserved
- Uicontroleventallevents
Uicontroleventtouchdown uicontroleventtouchdownrepeat uicontroleventtouchdraginside Uicontroleventtouchdragoutside uicontroleventtouchdragenter uicontroleventtouchdragexit UIControlEventTouchUpInside uicontroleventtouchupoutside uicontroleventtouchcancel uicontroleventvaluechanged uicontroleventeditingdidbegin uicontroleventeditingchanged Uicontroleventeditingdidend uicontroleventeditingdidendonexit uicontroleventalltouchevents Uicontroleventalleditingevents uicontroleventapplicationreserved uicontroleventsystemreserved Uicontroleventallevents
All button styles under the UIButton class
[CPP]View Plaincopyprint?
- Uibuttontypecustom
- Uibuttontyperoundedrect
- Uibuttontypedetaildisclosure
- Uibuttontypeinfolight
- Uibuttontypeinfodark
- Uibuttontypecontactadd
Uibuttontypecustomuibuttontyperoundedrectuibuttontypedetaildisclosureuibuttontypeinfolightuibuttontypeinfodarkuibuttontyp Econtactadd
Demo download
http://pan.baidu.com/share/link?shareid=73529&uk=101519637