您的位置:首页 > 移动开发

我的iphone开发学习笔记(一)创建view_based_application

2011-11-29 23:03 579 查看
对于刚刚开始学习的同学,跟着练习是最好的学习方法。一次编码胜过通读全书。

一。打开xcode, 选择创建view_based_application, 输入项目名称HelloNum

二。 在HelloNumControllerVier.h 

         定义三个控件

        1. UILabel *userOutput

        2. UITextField *userInput;

        3. UIButton *button

代码如下:

#import <UIKit/UIKit.h>

@interface HelloNumViewController : UIViewController {
IBOutlet UILabel *userOutput;
IBOutlet UITextField *userInput;
}

-(IBAction)setOutput:(id)sender;

@end


三。打开resource文件夹中HelloNumViewController.xib

        1. 打开其中的view

        2. 打开Tools菜单,选择Library

         3. 1 取出UILabel, 拖到view, 修改文字

         3.2  取出UILabel, 拖到view,   修改文字 Noun Goes Here

         3.3 取出 UITextField, 拖到view

        3.4. 取出Round Rect Button,  拖到view, 修改文字 Set Label

四。 关键步骤,连接

          打开HelloNumViewController.xib

          4.1 选择File's Owner, 按住control, 链接到Label(Num goes here),选择userOutput, ok

           4. 2 选择File's Owner, 按住control, 链接到TextField,选择userInput, ok

          4.3 选择RoundButton, 按住control, 选择File's Owner, 选择setOutput方法, ok

五, 回到HelloNumViewController.h

        添加ui控件的@property属性

             

//
//  HelloNumViewController.h
//  HelloNum
//
//  Created by 旭 陈 on 11-11-30.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface HelloNumViewController : UIViewController {
IBOutlet UILabel *userOutput;
IBOutlet UITextField *userInput;
}

@property(nonatomic,retain)UITextField *userInput;
@property(nonatomic,retain)UILabel *userOutput;

-(IBAction)setOutput:(id)sender;

@end


六。 HelloNumViewController.m 

        添加@synthesize userInput, userOutput;
        完成方法 setOutput:(id)sender
       代码如下
        
//
//  HelloNumViewController.m
//  HelloNum
//
//  Created by 旭 陈 on 11-11-30.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "HelloNumViewController.h"

@implementation HelloNumViewController

@synthesize userInput, userOutput;

/*
// 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;
}

- (void)dealloc {
[super dealloc];
}

-(IBAction)setOutput:(id)sender{
userOutput.text = userInput.text;
}

@end


第一个简单项目就这样完成了。
小结下:
1。 创建基于view_base_application
2.    view视图添加需要的uilabel, uitextfield, button
3.    .h 声明刚才的ui控件
4. 连接files Owner 与输出控件
     连接 button的方法和file's Owner
5. 编写@property, @synthesize
6.  完成setOutput:(id)sender
      userOutput.text = userInput.text;



多多练习,特别是ui 和 file‘s Owner的连接,与传统的android界面处理不太一样。
洗洗睡了。
        
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐