您的位置:首页 > 产品设计 > UI/UE

ios学习笔记之UI篇(六):多视图应用程序

2013-12-11 21:48 621 查看
首先要创建Empty Application,再创建在ios平台下cocoa touch的类,创建一个根控制器,重复前一步骤再创建两个类,再为每个内容视图分别创建xib文件(ios平台下user interface的view模版),目录结构如图,


然后修改应用程序委托,修改appdelegate.h文件

#import <UIKit/UIKit.h>
@class BIDSwitchViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) BIDSwitchViewController *switchViewController;//声明为属性,指向应用程序根控制器
@end

当应用程序启动时将根控制器的视图添加到应用程序的主窗口,在.m文件中添加如下代码:

#import "AppDelegate.h"
#import "BIDSwitchViewController.h"
@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.

self.switchViewController=[[BIDSwitchViewController alloc]initWithNibName:@"SwitchView" bundle:nil];//先创建switchviewcontroller实例
//后从xib中加载相应视图,修改视图几何属性以免视图被隐藏在状态栏后面
UIView *switchView=self.switchViewController.view;
CGRect switchViewFrame=switchView.frame;
switchViewFrame.origin.y+=[UIApplication sharedApplication].statusBarFrame.size.height;
switchView.frame=switchViewFrame;
self.window.rootViewController=self.switchViewController;

self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}


接下来修改switchviewcontroller头文件

#import <UIKit/UIKit.h>
@class BIDBlueViewController;
@class BIDYellowViewController;
@interface BIDSwitchViewController : UIViewController
@property (strong,nonatomic)BIDYellowViewController *yellowViewController;
@property (strong,nonatomic)BIDBlueViewController *blueViewController;

- (IBAction)switchViews:(id)sender;
@end
添加视图控制器:

更改files owner为switchviewcontroller实例(默认为nsobject):在身份检查器中更改class,将其从nsobject改为SwitchViewController,完成之后切换到关联检查器,可以看到switch views:可以用来连接了。

接下来,构建包含工具栏的视图,这个视图控制器是将是我们的根视图控制器,在底部拖入一个 ToolBar,更改工具栏自带的按钮title为switch views,单击按钮,注意单击两下,确保按钮被选中,(检验办法:切换到属性检查器,确保显示的是Bar Button Item)选定按钮后按住ctrl将按钮拖到files'owner上面,然后选择switchviews:如果没有出现switchviews:而是出现delegate原因是选定的不是按钮而是工具栏,将switchviewcontroller的视图输出口与nib中的视图关联起来:按住ctrl将file‘s
owner拖到view图标上,然后选择view输出口。

编写根视图控制器:用以下代码代替viewdidload方法

- (void)viewDidLoad
{
[super viewDidLoad];
self.blueViewController=[[BIDBlueViewController alloc]initWithNibName:@"BlueView" bundle:nil];
[self.view insertSubview:self.blueViewController.view atIndex:0];
}


在switchview:方法中添加类似如下代码:

- (IBAction)switchVC:(id)sender {//首先通过检查属性redVC的view的父视图是否为nil判断出当前显示的是哪个视图
//声明一个动画块,并指定动画的持续时间
//第一个是动画块标题,第二个是void *指针,指向关联到这个动画块的对象(或者任何c语言类型)
[UIView beginAnimations:@"view flip" context:nil];
//动画持续时间
[UIView setAnimationDuration:1.25];
//设置动化曲线,这决定了动画的持续时间,默认是一条线性曲线,使动画匀速运行,UIViewAnimationCurveEaseInOut指定动画以较慢的速度开始,中间加速,然后慢慢停止
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

if (self.redVC.view.superview==nil) {
if (self.redVC.view==nil) {//如果没有就创建
RedViewController *red=[[RedViewController alloc]initWithNibName:nil bundle:nil];
self.redVC=red;
[red release];
}
//指定转换类型从左侧翻入,catch选项生成一个快照,在动画执行每一步使用这个快照,而不是重新绘图,
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
//将blueVC的view从父视图中移除
[self.blueVC.view removeFromSuperview];
//插入redVC的view
[self.view insertSubview:self.redVC.view atIndex:0];
}
else{
if (self.blueVC.view==nil) {
BlueViewController *blue=[[BlueViewController alloc]initWithNibName:nil bundle:nil];
self.blueVC=blue;
[blue release];
}
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
[self.redVC.view removeFromSuperview];
[self.view insertSubview:self.blueVC.view atIndex:0];
}
//从动画开始一直到调用commitAnimations方法之间的所有动作都会被制成动画。
[UIView commitAnimations];
}


修改didReceiveMemoryWarning方法:

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
if (self.blueViewController.view.superview==nil) {
self.blueViewController=nil;
} else {
self.yellowViewController=nil;
}
}


添加的两个视图,都在每个视图头文件中添加一个按钮方法:

#import <UIKit/UIKit.h>

@interface BIDBlueViewController : UIViewController
- (IBAction)blueButtonPressed;
@end

#import <UIKit/UIKit.h>

@interface BIDYellowViewController : UIViewController
- (IBAction)yellowButtonPressed;
@end
双击blueview.xib单击file's owner图标打开身份检查器,更改class从nsobject到BIDBlueViewController在dock中点击view打开属性检查器,更改背景颜色为蓝色,在nib文件中status bar 设置为none,bottom
bar设置为toolbar,拖入一个按钮更改按钮title为press me

yellowview.xib操作同上。

分别在两个.m文件中添加如下代码:

- (IBAction)blueButtonPressed
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Blue View Button Pressed" message:@"pressed button in blue view" delegate:nil cancelButtonTitle:@"yep,i did" otherButtonTitles:nil];
[alert show];
}

- (IBAction)yellowButtonPressed
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"yellow View Button Pressed" message:@"pressed button in yellow view" delegate:nil cancelButtonTitle:@"yep,i did" otherButtonTitles:nil];
[alert show];
}
保存代码,运行,是不是小有成就感?哈哈哈,just kidding!

在ios7下的运行结果:(与ios6不同,button变成了透明背景没有了圆角button,so sorry,将就着看吧)视图切换的时候是有动画转场的。



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息