博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
页面跳转的方法
阅读量:4317 次
发布时间:2019-06-06

本文共 3410 字,大约阅读时间需要 11 分钟。

模态视图
 
页面切换  6-1,6-2;
 
页面跳转方式

方式一: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 ];
 
方式五:直接更改 UIWindow 的 rootViewController
 
 

#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 ];

    

}

转载于:https://www.cnblogs.com/tianlianghong/p/5281216.html

你可能感兴趣的文章
Python性能鸡汤
查看>>
android Manifest.xml选项
查看>>
Cookie/Session机制具体解释
查看>>
ATMEGA16 IOport相关汇总
查看>>
有意思的cmd命令
查看>>
js正則表達式语法
查看>>
Git学习系列-Git基本概念
查看>>
c#多个程序集使用app.config 的解决办法
查看>>
Linux+Apache+PHP+MySQL服务器环境配置(CentOS篇)
查看>>
Linux下获取本机IP地址的代码
查看>>
(C#)调用Webservice,提示远程服务器返回错误(500)内部服务器错误
查看>>
flex布局
查看>>
python-----python的文件操作
查看>>
java Graphics2d消除锯齿,使字体平滑显示
查看>>
控件中添加的成员变量value和control的区别
查看>>
Spring Boot Docker 实战
查看>>
Div Vertical Menu ver3
查看>>
Git简明操作
查看>>
InnoDB为什么要使用auto_Increment
查看>>
HDU 1087 Super Jumping! Jumping! Jumping!
查看>>