#import <UIKit/UIKit.h> int main(int argc, char *argv[]) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; int retVal = UIApplicationMain(argc, argv, nil, nil); [pool release]; return retVal; }
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. self.window.rootViewController = self.viewController; [self.window makeKeyAndVisible]; return YES; }
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. - (void)viewDidLoad { [super viewDidLoad]; }
- (void)viewDidLoad { [super viewDidLoad]; //创建label视图 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 200, 30)]; //设置显示内容 label.text = @"雨松MOMO的程序世界"; //设置背景颜色 label.backgroundColor = [UIColor blueColor]; //设置文字颜色 label.textColor = [UIColor whiteColor]; //设置显示位置居中 label.textAlignment = UITextAlignmentCenter; //设置字体大小 label.font = [UIFont fontWithName:[[UIFont familyNames] objectAtIndex:10] size:20]; //将label添加到显示视图中 [self.view addSubview:label]; //释放对象 [label release]; }
#import <UIKit/UIKit.h> @interface HelloWorldViewController : UIViewController { //定义了一个按钮buttonA IBOutlet UIButton *buttonA; //定义了一个按钮buttonB UIButton *buttonB; } //声明A按钮被按下的一个方法(IBAction) 相当于(void) -(IBAction)bttonPressed:(id)obj; -(void)showDialog; @end
在右侧栏中点中 鼠标 New Referencing Outlet 拉出一条线到左侧箭头指示方块上松开鼠标 这是会显示上面定义的IBOutlet UIButton ,选中这个buttonA 标示这个拖动的button控件和buttonA 绑定在了一起,然后鼠标点击右侧Touch up inside 同样拉出一条线到左侧箭头指示方块上松开鼠标 这时候会显示上面定义的方法buttonPressed 选中后 则标示 这个按钮点击后程序执行buttonPressed方法。
将按钮的事件绑定后在代码中去实现这个方法,点击后调用showDialog 方法弹出一个dialog框。
- (void)bttonPressed:(id)obj { [self showDialog]; } -(void)showDialog { //这里说一下nil 这个东西就好比java 语言中的 null UIAlertView * alertA= [[UIAlertView alloc] initWithTitle:@"我的视图" message:@"点开了弹出对话框" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil]; //objectiveC开发中调用方法是用"[]" 例如: [alertA addButtonWithTitle:@"取消"]; //如果是为方法赋值则类似java 对象.成员 例如 :textFieldA.text //添加了一个取消按钮 [alertA addButtonWithTitle:@"取消"]; //将这个UIAlerView 显示出来 [alertA show]; //objective-C 不像java 有自己的垃圾回收机制 所以我们在编写程序中一定要注意释放内存 从一开始就养成良好习惯 [alertA release]; }
同样的道理,使用可视化编辑器可以添加一个按钮响应,同样在代码中也可以添加这个响应。addTarget方法中设置按钮点击后响应的方法为showDialg方法。和上面一样弹出一个dialog框。
- (void)viewDidLoad { [super viewDidLoad]; //创建按钮 buttonB = [UIButton buttonWithType:1]; //设置按钮范围 buttonB.frame = CGRectMake(100, 250, 150, 30); //设置按钮显示内容 [buttonB setTitle:@"代码添加按钮" forState:UIControlStateNormal]; //设置按钮改变后 绑定响应方法 [buttonB addTarget:self action:@selector(showDialog) forControlEvents:UIControlEventTouchUpInside]; //将label添加到显示视图中 [self.view addSubview:buttonB]; }
- (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; //在这里将按钮对象释放掉 [buttonA release]; [buttonB release]; }
// // HelloWorldViewController.m // HelloWorld // // Created by 雨松MOMO on 11-9-30. // Copyright 2011年 __MyCompanyName__. All rights reserved. // #import "HelloWorldViewController.h" @implementation HelloWorldViewController - (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. } #pragma mark - View lifecycle // Implement viewDidLoad to do additional setup after loading the view, typically from a nib. - (void)viewDidLoad { [super viewDidLoad]; //创建label视图 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 200, 30)]; //设置显示内容 label.text = @"雨松MOMO的程序世界"; //设置背景颜色 label.backgroundColor = [UIColor blueColor]; //设置文字颜色 label.textColor = [UIColor whiteColor]; //设置显示位置居中 label.textAlignment = UITextAlignmentCenter; //设置字体大小 label.font = [UIFont fontWithName:[[UIFont familyNames] objectAtIndex:10] size:20]; //将label添加到显示视图中 [self.view addSubview:label]; //释放对象 [label release]; //创建按钮 buttonB = [UIButton buttonWithType:1]; //设置按钮范围 buttonB.frame = CGRectMake(100, 250, 150, 30); //设置按钮显示内容 [buttonB setTitle:@"代码添加按钮" forState:UIControlStateNormal]; //设置按钮改变后 绑定响应方法 [buttonB addTarget:self action:@selector(showDialog) forControlEvents:UIControlEventTouchUpInside]; //将label添加到显示视图中 [self.view addSubview:buttonB]; } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; //在这里将按钮对象释放掉 [buttonA release]; [buttonB release]; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return (interfaceOrientation == UIInterfaceOrientationPortrait); } - (void)bttonPressed:(id)obj { [self showDialog]; } -(void)showDialog { //这里说一下nil 这个东西就好比java 语言中的 null UIAlertView * alertA= [[UIAlertView alloc] initWithTitle:@"我的视图" message:@"点开了弹出对话框" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil]; //objectiveC开发中调用方法是用"[]" 例如: [alertA addButtonWithTitle:@"取消"]; //如果是为方法赋值则类似java 对象.成员 例如 :textFieldA.text //添加了一个取消按钮 [alertA addButtonWithTitle:@"取消"]; //将这个UIAlerView 显示出来 [alertA show]; //objective-C 不像java 有自己的垃圾回收机制 所以我们在编写程序中一定要注意释放内存 从一开始就养成良好习惯 [alertA release]; } @end