iOS 코드 구현: 생 성 단추, 바 인 딩 단추 이벤트, 컨트롤 값 읽 기

7267 단어 ios
//

//  main.m

//  Hello

//

//  Created by lishujun on 14-8-28.

//  Copyright (c) 2014  lishujun. All rights reserved.

//



#import <UIKit/UIKit.h>



//        

@interface HelloWorldViewController : UIViewController



@property (nonatomic, retain) IBOutlet UITextField *textField;

@end



@implementation HelloWorldViewController



-(void) loadView

{

    //       

    UIView *contentView = [[UIView alloc]initWithFrame:[[UIScreen mainScreen] applicationFrame]];

    contentView.backgroundColor = [UIColor lightGrayColor];

    self.view = contentView;

    

    //      

    self.textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 97.0, 31.0)];

    self.textField.borderStyle = UITextBorderStyleRoundedRect;

    self.textField.keyboardType = UIKeyboardTypeNamePhonePad;

    self.textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

    [contentView addSubview:self.textField];

    

    //     

    UIButton *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];

    

    //       

    [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

}



-(void) buttonClicked:(UIButton *)button

{

    //   1:              

    NSLog(@"button clicked...");

    UITextField *textField = [self.view subviews][0];

    NSLog(@"hello, %@", textField.text);

    

    //   2: textField           

    NSLog(@"hello, %@", self.textField.text);

    

    //        

    UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Hello"

        message:self.textField.text delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

    

    [av show];

}

@end



//     

@interface HelloWorldAppDelegate : NSObject <UIApplicationDelegate>

{

    IBOutlet UIWindow *window;

}



@property (nonatomic, retain) UIWindow *window;

@property (nonatomic, retain) HelloWorldViewController *viewController;

//window        ,              ,     

//apple      viewController       

@end



@implementation HelloWorldAppDelegate



@synthesize window;

@synthesize viewController;



-(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



//     

int main(int argc, char * argv[])

{

    @autoreleasepool {

        return UIApplicationMain(argc, argv, nil, @"HelloWorldAppDelegate");

    }

}

좋은 웹페이지 즐겨찾기