iphone之控件、控件事件与OC代码关联

来源:互联网 发布:中国汽车出口现状知乎 编辑:程序博客网 时间:2024/05/21 23:35

          ios中对应的控件(文本控件、按钮控件等)、控件事件(点击、拖动),两者的类型都非常多。 笔者以文本控控件、按钮事件来介绍它们与OC代码的关联。

          我以下图为例,当鼠标点击按钮式,文本框显示“Hello Word.”

                     

 

  • 按钮点击回调函数注册
    通过xib试图,以按钮为始,以File's Owner为终在提示的小窗口选取对应的回调函数
  • 文本框与OC中UITextField关联
    首先要定义UITextField为IBoutle类型。
    然后以File's Owner为始,以按钮为终,在提示的小窗口选取对应的控件

主要代码:

Hello_WorldViewController.h

////  Hello_WorldViewController.h//  Hello World////  Copyright __MyCompanyName__ 2012. All rights reserved.//#import <UIKit/UIKit.h>@interface Hello_WorldViewController : UIViewController {UITextField *txtField;}@property (nonatomic, retain) IBOutlet UITextField *txtField;-(IBAction)onClickButton:(id)sender;@end


Hello_WorldViewController.m

////  Hello_WorldViewController.m//  Hello World////  Copyright __MyCompanyName__ 2012. All rights reserved.//#import "Hello_WorldViewController.h"@implementation Hello_WorldViewController@synthesize txtField;-(IBAction)onClickButton:(id)sender {//txtField.text = @"Hello World.";NSString *str = [[NSString alloc] initWithFormat:@"Hello. %@", txtField.text];UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello!" message:str delegate:self cancelButtonTitle:@"Done" otherButtonTitles:nil];[alert show];[alert release];[str release];}/*// The designated initializer. Override to perform setup that is required before the view is loaded.- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {        // Custom initialization    }    return self;}*//*// Implement loadView to create a view hierarchy programmatically, without using a nib.- (void)loadView {}*//*// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.- (void)viewDidLoad {    [super viewDidLoad];}*//*// Override to allow orientations other than the default portrait orientation.- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {    // Return YES for supported orientations    return (interfaceOrientation == UIInterfaceOrientationPortrait);}*/- (void)didReceiveMemoryWarning {// Releases the view if it doesn't have a superview.    [super didReceiveMemoryWarning];// Release any cached data, images, etc that aren't in use.}- (void)viewDidUnload {// Release any retained subviews of the main view.// e.g. self.myOutlet = nil;self.txtField = nil;}- (void)dealloc {[txtField dealloc];    [super dealloc];}@end