swift 模態(tài)

平時(shí)大家跳轉(zhuǎn)頁(yè)面,我們模態(tài)跳轉(zhuǎn)到某個(gè)頁(yè)面使用

[selfpresentViewController:<#(nonnull UIViewController *)#> animated:<#(BOOL)#> completion:<#^(void)completion#>]

返回時(shí)使用對(duì)應(yīng)的返回方法

[selfdismissViewControllerAnimated:YEScompletion:nil];

push跳轉(zhuǎn)頁(yè)面使用

[self.navigationController pushViewController:<#(nonnull UIViewController *)#> animated:<#(BOOL)#>]

對(duì)應(yīng)的返回方法為

[self.navigationControllerpopViewControllerAnimated:YES];

這里提出一個(gè)場(chǎng)景:從頁(yè)面A可以模態(tài)跳轉(zhuǎn)到公共頁(yè)面M,從頁(yè)面B也可以push跳轉(zhuǎn)到頁(yè)面M,那么頁(yè)面M相應(yīng)的返回按鈕應(yīng)該怎么實(shí)現(xiàn),這里我給出一個(gè)解決方案:

給公共頁(yè)面M聲明一個(gè)屬性:

@property(nonatomic,assign)BOOLmark;

//a.從頁(yè)面A跳轉(zhuǎn)到公共頁(yè)面M

M* mainDetailVC = [[Malloc]init];

mainDetailVC.mark=YES;

UINavigationController*navc = [[UINavigationControlleralloc]initWithRootViewController:mainDetailVC];

[selfpresentViewController:navcanimated:YEScompletion:nil];

//b.從頁(yè)面B跳轉(zhuǎn)到公共頁(yè)面M

M* mainDetailVC = [[Malloc]init];

[self.navigationControllerpushViewController:mainDetailVCanimated:NO];

最后一步:在控制器M中實(shí)現(xiàn)返回按鈕,需要兩種返回方法

- (void)viewWillAppear:(BOOL)animated {

//判斷是模態(tài)過(guò)來(lái)的還是push過(guò)來(lái)的,對(duì)應(yīng)創(chuàng)建相應(yīng)的返回按鈕及方法

if(self.mark==YES) {

UIBarButtonItem*btn = [[UIBarButtonItemalloc]initWithTitle:@"返回"style:(UIBarButtonItemStylePlain)target:selfaction:@selector(clickback:)];

self.navigationItem.leftBarButtonItem= btn;

}

else{UIBarButtonItem*btn = [[UIBarButtonItemalloc]initWithTitle:@"返回"style:(UIBarButtonItemStylePlain)target:selfaction:@selector(clickbacktwo:)];

self.navigationItem.leftBarButtonItem= btn;

}

}

//模態(tài)返回方法,如果是模態(tài)過(guò)來(lái)的就會(huì)使用此方法返回

- (void)clickback:(UIBarButtonItem*)but {

[selfdismissViewControllerAnimated:YEScompletion:nil];

}

//pop返回方法,如果是push過(guò)來(lái)的就會(huì)使用此方法

- (void)clickbacktwo:(UIBarButtonItem*)but {

[self.navigationControllerpopViewControllerAnimated:YES];

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容