A button is the most common UI control that inherits the Uicontrol base class, which, by default, belongs to the active control, which interacts with the user and fires the appropriate event-handling method.
The following examples are several commonly used button setup methods:
viewcontroller.m//uibuttondemo////Created by Apple on 16/5/10.//copyright©2016 year. All rights reserved.//#import "ViewController.h" @interface Viewcontroller () @end @implementation viewcontroller-(void ) Viewdidload {[Super viewdidload]; uilabel* label = [[UILabel alloc] initwithframe:cgrectmake (100, 100, 300, 50)]; Label.text = @ "please click button"; Sets the label of the label so that the label can be used later to get the label [Label SETTAG:100]; [Self.view Addsubview:label]; /**//uibutton type Uibuttontypecustom custom style Uibuttontypesystem standard type uibuttontypedetaildisclosure blue arrow Head button, mainly to do a detailed description with uibuttontypeinfolight color exclamation mark Uibuttontypeinfodark Dark exclamation mark Uibuttontypecontactadd Cross plus button UIButton Typeroundedrect = Uibuttontypesystem,//Deprecated, use Uibuttontypesystem instead */uibutton* BTN = [UIButton Buttonwithtype:uibuttontypecontactadd]; Btn.frame = CGRectMake (20,200,100,50); [Self.view ADDSUBVIEW:BTN]; Type of Uibuttontypesystem standard uibutton* BTN2 = [UIButton Buttonwithtype:uibuttontypesystem]; Btn2.frame = CGRectMake (100,200,100,50); /** uicontrolstatenormal normal state appearance uicontrolstatehighlighted highlighting state shows uicontrolstatedisabled disabled state will appear Uicontro lstateselected checked Status Uicontrolstateapplication when the application flag uicontrolstatereserved temporarily without the text on the//Settings button and the default state [ BTN2 settitle:@ "button" forstate:uicontrolstatenormal]; Point state [Btn2 settitle:@ "Button2" forstate:uicontrolstatehighlighted]; [Self.view ADDSUBVIEW:BTN2]; Type of Uibuttontypesystem standard uibutton* btn3 = [UIButton Buttonwithtype:uibuttontypesystem]; Btn3.frame = CGRectMake (200,200,100,50); Create a UIImage uiimage* img = [UIImage imagenamed:@ "Button.png"]; Set background Image [Btn3 setbackgroundimage:img forstate:uicontrolstatenormal]; Set the text on the button [btn3 settitle:@ "submit" forstate:uicontrolstatenormal]; Sets the color of the text on the button [Btn3 settitlecolor:[uicolor blackcolor] forstate:uicontrolstatenormal]; [Self.view Addsubview:btn3]; Register button Event [Btn3 addtarget:self Action: @selector (OnClick:) forcontrolevents:uicontroleventtouchupinside]; }-(void) OnClick: (ID) sender{uilabel* label = (uilabel*) [Self.view viewwithtag:100]; Label.text = @ "button event is activated";} @end
As follows:
After pressing the "Submit" button:
Basic view of iOS development--uibutton