/*典型C main()函数,以便接受命令行的可变参数
*/
int main(int argc, char *argv[]) {
//内存管理类,建立一个自动释放池(autorelease pool)
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
//启动主要事件循环并开始运行应用程序,第三个参数指定了应用程序的主类,第四个是应用程序的委托,也就是代理。
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
}
#import <UIKit/UIKit.h>
@interface Hello_userViewController : UIViewController {
//声明了一个指向UILabel的指针,指针变量名是hellolabel,它带有附加的IBOutlet申明,说明这个指针变量可以连接到
//Interface Builder中创建的某个对象,
IBOutlet UILabel *helloLabel;
//和上面一样
IBOutlet UITextField *nameField;
}
/*
声明了一个方法来处理事件,并把它声明为一个动作
(注意,如果我们不在代码中直接操作它,比如改变它的大小和文本之类,那么就不需要为按钮声明一个插座变量)。
在这里IBAction相当于void表示不返回任何值,但通过IBAction表示了,通过Interface Builder把这方法连接到GUI事件上。
sender 寄件人,id类型是一个指向任何对象的指针;
*/
-(IBAction) sayHello:(id) sender;
@end
//
// Hello_userViewController.m
// Hello user
//
// Created by sinnMac05 on 3/25/10.
// Copyright sinn 2010. All rights reserved.
//
#import "Hello_userViewController.h"
@implementation Hello_userViewController
/*
// 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);
}
*/
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)viewDidLoad {
[super viewDidLoad];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
- (void)loadView {
[super loadView];
}
- (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;
}
- (void)dealloc {
[super dealloc];
[nameField release];
}
-(void) sayHello:(id)sender{
//nameField通过属性得到text值,属性可以用.
NSString *userName=nameField.text;
//创建一个新的NSString从而创建一个新的“Hello”消息,%@是一个格式指示符(format specifier)
NSString *helloMessage=[[NSString alloc] initWithFormat:@"Hello %@",userName];
[nameField resignFirstResponder];
helloLabel.text=helloMessage;
/*我们在第3行为一个字符串分配了内存。当我们不需要这个字符串时,我们有义务释放它,以便释放分配的内存。
在第6行,我们通过发送release消息给这个字符串来释放它。从技术上说,这个动作并没有释放内存;
它表示的是我们已经不再对这个对象感兴趣了,因此如果另外没有谁声称拥有这个字符串,它就会被释放
(其实它并没有被释放,因为在我们把它设置成标签的文本的时候,UILabel还保留着它)
*/
[helloMessage release];
nameField.text=NULL;
}
-(void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
-(BOOL) textFieldShouldReturn:(UITextField*) textField{
[textField resignFirstResponder];
return YES;
}
@end