iOS code implementation: Create button, bind button event, read control value

Source: Internet
Author: User
Tags uikit

////main.m//Hello////Created by Lishujun on 14-8-28.//Copyright (c) 2014 Lishujun. All rights reserved.//#import<UIKit/UIKit.h>//View Controller Objects@interfaceHelloworldviewcontroller:uiviewcontroller@property (nonatomic, retain) Iboutlet Uitextfield*TextField;@end@implementationHelloworldviewcontroller-(void) loadview{//Create a View objectUIView *contentview =[[UIView Alloc]initwithframe:[[uiscreen Mainscreen] applicationframe]; Contentview.backgroundcolor=[Uicolor Lightgraycolor]; Self.view=Contentview; //Create a text boxSelf.textfield = [[Uitextfield alloc]initwithframe:cgrectmake ( -, -,97.0,31.0)]; Self.textField.borderStyle=Uitextborderstyleroundedrect; Self.textField.keyboardType=Uikeyboardtypenamephonepad; Self.textField.contentVerticalAlignment=Uicontrolcontentverticalalignmentcenter;        [Contentview AddSubview:self.textField]; //Create buttonUIButton *button = [[UIButton alloc]initwithframe:cgrectmake (0.0,0.0,320.0,30.0)]; [Button settitle:nslocalizedstring (@"Hello", nil) forstate:uicontrolstatenormal]; Button.center=Contentview.center;        [Contentview Addsubview:button]; //Bind button Event[button addtarget:self action: @selector (buttonclicked:) forcontrolevents:uicontroleventtouchupinside];}-(void) buttonclicked: (UIButton *) button{//Method 1: Get text from the view level in the TextBoxNSLog (@"Button Clicked ..."); Uitextfield*textfield = [Self.view subviews][0]; NSLog (@"Hello,%@.", Textfield.text); //Method 2: Change the TextField from a local variable to a class propertyNSLog (@"Hello,%@.", Self.textField.text); //Popup Information with dialog boxUialertview *av = [[Uialertview alloc] Initwithtitle:@"Hello"Message:self.textField.textDelegate: Nil Cancelbuttontitle:@"OK"Otherbuttontitles:nil]; [AV show];}@end//Delegate Object@interfaceHelloworldappdelegate:nsobject <UIApplicationDelegate>{iboutlet UIWindow*window;} @property (nonatomic, retain) UIWindow*window; @property (nonatomic, retain) Helloworldviewcontroller*Viewcontroller;//window must be declared as a property, declared as a local variable, cannot draw a view, display as a black screen//The official Apple document also declares Viewcontroller as a property.@end@implementationhelloworldappdelegate@synthesizewindow;@synthesizeViewcontroller;-(void) Applicationdidfinishlaunching: (UIApplication *) application{Self.window=[[UIWindow alloc] Initwithframe:[[uiscreen Mainscreen]bounds]]; Self.viewcontroller=[[Helloworldviewcontroller alloc]init]; Self.window.rootViewController=Self.viewcontroller; [Self.window makekeyandvisible];}@end//Program EntryintMainintargcChar*argv[]) {@autoreleasepool {returnUiapplicationmain (argc, argv, Nil,@"helloworldappdelegate"); }}

iOS code implementation: Create button, bind button event, read control value

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.