IPhone多视图开发
IPhone多视图开发
近来公司有个项目,一部分功能需要在IPhone手机上面完成。采用了多视图和WebService客户端技术。
项目完成后,把中间开发的一些东西记录下来,一方面为了以后能加以应用,另一方面和大家一起分享,一起提高。
以一个简单的例子来说明,文章后面提供了项目下载地址。
1.新建IPhone项目
打开XCode,新建IPhone项目,选择“window-based Application”模板,项目名称暂定为shouji138,效果如下图:
完成之后的界面如下图:
2.添加控制视图的ViewController类:SwitchViewController;
这里用一个ViewController来负责整个程序的视图跳转,这样控制起来就很方便了。
在XCode左边“Classes”上面点右键,“Add”->“New File...”,选择“Cocoa Touch Class”->“UIViewController subclass”,取名为SwitchViewController,如下图:
3.添加第一个视图的控制类FirstViewController
添加第一个视图控制类,命名为FirstViewController,方法同上,如下图:
4.添加第一个视图,FirstView.xib
添加视图文件,在Resources上面新建文件,选择“User Interface”->“View XIB”,输入名称“FirstView”,如下图:
5.连接好FirstView的对应关系
添加了视图控制类和XIB文件之后,需要将他们关联起来。方法如下:
双击新建的“FirstView.xib”文件,打开界面设计器,选择"FirstView.xib"的属性面板,选中“File's Owner”,如下图:
选中菜单“Tools”->“Inspector”,调出属性窗口,选中最后一个标签栏,在“Class”下,选中“FirstViewController”,如下图:
在第二个标签栏,选中“Outlets”下的view,用鼠标拖曳它到FirstView.xib属性窗口中的“View”上面,如下图:
6.在FirstView.xib上添加控件
选择菜单“Tools”->“Library”,调出控件库,拖一个Label和Button到设计窗口,效果如下图:
7.添加第二个视图:SecondViewController和SecondView.xib
如法炮制添加第二个视图,方法同上。
8.连接好SecondView的对应关系
如法炮制连接好ViewController和View。
9.在SecondView.xib添加控件
如法炮制添加好控件,如下图。
10.在控制类SwitchViewController添加代码,实现对2个视图的跳转。
在SwitchViewController.h中添加代码:
//
// SwitchViewController.h
// shouji138.com 手机主题
//
// Created by administrator on 8/27/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
@class FirstViewController;
@class SecondViewController;
@interface SwitchViewController : UIViewController {
FirstViewController* firstviewcontroller;
SecondViewController* secondviewcontroller;
}
@property (nonatomic,retain) FirstViewController* firstviewcontroller;
@property (nonatomic,retain) SecondViewController* secondviewcontroller;
-(void)initView;
-(void)showFirstView;
-(void)showSecondView;
-(void)removeAllView;
@end
说明一下:
initView 方法用来程序加载时初始化view,showFirstView方法用来显示第一个view,showSecondView用来显示第二view。
在SwitchViewController.m中添加代码:
//
// SwitchViewController.m
// shouji138.com 手机主题
//
// Created by administrator on 8/27/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "SwitchViewController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
@implementation SwitchViewController
@synthesize firstviewcontroller;
@synthesize secondviewcontroller;
-(void)initView{
NSLog(@"ttt");
if(self.firstviewcontroller == nil){
self.firstviewcontroller = [[FirstViewController alloc]initWithNibName:@"FirstView" bundle:nil];
}
[self removeAllView];
[self.view insertSubview:self.firstviewcontroller.view atIndex:0];
}
-(void)showFirstView{
if(self.firstviewcontroller == nil){
self.firstviewcontroller = [[FirstViewController alloc]initWithNibName:@"FirstView" bundle:nil];
}
[self removeAllView];
[self.view insertSubview:self.firstviewcontroller.view atIndex:0];
}
-(void)showSecondView{
if(self.secondviewcontroller == nil){
self.secondviewcontroller = [[SecondViewController alloc]initWithNibName:@"SecondView" bundle:nil];
}
[self removeAllView];
[self.view insertSubview:self.secondviewcontroller.view atIndex:0];
}
-(void)removeAllView{
for(NSInteger i=0;i<[self.view.subviews count];i++){
[[self.view.subviews objectAtIndex:i] removeFromSuperview];
}
}
/*
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (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 {
[firstviewcontroller release];
[secondviewcontroller release];
[super dealloc];
}
@end
11.修改shouji138AppDelegate代码
修改shouji138AppDelegate.h,代码如下:
//
// shouji138AppDelegate.h
// shouji138.com 手机主题
//
// Created by administrator on 8/27/09.
// Copyright __MyCompanyName__ 2009. All rights reserved.
//
#import <UIKit/UIKit.h>
@class SwitchViewController;
@interface shouji138AppDelegate : NSObject <UIApplicationDelegate> {
IBOutlet UIWindow *window;
IBOutlet SwitchViewController *viewController;
}
@property (nonatomic, retain) UIWindow *window;
@property (nonatomic, retain) SwitchViewController *viewController;
+(shouji138AppDelegate *)App;
@end
修改shouji138AppDelegate.m代码如下:
//
// shouji138AppDelegate.m
// shouji138.com 手机主题下载
//
// Created by administrator on 8/27/09.
// Copyright __MyCompanyName__ 2009. All rights reserved.
//
#import "shouji138AppDelegate.h"
#import "SwitchViewController.h"
@implementation shouji138AppDelegate
@synthesize window;
@synthesize viewController;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after application launch
[window addSubview:viewController.view];
[viewController initView];
[window makeKeyAndVisible];
}
+(shouji138AppDelegate *)App{
return (shouji138AppDelegate *)[[UIApplication sharedApplication]delegate];
}
- (void)dealloc {
[window release];
[viewController release];
[super dealloc];
}
@end
其中:applicationDidFinishLaunching 方法中调用了SwitchViewController的initView方法,把第一个视图FirstView加载到了屏幕中,因此程序运行之后,我们看到的第一个页面是FirstView。
选择菜单“Build”->“Build”,进行编译,如果没有问题,应该可以编译通过。
12.在MainWindow.xib中连接好与SwitchViewController的对应关系。
这一步是非常重要的。
双击“MainWindow.xib”,调出“Interface Builder”;
从Library控件库中,拖动一个view Controller到“MainWindow.xib”窗口;
将这个添加的view Controller的Class设置为SwitchViewController;
选择“Shouji138 APP Delegate”,在“Outlets”->“viewController”中,拖曳一个连接线到“Switch View Controller”;
到此,完成了最重要的部分了,保存之后,点击“Build and Go”,应该会出现第一个页面。
13.添加FirstViewController和SecondViewController代码
修改FirstViewController.h如下:
//
// FirstViewController.h
// shouji138.com
//
// Created by administrator on 8/27/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface FirstViewController : UIViewController {
}
-(IBAction)buttonClick:(id)sender;
@end
修改FirstViewController.m如下
//
// FirstViewController.m
// shouji138.com
//
// Created by administrator on 8/27/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "FirstViewController.h"
#import "shouji138AppDelegate.h"
#import "SwitchViewController.h"
@implementation FirstViewController
-(IBAction)buttonClick:(id)sender{
[[shouji138AppDelegate App].viewController showSecondView];
}
....中间省略.....
- (void)dealloc {
[super dealloc];
}
@end
修改SecondViewController.h如下:
//
// SecondViewController.h
// shouji138.com
//
// Created by administrator on 8/27/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController {
}
-(IBAction)buttonClick:(id)sender;
@end
修改SecondViewController.m如下:
//
// SecondViewController.m
// shouji138.com
//
// Created by administrator on 8/27/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "SecondViewController.h"
#import "shouji138AppDelegate.h"
#import "SwitchViewController.h"
@implementation SecondViewController
-(IBAction)buttonClick:(id)sender{
[[shouji138AppDelegate App].viewController showFirstView];
}
....中间省略.....
- (void)dealloc {
[super dealloc];
}
@end
编译一下。
14.连接输出口
双击“FirstView.xib”,进入“Interface Builder”,选择“Show Second”按钮,选择“Button Connections”->“Events”->“Touch Up Inside”,拖出连接线到“File's Owner”,选择输出口“buttonClick”,效果如下图:
按照同样的设置,将SecondView.xib的“Show First”按钮事件连接到SecondViewController的buttonClick方法。
15.运行调试
点击“Build and Go”,在模拟器上出现第一个页面,点击“Show Second”按钮,跳转到第二个页面,点击“Show First”按钮,跳转到第一个页面。
最后提供演示项目的下载:
http://www.shouji138.com/files/iphone090828.zip
也欢迎光临作者的小站:手机主题 http://www.shouji138.com/
欢迎与作者交流:haolinks#163.com