行书书法作品图片:Ipad开发课程-Charpter4 基础窗口应用 ? 疯狂的老九

来源:百度文库 编辑:九乡新闻网 时间:2024/04/27 19:48:21

Ipad开发课程-Charpter4 基础窗口应用

在这节,你会发现另外一种创建iPhoneSDK程序的方法:Windows-based Application.和View-basedApplication不一样,Windows-basedApplication创建的时候默认不包含一个View控制器.它只会提供一个iPad应用程序的骨架,其它都需要开发者自己添加--你需要自己添加你的View窗口和他们的控制器.为此,一个Window-basedApplication是一个了解View控制器怎样工作和领悟View控制器和XIB文件链接间的关系.当了解清楚了View控制器工作原理后,我们就能创建复杂的程序了.

首先第一件事,我们试着创建写一个Window-based应用,添加一个View控制器.

创建一个Window-based应用,程序下载:windowBasedApp.rar

1.打开Xcode,用Window-basedApplication创建一个项目,取名:windowBasedApp.项目创建后在文件list列表中我们只能看到一个XIB文件(MainWindow.xib)和两个委托类文件(windowBasedAppAppDelegate.h和windowBasedAppAppDelegate.m)

2.C+R,一个空屏幕被显示在iPad模拟器,这是因为Window-based Application只提供一个框架,就一个窗口和应用委托类.

3.在Xcode,双击MainWindow.xib在Interface Builder中编辑它.注意在MainWindow.xib窗口有四个项目:

 

File’s Owner

First Responder

Window

Window Based App App Delegate

4.从Library窗口拖动一个View控制器项目到MainWinidow.xib窗口.你连接一个View控制器到一个窗口

 

 

File’s Owner First Responder Window Window Based App App Delegate

5.在Xcode,右键点击Classes文件夹,然后添加一个新的文件

在新文件窗口,点击Cocoa Touch Class项和选择UIAlertViewDelegateviewController子类,勾选"Targeted for iPad"和"With VIB for user interface"

点击Next,为这个项目取名HelloWorldViewController.m.Xcode将会这样显示:

.h和.m两个文件是为刚才你在Interface Builder添加的View控制器项目服务的ViewController类文件,这个.xib文件是为试图控制器UI界面服务的.为了让项目变得有更有条理性,我们应该将.xib文件拖动到Resources文件夹.

6.双击HelloWorldViewController.xib文件,编辑它,编辑它,我们编辑它!

7.添加一个Round Rect Button到View窗口:

8.在IB里,选择View Controller项到MainWindow.xib窗口,查看它的IdentityInspector窗口.拉动Class后面的列表,选择HelloWorldViewController.搞定后,名字会自动添加空格,为了美观不影响什么!

9.查看Hello World View controller的Attributes Inspector 窗口,拉动NIB后面的列表,选择HelloWorldViewController:

10,最麻烦的来了!写代码!

在windowBasedAppAppDelegate.h文件中添加:

 

#import

 

//---add a forward reference to the HelloWorldViewController class---

@class HelloWorldViewController;

@interface windowBasedAppAppDelegate :

NSObject {

UIWindow *window;

//---create an instance of the view controller---

HelloWorldViewController *viewController;

}

@property (nonatomic, retain) IBOutlet UIWindow *window;

//---expose the view controller as a property---

@property (nonatomic, retain) IBOutlet

HelloWorldViewController *viewController;

@end

11.在windowBasedAppAppDelegate.m文件中添加:

 

#import “windowBasedAppAppDelegate.h”

#import “HelloWorldViewController.h”

@implementation windowBasedAppAppDelegate

@synthesize window;

//---synthesize the property---

@synthesize viewController;

- (BOOL)application:(UIApplication *)application

didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// Override point for customization after application launch

//---add the new view to the current window---

[window addSubview:viewController.view];

[window makeKeyAndVisible];

return YES;

}

- (void)dealloc {

[viewController release];

[window release];

[super dealloc];

}

@end

12.在MainWindow.xib窗口中,Ctrl+鼠标拖动这个window based App AppDelegate项目到Hello World ViewController项目,选择viewController.将这个窗口和View控制器关联起来

13.C+R,test it,一个按钮显示在了主屏幕上!

工作原理:(第二天继续)

当你用Window-based Application创建了一个iPad项目,Xcode提供了你一个最小化的程序构架---一个MainWindow.xib文件,一个应用委托类.你自己去添加View控制器,和窗口.

在上面的练习中,我们首先添加了一个ViewController项目链接到MainWindow.xib窗口.接着我们添加一个UIViewController类(我们命名为HelloWorldViewController),以至于它能和View控制器进行连接.你所编辑的这个控制类包含代码能在用户和窗口之间相互联系.

当这个应用程序完成装载运行的时候,你所添加的HelloWorldViewController对象到这个窗口以至于它能通过UIWindow类中的addSubview方法实现显示:

           [window addSubview:viewController.view];

添加一个View控制器和一个View程序

1.在上面这个windowBasedAppp程序,右键点击Xcode左侧的classes文件夹,添加一个新文件.选择UIViewController子类项目,取名:MySecondViewControlle.确认With XIBfor user interface勾选了.

2.在windowbasedAppAppDelegate.m文件中,添加下面代码:

 

#import “windowBasedAppAppDelegate.h”

#import “HelloWorldViewController.h”

#import “MySecondViewController.h”

@implementation windowBasedAppAppDelegate

@synthesize window;

@synthesize viewController;

 

//---create an instance of the second view controller---

MySecondViewController *mySecondViewController;

- (BOOL)application:(UIApplication *)application

didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

//---instantiate the second view controller---

mySecondViewController = [[MySecondViewController alloc]

initWithNibName:nil

bundle:nil];

//---add the view from the second view controller---

[window addSubview:mySecondViewController.view];

//---comment this out so that it doesn’t load the viewController---

//[window addSubview:viewController.view];

[window makeKeyAndVisible];

return YES;

}

- (void)dealloc {

[mySecondViewController release];

[viewController release];

[window release];

[super dealloc];

}

 

 

3.在MySecondViewController.h文件中,再添加:

 

#import

@interface MySecondViewController : UIViewController {

//---create two outlets - label and button---

UILabel *label;

UIButton *button;

}

//---expose the outlets as properties---

@property (nonatomic, retain) UILabel *label;

@property (nonatomic, retain) UIButton *button;

@end

 

4.打开MySecondViewControlle.m文件,添加ViewDidLoad()方法和修改dealloc方法

 

@synthesize label, button;

- (void)viewDidLoad {

//---create a CGRect for the positioning---

CGRect frame = CGRectMake(230, 10, 300, 50);

//---create a Label view---

label = [[UILabel alloc] initWithFrame:frame];

label.textAlignment = UITextAlignmentCenter;

 

label.font = [UIFont fontWithName:@“Verdana” size:20];

label.text = @“This is a label”;

//---create a Button view---

frame = CGRectMake(230, 100, 300, 50);

button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

button.frame = frame;

[button setTitle:@“OK” forState:UIControlStateNormal];

button.backgroundColor = [UIColor clearColor];

//---add the views to the View window---

[self.view addSubview:label];

[self.view addSubview:button];

[super viewDidLoad];

}

- (void)dealloc {

[label release];

[button release];

[super dealloc];

}

5.C+R,一个标签和按钮窗口被显示在程序的主窗口!

程序执行原理:

在前面一个步骤,你添加了一个View控制器项目.一个UIViewController类的实体,一个XIB文件到你的项目.在这个步骤里面,我们创建了一个UIVIewController类实体和一个窗口程序到主窗口.

创建UIViewController类的实体:

 

//---instantiate the second view controller---

mySecondViewController = [[MySecondViewController alloc]

initWithNibName:nil

bundle:nil];

这里你不需要XIB文件,因为多个视图可公用一个程序文件.因此initWithNibName:常量被设为nil.

加载一个试图窗口将UIViewControoler类实例化,你可以用UIWindow实例中的addSubview方法

 

//---add the view from the second view controller---

[window addSubview:mySecondViewController.view];

在运行的时候程序创建一个视图,你需要重置UIViewController类的viewDidLoad()方法.这里,你创建了一个标签实体和一个按钮实体.定义他们的位置和文字标签,最后添加他们到主视图窗口:

 

- (void)viewDidLoad {

//---create a CGRect for the positioning---

CGRect frame = CGRectMake(10, 10, 300, 50);

 

//---create a Label view---

label = [[UILabel alloc] initWithFrame:frame];

label.textAlignment = UITextAlignmentCenter;

label.font = [UIFont fontWithName:@“Verdana” size:20];

label.text = @“This is a label”;

//---create a Button view---

frame = CGRectMake(10, 250, 300, 50);

button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

button.frame = frame;

[button setTitle:@“OK” forState:UIControlStateNormal];

button.backgroundColor = [UIColor clearColor];

//---add the views to the current View---

[self.view addSubview:label];

[self.view addSubview:button];

[super viewDidLoad];

}

下一节,创建和连接Actions