界面跳轉(zhuǎn)系列一:push和modal

一、介紹(基礎(chǔ)操作忽略)

push:是針對(duì)在Navigation(導(dǎo)航欄)下使用的
modal:模態(tài)窗口(只能對(duì)這個(gè)窗口操作,點(diǎn)擊其他窗口沒(méi)有響應(yīng)的), 是可以在任何控制器使用的,主要有兩種:
1.窗口彈出(彈出后,后面的視圖不可用的,比如A彈出B,A不可用)
2.控制器的跳轉(zhuǎn)

二、使用

1、push必須在Navigation下使用、

Paste_Image.png

解釋:按ctrl進(jìn)行連線,分別連出push和modal

Paste_Image.png

問(wèn)題:點(diǎn)擊push,報(bào)錯(cuò)
原因:因?yàn)閜ush必須在Navigation下使用,我這個(gè)沒(méi)有在這個(gè)環(huán)境下

2、如果沒(méi)有在Navigation下使用modal是可以跳轉(zhuǎn)的,而且還不需要任何代碼

那么在使用modal怎么做出Navigation的效果呢?

Paste_Image.png
Paste_Image.png

Paste_Image.png

效果:這樣就可以返回到主視圖了
注意:dismissViewControllerAnimated這個(gè)方法并不算返回上一層,正確的說(shuō)是將現(xiàn)有視圖隱藏,自然就會(huì)顯示出上一層的視圖出來(lái),從而達(dá)到返回上一層的效果

3、實(shí)現(xiàn)不連線跳轉(zhuǎn)

Paste_Image.png
Paste_Image.png

然后再主視圖的.m文件寫上這個(gè)代碼,注意,剛才modal的連線要去掉

Paste_Image.png
- (IBAction)modalClicked:(UIButton *)sender {
    
    UIStoryboard *storyboard = self.storyboard;
    
    FirstViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"jump"];
    
    
    [self presentViewController:vc animated:YES completion:nil];
}

方便copy,附上代碼

4、presentViewController 跳轉(zhuǎn) 后視圖是黑色

testViewController *vc = [[testViewController alloc] init];
 [self presentViewController:vc animated:YES completion:nil];

解析:如果用以上的方法跳轉(zhuǎn),那么跳轉(zhuǎn)后的視圖是黑色的
原因:我覺(jué)得操作跳轉(zhuǎn)是成功了,確實(shí)跳轉(zhuǎn)到了指定的控制器,但是我們的控制器視圖的顯示是故事版繪制的,所以我們只是跳轉(zhuǎn)了控制器,但是并沒(méi)有找到指定的故事版把視圖繪制出來(lái)
解決

UIStoryboard *storyboard = self.storyboard;

 FirstViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"jump"];

 [self presentViewController:vc animated:YES completion:nil];

這樣才可以成功

5、代碼操作

1.Navigation下操作:

//跳轉(zhuǎn)到指定id的控制器(這里的id是指在故事板里設(shè)置控制器的id)
    [self.navigationController pushViewController:vc animated:YES];
//返回到上一層視圖
    [self.navigationController popViewControllerAnimated:YES];
// 返回到根視圖
    [self.navigationController popToRootViewControllerAnimated:YES];

這里,同樣有個(gè)問(wèn)題,操作不當(dāng),就會(huì)出現(xiàn)跳轉(zhuǎn)后黑色的情況

// 創(chuàng)建控制器對(duì)象要用這種方法創(chuàng)建
UIStoryboard *storyboard = self.storyboard;

 FirstViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"jump"];

// 如果用這種,就會(huì)出現(xiàn)跳轉(zhuǎn)后黑色
FirstViewController *vc = [[FirstViewController alloc] init];

最后編輯于
?著作權(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)容