方式一:Storyboard的segues方式
鼠标点击按钮button然后按住control键拖拽到SVC页面,在弹出的segue页面中选择跳转模式即可
优点:操作方便,无代码生成,在storyboard中展示逻辑清晰
缺点:页面较多时不方便查看,团队合作时可维护性差, 多人合作时不建议使用这种方式。
方式二:选项卡UITabBarController控制器
通过调用UITabBarController的addChildViewController方法添加子控制器,代码实例如下:
1
2
3
4
5
6
7
8
9
10
|
UITabBarController *tabbarVC = [[ UITabBarController alloc ] init ]; FirstViewController *FVC = [[FirstViewController ] init ]; FVC.tabBarItem.title = @ "控制器1" ;
FVC.tabBarItem.image = [ UIImage imageNamed : @ "first.png" ];
SecondViewController *SVC = [[SecondViewController ] init ]; SVC.tabBarItem.title = @ "控制器2" ;
SVC. tabBarItem.image = [UIImage imageNamed : @ "new.png" ];
// 添加子控制器(这些子控制器会自动添加到UITabBarController的 viewControllers 数组中) [tabbarVC addChildViewController :FVC]; [tabbarVC addChildViewController :SVC]; |
优点:代码量较少
缺点:tabbar的ios原生样式不太好看,(不常用,目前不建议使用),如果要使用,建议自定义tabbar
方式三:导航控制器UINavigationController
在FVC的button的监听方法中调用:
1
|
[self.navigationController pushViewController:newC animated:YES]; //跳转到下一页面 |
在SVC的方法中调用:
1
|
[self.navigationController popViewControllerAnimated:YES]; //返回上一页面 |
当有多次跳转发生并希望返回根控制器时,调用:
1
|
[ self .navigationController popToRootViewControllerAnimated: YES ]; //返回根控制器,即最开始的页面 |
方式四:利用 Modal 形式展示控制器
在FVC中调用:
1
|
[ self presentViewController:SVC animated: YES completion:nil]; |
在SVC中调用:
1
|
[ self dismissViewControllerAnimated: YES completion: nil ]; |
#import <UIKit/UIKit.h>
#import "FirstViewController.h"
#import "SecondViewController.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
FirstViewController *firsV=[[FirstViewController alloc]init];
UINavigationController *navc=[[UINavigationController alloc]initWithRootViewController:firsV];
self.window.rootViewController=navc;
return YES;
}
#import "FirstViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor redColor];
self.title=@"First";
UIBarButtonItem *nextbar=[[UIBarButtonItem alloc]initWithTitle:@"下一页" style:(UIBarButtonItemStylePlain) target:self action:@selector(Page)];
self.navigationItem.rightBarButtonItem=nextbar;
}
-(void)Page
{
NSLog(@"next");
SecondViewController *secondVC=[[SecondViewController alloc]init];
// 跳转到下一页。
[self.navigationController pushViewController:secondVC animated:YES];
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor greenColor];
// 隐藏返回按钮
self.navigationItem.hidesBackButton=YES;
self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"back" style:2 target:self action:@selector(backPage)];
}
-(void)backPage
{
// 返回上一页
[self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//返回根控制器 即最开始的页面。
//[ self .navigationController popToRootViewControllerAnimated: YES ];
}