IOS Note

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

原文地址:https://www.cnblogs.com/davidgu/p/3467794.html