Use of the delegate protocol, button processing, Return key processing, and program flow

Source: Internet
Author: User

Main. h

# Import <uikit/uikit. h> # import "appdelegate. H "int main (INT argc, char * argv []) {// app startup process // 1. start an iOS program. Start the main function @ autoreleasepool {// the startup function of the application: 1. Start a time loop to ensure that the program is being executed. 2. create an application object. specify an agent to respond to various states of the program. // parameter 3: Create the class to be used by an application object. uiapplication is the default. // parameter 4: specify a class as the agent of the application. Return uiapplicationmain (argc, argv, nil, nsstringfromclass ([appdelegate class]) is used for various states of the application.}

Appdelegate. h

# Import <uikit/uikit. h> // 1. Sign the Protocol @ interface appdelegate: uiresponder <uiapplicationdelegate, delimiter, uitextfielddelegate> @ property (strong, nonatomic) uiwindow * window; @ end

Appdelegate. m

# Import "appdelegate. H "@ implementation appdelegate-(bool) Application :( uiapplication *) Application didfinishlaunchingwitexceptions :( nsdictionary *) launchoptions {nslog (@" when the program is loaded, call this method "); self. window = [[uiwindow alloc] initwithframe: [[uiscreen mainscreen] bounds]; // override point for customization after application launch. self. window. backgroundcolor = [uicolor whitecolor]; [self. window makekeya Ndvisible]; uitextfield * textfield = [[uitextfield alloc] initwithframe: cgrectmake (80, 60,150, 30)]; textfield. borderstyle = uitextborderstyleroundedrect; textfield. placeholder = @ "Enter the content you want"; textfield. layer. borderwidth = 2; textfield. layer. cornerradius = 5; textfield. clearbuttonmode = uitextfieldviewmodealways; // 2. sets the agent textfield. delegate = self; [self. window addsubview: textfield]; [textfield R Elease]; uitextfield * textfield1 = [[uitextfield alloc] initwithframe: cgrectmake (80, 25,150, 30)]; textfield1.borderstyle = blank; Blank = @ "Enter Password"; textfield1.layer. borderwidth = 1; textfield1.layer. cornerradius = 5; textfield1.clearbuttonmode = uitextfieldviewmodealways; textfield1.securetextentry = yes; // the agreement is signed here. To display the message box. The box is textfield1.delegate = self; [self. window addsubview: textfield1]; [textfield1 release]; uibutton * button = [uibutton buttonwithtype: uibuttontypesystem]; [Button settitle: @ "click" forstate: uicontrolstatenormal]; button. layer. cornerradius = 5; [Button setshowstouchwhenhighlighted: Yes]; button. frame = cgrectmake (56,100,100, 30); button. alpha = 0.3; button. backgroundcolor = [uicolor Yellowcolor]; [Button addtarget: Self action: @ selector (buttonclicked :) forcontrolevents: uicontroleventtouchupinside]; [self. window addsubview: button]; uibutton * button1 = [uibutton buttonwithtype: uibuttontypesystem]; [button1 settitle: @ "for" forstate: uicontrolstatenormal]; button1.layer. cornerradius = 5; [button1 setshowstouchwhenhighlighted: Yes]; button1.frame = cgrectmake (180,100,100, 30); Ton1.alpha = 0.3; button1.backgroundcolor = [uicolor magentacolor]; [button1 addtarget: Self action: @ selector (buttonclicked :) forcontrolevents: uicontroleventtouchupinside]; [self. window addsubview: button1]; [_ window release]; return yes;} // whether to edit-(bool) textfieldshouldbeginediting :( uitextfield *) textfield {// can you start to edit the status uialertview * alertview = [[uialertview alloc] initwithtitle: @ "paid notification" message: @ "if you follow Continue, you will be charged $0.99 "delegate: Self cancelbuttontitle: @" no payment "otherbuttontitles: @" ", nil]; [alertview show]; [alertview release]; return yes;} // process the return on the keyboard. the operation here is to recycle the keyboard ......... -(Bool) textfieldshouldreturn :( uitextfield *) textfield {// when the return key is clicked, [textfield resignfirstresponder]; return YES ;}// the keyboard input is controlled here, can I enter it? Here, the control keyboard cannot enter a-(bool) textfield :( uitextfield *) textfield shouldchangecharactersinrange :( nsange) range replacementstring :( nsstring *) string {// determine the character input by the current user if ([String isequaltostring: @ "A"]) {return no;} nslog (@ "% @", string ); return yes; // if no input is returned, it is not displayed.}-(void) Buttonclicked :( uibutton *) button {// 2. Specify the view proxy. Generally, the uialertview * alertview = [[uialertview alloc] initwithtitle: @ "title" message: @ "message" delegate: Self cancelbuttontitle: @ "cancel" otherbuttontitles: @ "OK", nil]; [alertview show]; [alertview release];} // 3. implementation Protocol method-(void) alertview :( uialertview *) alertview clickedbuttonatindex :( nsinteger) buttonindex; {// when you click a button in alertview, nslog (@ "Click % d ", buttonindex); If (0 = buttonindex) {Nslog (@ "cancel");} else if (1 = buttonindex) {nslog (@ "OK") ;}}-(void) dealloc {[_ window release]; [Super dealloc];}-(void) applicationwillresignactive :( uiapplication *) application {// store as much user data as possible and the current application status nslog (@ "called when the application is deactivated "); // sent when the application is about to move from active to inactive state. this can occur for certain types of temporary interruptions (such as an incoming phone call Or SMS message) or when the user quits the application and it begins the transition to the background state. // use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. games shocould use this method to pause the game .} -(void) applicationdidenterbackground :( uiapplication *) Application {nslog (@ "called after the program enters the background"); // use this method to release shared resources, save User Data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // if your application supports background execution, this method is called instead of applicationwillterminate: when the user quits .} -(void) applicationwillenterforeground :( uiapplication *) Application {// 1. restore the status before the application enters the background. The Undo operation is not completed... Nslog (@ "the application is about to enter the foreground and will be displayed"); // called as part of the transition from the background to the inactive state; here you can undo records of the changes made on entering the background .} -(void) applicationdidbecomeactive :( uiapplication *) Application {// restart various tasks/games listened ,, you can refresh the nslog UI in this method (@ "when the application is activated"); // restart any tasks that were paused (or not yet started) while the application was inactive. if the application was previusly in the background, optionally refresh the user interface .} -(void) applicationwillterminate :( uiapplication *) Application {// called when the application is about to terminate. save data if appropriate. see also applicationdidenterbackground :.} @ end



This article from the "Liu _ blog" blog, please be sure to keep this source http://liuyafang.blog.51cto.com/8837978/1547661

Use of the delegate protocol, button processing, Return key processing, and program flow

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.