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

          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

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值