IOS Note - Outlet(插座) & Action(动作)

OutletActionViewController.h

#import <UIKit/UIKit.h>

@interface OutletActionViewController : UIViewController
{
    IBOutlet UITextField *txtName;
}

// expose the outlet as a property
@property (nonatomic, retain) UITextField *txtName;

// declare the action
- (IBAction) btnClicked: (id) sender;

@end

 

OutletActionViewController.m

#import "OutletActionViewController.h"

@implementation OutletActionViewController

// synthesize the property
@synthesize txtName;

// display text of the textbox in the alert
- (IBAction) btnClicked: (id) sender {
    NSString *str = 
        [[NSString alloc] initWithFormat: @"Hello, %@", txtName.text];
    
    UIAlertView *alert =
    [[UIAlertView alloc] 
    initWithTitle: @"Hello"
    message: str
    delegate: self
    cancelButtonTitle: @"OK"
    OtherButtonTitles: nil];
    
    [alert show];
    [str release];
    [alert release];
}

- (void) dealloc {
    // release the outlet
    [txtName release];
    [super dealloc];
}

 

如何添加关联

-------------------------------------------------------------------------------------------

Outlet添加:
按Control同时将File's Owner拖放到IBOutlet在视图中所在的对象(eg: Text Field)
File's Owner --> Outlet

Action添加:
按Control同时将事件需要触发的控件(eg: Round Rect Button)拖放到File's Owner
选择动作的指定函数
Action --> File's Owner

 

posted @ 2013-12-10 17:03  Master HaKu  阅读(771)  评论(0编辑  收藏  举报