• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

iOSpresent和push

武飞扬头像
zdsey
帮助1

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:
学新通
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgabagc
系列文章
更多 icon
同类精品
更多 icon
继续加载