push 🆚 present
- 前言
- present和dismiss
- 特點
- 代碼演示
- push和pop
- 特點
- 代碼演示
前言
在 iOS 開發中,push
和 present
是兩種不同的視圖控制器切換方式,它們有著顯著的區別。
present和dismiss
特點
- 在當前控制器上方新建視圖層級
- 需要手動調用dismiss
ViewController* vc = [[ViewController alloc] init];
[self presentViewController:vc2 animated:YES completion:nil];
對應的dismiss方法:(用于關閉或取消當前顯示的視圖、對話框等)
[self dismissViewControllerAnimated:YES completion:nil];
- 默認從下向上滑入
- 只保留當前presented的控制器
- 可配置為全屏覆蓋
代碼演示
新建View02類作為視圖控制器二
dimiss方法在View02文件中實現
#import "View02.h"@interface View02 ()@end@implementation View02- (void)viewDidLoad {[super viewDidLoad];self.view.backgroundColor = [UIColor cyanColor];
}-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {[self dismissViewControllerAnimated:YES completion:nil];
}
作為視圖控制器一
present方法在ViewController文件中實現
#import "ViewController.h"
#import "View02.h"
@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];[self creatUIRectButton];
}
- (void)creatUIRectButton {UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];btn1.frame = CGRectMake(10, 400, 120, 80);[btn1 setTitle:@"test button" forState:UIControlStateNormal];[btn1 setTitle:@"按下時候" forState:UIControlStateHighlighted];[btn1 addTarget:self action:@selector(press) forControlEvents:UIControlEventTouchUpInside];[btn1 setTintColor:[UIColor whiteColor]];btn1.backgroundColor = [UIColor grayColor];[self.view addSubview: btn1];
}
- (void) press {View02* vc = [[View02 alloc] init];[self presentViewController:vc animated:YES completion: nil];
}@end
運行效果:
push和pop
特點
- 用于導航控制器堆棧中的視圖控制器的界面切換。壓入導航棧,成為棧頂控制器
ViewController* vc = [[ViewController alloc] init];
[self.navigationController pushViewController:vc animated:YES];
對應的消失視圖方法為:
[self.navigationController popViewControllerAnimated:YES];
- 系統自動生成返回按鈕
- 新視圖從右向左滑入
- 保留所有pushed的控制器
- 不能全屏覆蓋
代碼演示
創建一個導航控制器:
//SceneDelegate.m
#import "SceneDelegate.h"
#import "VCRoot.h"
@interface SceneDelegate ()@end@implementation SceneDelegate- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {self.window.frame = [UIScreen mainScreen].bounds;UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[[VCRoot alloc] init]];self.window.rootViewController = nav;[self.window makeKeyAndVisible];
}
第一個視圖控制器:
#import "VCRoot.h"
#import "VCSecond.h"
@interface VCRoot ()@end@implementation VCRoot- (void)viewDidLoad {[super viewDidLoad];self.navigationController.navigationBar.translucent = YES;self.title = @"title";self.navigationItem.title = @"根視圖";self.view.backgroundColor = [UIColor blueColor];self.navigationController.navigationBar.barStyle = UIBarStyleDefault;UIBarButtonItem *next = [[UIBarButtonItem alloc] initWithTitle:@"下一級" style:UIBarButtonItemStylePlain target:self action:@selector(pressRight)];self.navigationItem.rightBarButtonItem = next;
}
-(void)pressRight {VCSecond *vc2 = [[VCSecond alloc] init];[self.navigationController pushViewController:vc2 animated:YES];
}
第二個視圖控制器:
#import "VCSecond.h"
#import "VCthird.h"
@interface VCSecond ()@end@implementation VCSecond- (void)viewDidLoad {[super viewDidLoad];self.view.backgroundColor = [UIColor yellowColor];UIBarButtonItem *btnNext = [[UIBarButtonItem alloc] initWithTitle:@"next" style:UIBarButtonItemStylePlain target:self action:@selector(press)];self.navigationItem.rightBarButtonItem = btnNext;
}
-(void)press {VCthird *vc3 = [[VCthird alloc] init];[self.navigationController pushViewController:vc3 animated:YES];
}
第三個視圖控制器:
#import "VCthird.h"@interface VCthird ()@end@implementation VCthird- (void)viewDidLoad {[super viewDidLoad];self.view.backgroundColor = [UIColor cyanColor];UIBarButtonItem *btnLeft = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(pressBack)];self.navigationItem.rightBarButtonItem = btnLeft;
}
-(void)pressBack {[self.navigationController popToRootViewControllerAnimated:YES];
}
運行效果: