ios学习笔记之UI篇(六):多视图应用程序
来源:互联网 发布:java全文检索 编辑:程序博客网 时间:2024/05/01 21:05
首先要创建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,将就着看吧)视图切换的时候是有动画转场的。
- ios学习笔记之UI篇(六):多视图应用程序
- IOS开发学习笔记(六)——多视图应用程序之StoryBoard实践
- IPhone学习笔记六-----多视图应用程序
- ios学习笔记之UI篇(五):切换视图
- IOS学习笔记(二)之应用程序UI基础
- IOS开发学习笔记(五)——多视图应用程序之框架分析
- ios学习笔记(六)视图控制器
- IOS学习之旅之7 多视图应用程序
- iOS UI学习笔记(六)UIViewController
- SpringSecurity学习笔记之六:保护视图
- 暑期留校之iOS学习笔记之视图控制器(UI)
- IOS单视图UI笔记
- iOS开发笔记之六——视图的生命周期
- IOS之UI 视图(笔记)
- UI -- 多视图应用程序总结
- IOS学习笔记之网格视图
- IOS学习笔记UI篇
- IOS视图之UI基础
- IOS中ARC的应用
- 2013ACM小结
- ubuntu配置静态IP地址
- Canada Tour
- test
- ios学习笔记之UI篇(六):多视图应用程序
- hive sql执行计划树解析
- 黑马程序员 面向对象(上)
- 揭开Socket编程的面纱
- keyCode键码表
- PE
- MapReduce 中的两表 join 几种方案简介
- 将用DataSet获取所有的数据存入数组中去
- eclipese安装pydev(解决不显示pydev的设置项)