需求描述
- 在 UINavigationController 中的【A頁(yè)面】 push 到【B 頁(yè)面】之前,先顯示一個(gè)【模態(tài)頁(yè)面】顯示使用須知、服務(wù)條款等內(nèi)容。
- 在【模態(tài)頁(yè)面】中,如果點(diǎn)擊“返回”按鈕,則關(guān)閉【模態(tài)頁(yè)面】,返回到【A頁(yè)面】 ;如果點(diǎn)擊“同意”按鈕,關(guān)閉【模態(tài)頁(yè)面】顯示【B 頁(yè)面】
Demo
- 【A 頁(yè)面】→【模態(tài)頁(yè)面】→【A 頁(yè)面】
- 【A 頁(yè)面】→【模態(tài)頁(yè)面】→【B 頁(yè)面】→【A 頁(yè)面】
源碼
MainViewController
// 【主頁(yè)按鈕】按下時(shí)執(zhí)行
- (IBAction)buttonDidClicked:(id)sender {
// 模態(tài)視圖控制器
ModelViewController *modelViewController = [[ModelViewController alloc] initWithNibName:NSStringFromClass([ModelViewController class]) bundle:nil];
// ?? Block回調(diào),push 到第二頁(yè)
modelViewController.conformBlock = ^{
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:NSStringFromClass([SecondViewController class]) bundle:nil];
[self.navigationController pushViewController:secondViewController animated:NO];
};
// 你可以直接 present 【模態(tài)視圖控制器】
// [self presentViewController:modelViewController animated:YES completion:nil];
// 也可以將【模態(tài)視圖控制器】封裝到【導(dǎo)航視圖控制器】中顯示。
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:modelViewController];
[self presentViewController:navController animated:YES completion:nil];
}
ModelViewController
// .h 文件中設(shè)置 Block 屬性
@property (nonatomic, copy) void(^conformBlock)(void);
// .m 文件
- (void)setupNavigationBackBarButton {
self.title = @"模態(tài)頁(yè)面";
UIBarButtonItem *cancelButtonItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:@selector(cancelButtonDidClicked:)];
self.navigationItem.leftBarButtonItem = cancelButtonItem;
}
// 取消按鈕
- (void)cancelButtonDidClicked:(id)sender {
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
// 關(guān)閉模態(tài)頁(yè)面,push 到第二頁(yè)
- (IBAction)dismissModelViewController:(id)sender {
// ?? 先執(zhí)行 Block 塊 ,第二頁(yè)視圖控制器 push 進(jìn)導(dǎo)航堆棧后,再關(guān)閉模態(tài)頁(yè)面。
// 如果先關(guān)閉模態(tài)頁(yè)面,再 push 第二頁(yè)視圖控制器會(huì)影響頁(yè)面動(dòng)畫過渡上的流暢性。
self.conformBlock();
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
SecondViewController
// 返回到根視圖控制器
- (IBAction)backToRootViewController:(id)sender {
[self.navigationController popToRootViewControllerAnimated:YES];
}
參考
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。