【iOS】present和push

present和dismiss

使用方法

使用一般的视图控制器用present方法:

[self presentViewController:xxx animated:BOOL completion:nil];

返回时用dismiss:

[self dismissViewControllerAnimated:BOOL completion:nil];

特点

  • 使用present来呈现视图,弹出的视图时模态视图,属于临时呈现(present)一个视图,要返回的话只能逐级返回。
  • present一般用于不同业务界面的切换。

push和pop

使用方法

使用push和pop首先需要让你的viewController的navigationController属性不为空,否则push/pop是无效的。所以说要先把你的第一个controller的navigation属性初始化

如何初始化NavigationController出来?
在SceneDelegate.m中:

在这里插入图片描述
这里我的MainViewController的viewController就可以随意的push/pop了!

回到正题。
使用UINavigationController时使用push方法:

[self.navigationController pushViewController:xxx animated:BOOL];

返回时使用pop方法:

[self.navigationController popViewControllerAnimated:BOOL];

pop由于是视图栈管理,可选择返回之前的任一级:

//返回根控制器
[self.navigationController popToRootViewControllerAnimated:YES];

//返回到指定控制器
[self.navigationController popToViewController: SecondViewController animated:YES];

特点

  • 使用push,其所有视图都由视图栈来控制,可以选择返回上一级,也可以直接选择返回根视图控制器,或是其他viewController。
  • push一般用于同一业务不同界面的切换。
  • 执行完push后会在下个界面navigation的left bar自动添加back按钮,它的响应方法就是返回,所以一般不需要写返回方法,点back按钮即可。

demo

在这里插入图片描述

viewController.m

#import "ViewController.h"
#import "MainViewController.h"

#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor cyanColor];
    
    UIButton* buttonStart = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [buttonStart setTitle:@"present进入" forState:UIControlStateNormal];
    
    [buttonStart addTarget:self action:@selector(startMainView) forControlEvents:UIControlEventTouchUpInside];
    
    
    buttonStart.frame = CGRectMake(100, 300, 100, 50);
    
    [self.view addSubview:buttonStart];
    
}

- (void) startMainView {
    
    
    FirstViewController* firstController = [[FirstViewController alloc] init] ;
    SecondViewController* secondController = [[SecondViewController alloc] init] ;
    ThirdViewController* thirdController = [[ThirdViewController alloc] init] ;
    
    
    firstController.title = @"first";
    secondController.title = @"首页";
    thirdController.title = @"Third";
    
    UINavigationController* navFirst = [[UINavigationController alloc] initWithRootViewController:firstController] ;
    UINavigationController* navSecond = [[UINavigationController alloc] initWithRootViewController:secondController] ;
    UINavigationController* navThird = [[UINavigationController alloc] initWithRootViewController:thirdController] ;
    
  
    
    //使导航栏上方不留白,并设置颜色
    UINavigationBarAppearance* appear = [UINavigationBarAppearance new] ;
    
    [appear configureWithOpaqueBackground] ;
    
    appear.backgroundColor = [UIColor orangeColor] ;
    
    appear.shadowColor = [UIColor clearColor] ;
    navFirst.navigationBar.barTintColor = [UIColor linkColor];
    
    
    navFirst.navigationBar.standardAppearance = appear ;
    navFirst.navigationBar.scrollEdgeAppearance = navFirst.navigationBar.standardAppearance ;

    navSecond.navigationBar.standardAppearance = appear ;
    navSecond.navigationBar.scrollEdgeAppearance = navFirst.navigationBar.standardAppearance ;

    navThird.navigationBar.standardAppearance = appear ;
    navThird.navigationBar.scrollEdgeAppearance = navFirst.navigationBar.standardAppearance ;
    
    
    //准备用来初始化tbc的array
    NSArray* array = [NSArray arrayWithObjects:navFirst, navSecond, navThird, nil];
    
    UITabBarController* tabBarController = [[UITabBarController alloc]init];
    //tbc的viewControllers属性被赋值
    tabBarController.viewControllers = array;
    //设置颜色
    tabBarController.view.backgroundColor = [UIColor whiteColor];
    //设置分栏的透明度
    tabBarController.tabBar.translucent = NO;
    //设置风格
    tabBarController.modalPresentationStyle = UIModalPresentationFullScreen ;
    
    tabBarController.selectedIndex = 1;
    
    [self presentViewController:tabBarController animated:NO completion:nil];
    
    
}

@end

MainViewController.m

#import "MainViewController.h"

@interface MainViewController ()

@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor yellowColor];
    
    UILabel* label = [[UILabel alloc]initWithFrame:CGRectMake(30, 200, 300, 100)];
    label.text = @"There is the message that is written by zsy";
    
    [label setLineBreakMode:NSLineBreakByWordWrapping];//设置换行模式
    
    label.numberOfLines = 0;//设置label行数,表示可多行显示
    
    [self.view addSubview:label];
}

SecondViewController.m

#import "SecondViewController.h"
#import "MainViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    UIButton* buttonReturn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    buttonReturn.frame = CGRectMake(100, 300, 100, 50);
    
    [buttonReturn setTitle:@"dismiss返回" forState:UIControlStateNormal];
    
    [buttonReturn addTarget:self action:@selector(returnView) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
    UIButton* buttonNext = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    
    buttonNext.frame = CGRectMake(100, 500, 100, 50);
    
    [buttonNext setTitle:@"push进入" forState:UIControlStateNormal];
    
    [buttonNext addTarget:self action:@selector(getInNext) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    [self.view addSubview:buttonNext];
    [self.view addSubview:buttonReturn];
    
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void) returnView {
    [self dismissViewControllerAnimated:NO completion:nil];
}

- (void) getInNext {
    MainViewController* mainController = [[MainViewController alloc]init];
    
    mainController.modalPresentationStyle = UIModalPresentationFullScreen;
    
    [mainController setTitle:@"内容"];
    
    [self.navigationController pushViewController:mainController animated:YES];
}

@end

在这里插入图片描述
点击button,用present的方法进入主页面(这里我设置了 tabBarController.selectedIndex = 1;所以先进入SecondViewController):

请添加图片描述
点击push进入按钮,用push的方法进入MainViewController的View。。。
在这里插入图片描述
这里没有设置返回按钮,但系统自动生成了一个导航栏的左按钮,点击事件为pop回上一级,点击后返回:
请添加图片描述
first、third:
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值