iOS——Present和Push

iOS——Present和Push

Present和Push的区别

  1. present与dismiss对应,push和pop对应
  2. pushViewController 导航控制器入栈的方式切换页面,presentViewController 模态切换的方式切换页面
  3. present只能逐级返回,push所有视图由视图栈控制,可以返回上一级,也可以返回到别的层级。
    present一般用于不同业务界面的切换,push一般用于同一业务不同界面之间的切换

用法

用 UINavigationController 的时候用 pushViewController:animated
返回之前的视图 [[self navigationController] popViewControllerAnimated:YES];
push 以后会在 navigation的 left bar自动添加back按钮,它的响应方法就是返回,所以一般不需要写返回方法,点back按钮即可

其他时候用presentModalViewController:animated
[self presentModalViewController:controller animated:YES]; // YES有动画效果
返回之前的视图 [self dismissModalViewControllerAnimated:YES];

页面切换的demo

首先我们在第一个页面中定义一个button用于跳转下一个页面

@interface ViewController : UIViewController

@property(nonatomic , strong)UIButton *buttonFirst;

@end


#import "ViewController.h"
#import "SecondViewController.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor orangeColor];
    _buttonFirst = [UIButton buttonWithType:UIButtonTypeSystem];
    [_buttonFirst setTitle:@"下一页" forState:UIButtonRoleNormal];
    _buttonFirst.frame = CGRectMake(195, 422, 80, 80);
    [_buttonFirst addTarget:self action:@selector(press) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_buttonFirst];
    
}

- (void)press {
    SecondViewController *second = [[SecondViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:second];
    nav.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:nav animated:YES completion:nil];
    
}
@end

我们在跳转下一个页面的时候建立好UINavigationController,方便第二个页面到第三个页面时候的push。

#import "SecondViewController.h"
#import "ThirdViewController.h"
@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor greenColor];
    
    _buttonSecond = [UIButton buttonWithType:UIButtonTypeSystem];
    [_buttonSecond setTitle:@"下一页" forState:UIButtonRoleNormal];
    _buttonSecond.frame = CGRectMake(195, 422, 80, 80);
    [_buttonSecond addTarget:self action:@selector(push) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_buttonSecond];
    
    _backButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_backButton setTitle:@"返回" forState:UIButtonRoleNormal];
    _backButton.frame = CGRectMake(195, 340, 80, 80);
    [_backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_backButton];
}

- (void)push {
    ThirdViewController *third = [[ThirdViewController alloc] init];
    [self.navigationController pushViewController:third animated:YES];
}

- (void)back {
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end

iOS——Present和Push_第1张图片

iOS——Present和Push_第2张图片

iOS——Present和Push_第3张图片

你可能感兴趣的:(ios,javascript)