IOS learning: uibutton

Source: Internet
Author: User
1-(void) viewdidload 2 {3 [Super viewdidload]; 4 // do any additional setup after loading the view, typically from a nib. 5 6 // Method 1: Create 7 uibutton * newbtn = [uibutton buttonwithtype: uibuttontyperoundedrect]; 8 9 newbtn. frame = cgrectmake (501_f, 501_f, 1001_f, 501_f); 10 [self. view addsubview: newbtn]; 11 12 [newbtn settitle: @ "normal" forstate: uicontrolstatenormal]; 13 [newbtn settitle: @ "selected" forstate: Uicontrolstatehighlighted]; 14/* settitle method: sets title15 of the button in a certain state. The second parameter forstate is displayed only when the text or image of the button is defined, is an enumeration type 16 Enum {17 uicontrolstatenormal = 0, the general status shows 18 uicontrolstatehighlighted = 1 <0, the highlighted status is displayed. when you click it, the status is highlighted. 19 uicontrolstatedisabled = 1 <1. 20 uicontrolstateselected = 1 <2 is displayed. when is the selected status? 21 uicontrolstateapplication = 0x00ff0000, 22 uicontrolstatereserved = 0xff000000 internal reserved 23}; */24 25 [newbtn addtarget: Self action: @ selector (CLICK: Event :) forcontrolevents: uicontroleventtouchupinside]; 26/* adds an event to the button. The last parameter specifies the event type, and the second parameter specifies the method to respond to the button event. This method has two parameters, the first is the event occurrence, and the second is the event. It seems that there are only two events at most. 27 The first parameter specifies who provides the response method. When the first parameter is nil, the system will recursively ask the response chain from the current object until the method provider is found or follow the response chain? 28 */29 30 31 [self. view addsubview: newbtn]; 32 33 34 // method 2 Creation. in this method, the type (style) of the button is not specified during creation. The default value is uibuttontypecustom. The emphasis is that this attribute can only be specified during initialization, after initialization, there is no way to change the 35 //. Therefore, this method is generally not used. 36 uibutton * BTN = [[uibutton alloc] initwithframe: cgrectmake (100366f, 100366f, 200366f, 50366f)]; 37 38 BTN. tag = 100; 39 [BTN settitle: @ "Default display style" forstate: uicontrolstatenormal]; 40 41 [self. view addsubview: BTN]; 42 [BTN release]; 43} 44 45 46/* 47 event response method provided to uibutton 48 */49-(void) Click :( ID) sender event :( uitouch *) touchevent50 {51/* 52 specifies the delegate object as the current class. This indicates that some methods in the protocol should be implemented when the current class implements the uialertviewdelegate protocol, these methods can respond to some events of alertview. The problem is that the current class does not inherit the uialertviewdelegate protocol, only do not respond to alertview events. 54 */55 uialertview * alerview = [[uialertview alloc] 56 initwithtitle: @ "Attention" 57 message: @ "test" 58 delegate: self 59 cancelbuttontitle: @ "OK" 60 otherbuttontitles: @ "cancel", 61 nil]; 62 63 [alerview show]; 64 [alerview release]; 65 66} 67 68 69/* 70 the current class inherits the uialertviewdelegate protocol. One of the methods in the protocol can be provided to respond to events in alertview 71 */72-(void) alertview :( optional *) alertview detail :( nsinteger) buttonindex73 {74 uialertview * alerview = [[uialertview alloc] 75 initwithtitle: @ "attention123" 76 message: [nsstring stringwithformat: @ "index at % d", buttonindex] 77 delegate: nil78 cancelbuttontitle: @ "ok123" 79 otherbuttontitles: nil, 80 nil]; 81 82 [alerview show]; 83 [alerview release]; 84} 85 86-(void) didreceivememorywarning87 {88 [Super didreceivemorywarning]; 89 // dispose of any resources that can be recreated.90}
Related Article

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.