标签:style color io os ar for sp on c
storyboard之间的切换有三种方法:
[self presentViewController:viewControllerToPresent animated:YES completion:nil];
[self.navigationController pushViewController:viewControllerToPresent animated:YES];
[self.navigationController setViewControllers:ViewControllers animated:YES];
前两种方法都是在当前VC的基础上进行PUSH操作,
第三种方法可以自己排列当前的VC栈,一个典型的案例是:当前登录状态下,退出登录,需要重新返回到登录界面,这个时候当前登录界面就成为根界面(所有的界面都是退不回去),用setViewControllers。
程序设计为这样:
1,新建Empty工程
2,创建故事板FirstStoryBoard,先后拖进Navigation Controller,Tab Bar Controller(自动生成2个普通的View Controller),再拖进一个View Controller,成为Tab Bar Controller的第三个Tab。
Navigation Controller和Tab Bar Controller用root view进行segue,Tab Bar Controller和View Controller之间用view controllers进行segue。
更改Tab Bar Controller的identity属性面板中的Storyboard id为TABBAR。
更改View Controller的Tab Bar Item的名称为Tab1,Tab2,Tab3,之后Tab Bar Controller的显示名称也同时变成Tab1,Tab2,Tab3。
在Tab2所对应的第二个View Controller添加一个按钮,按钮文本为:这是FirstStoryBoard,点击切换到SecondStoryBoard。
向导添加ViewControllerFirstTab2类,继承UIViewController,增加按钮的action动作,后面添加代码。
3,创建故事板SecondStoryBoard,拖进Navigation Controller,View Controller,用root view进行segue。
更改View Controller的identity属性面板中的Storyboard id为SenondViewController。
在View Controller添加第一个按钮,按钮文本为:这是SecondStoryBoard,点击切换到SecondStoryBoard。
在View Controller添加第二个按钮,按钮文本为:这是SecondStoryBoard,点击切换到FirstStoryBoard。
向导添加ViewControllerSecond类,增加按钮的action动作,后面添加代码。
4,创建故事板ThirdStoryBoard,拖进Navigation Controller,View Controller,用root view进行segue。
更改View Controller的Show the identity inspector属性面板中的Storyboard id为ThreeViewController。
在View Controller添加第一个按钮,按钮文本为:这是SecondStoryBoard,点击切换到SecondStoryBoard。
在View Controller添加第二个按钮,按钮文本为:这是SecondStoryBoard,点击切换到FirstStoryBoard。
向导添加ViewControllerThird类,增加按钮的action动作,后面添加代码。
下面开始添加代码:
1,
AppDelegate.m代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
UIStoryboard *StoryBoard = [UIStoryboard storyboardWithName:@"FirstStoryBoard" bundle:nil];
self.window.rootViewController=[StoryBoard instantiateInitialViewController];
return YES;
}
2,
ViewControllerFirstTab2.h代码:
@interface ZViewControllerFirstTab2 : UIViewController
- (IBAction)buttonActionSecond:(id)sender;
@end
ViewControllerFirstTab2.m代码:
- (IBAction)buttonActionSecond:(id)sender {
NSString *NSStringStoryBoard = @"SecondStoryBoard";
NSString *NSStringVC = @"Second";
UIStoryboard *Storyboard = [UIStoryboard storyboardWithName:NSStringStoryBoard bundle:nil];
if(nil == Storyboard) NSLog(@"NO %@", NSStringStoryBoard);
UIViewController *VC = [Storyboard instantiateViewControllerWithIdentifier:NSStringVC];
if(nil == VC) NSLog(@"NO %@", NSStringVC);
/*
/// 该方法直接操作VC,不需要NAV,与presentViewController对应的函数是dismissViewControllerAnimated
/// 该函数已经废弃
/// [self presentModalViewController:[Storyboard instantiateInitialViewController] animated:(TRUE)];
[self presentViewController:VC animated:YES completion:nil];
*/
if(nil == self.navigationController) NSLog(@"null navigationController");
else
{
self.navigationController.navigationBarHidden = YES;
[self.navigationController pushViewController:VC animated:YES];
}
}
3,
ViewControllerSecond.h代码:
@interface ZViewControllerSecond : UIViewController
- (IBAction)buttonActionThree:(id)sender;
- (IBAction)buttonActionOne:(id)sender;
@end
ViewControllerSecond.m代码:
- (IBAction)buttonActionThree:(id)sender {
NSString *NSStringStoryBoard = @"ThirdStoryBoard";
NSString *NSStringVC = @"Third";
UIStoryboard *Storyboard = [UIStoryboard storyboardWithName:NSStringStoryBoard bundle:nil];
if(nil == Storyboard) NSLog(@"NO %@", NSStringStoryBoard);
UIViewController *VC = [Storyboard instantiateViewControllerWithIdentifier:NSStringVC];
if(nil == VC) NSLog(@"NO %@", NSStringVC);
/*
/// 该方法直接操作VC,不需要NAV,与presentViewController对应的函数是dismissViewControllerAnimated
/// 该函数已经废弃
/// [self presentModalViewController:[Storyboard instantiateInitialViewController] animated:(TRUE)];
[self presentViewController:VC animated:YES completion:nil];
*/
if(nil == self.navigationController) NSLog(@"null navigationController");
else
{
self.navigationController.navigationBarHidden = YES;
[self.navigationController pushViewController:VC animated:YES];
}
}
- (IBAction)buttonActionOne:(id)sender {
/// 返回第一个界面时,第一个界面保留切换时的状态
/// [self dismissViewControllerAnimated:YES completion:nil];
[self.navigationController popViewControllerAnimated:YES];
}
4,
ViewControllerThird.h代码:
@interface ZViewControllerThird : UIViewController
- (IBAction)buttonActionSecond:(id)sender;
- (IBAction)buttonActionFirst:(id)sender;
@end
ViewControllerThird.m代码:
- (IBAction)buttonActionSecond:(id)sender
{
/// [self dismissViewControllerAnimated:YES completion:nil];
[self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)buttonActionFirst:(id)sender
{
NSString *NSStringStoryBoard = @"FirstStoryBoard";
NSString *NSStringVC = @"TABBAR";
UIStoryboard *Storyboard = [UIStoryboard storyboardWithName:NSStringStoryBoard bundle:nil];
UIViewController *VC = [Storyboard instantiateViewControllerWithIdentifier:NSStringVC];
if(nil == self.navigationController) NSLog(@"null navigationController");
NSMutableArray *ViewControllers = [self.navigationController.viewControllers mutableCopy];
[ViewControllers removeAllObjects];
[ViewControllers addObject:VC];
NSLog(@"%d", [ViewControllers count]);
[self.navigationController setViewControllers:ViewControllers animated:YES];
}
标签:style color io os ar for sp on c
原文地址:http://www.cnblogs.com/zsonce/p/3999562.html