47-Swift?之控制器之間的跳轉(zhuǎn)

一 、 App開發(fā)中控制器之間的跳轉(zhuǎn)有哪些方法?

》1 、導(dǎo)航跳轉(zhuǎn)
》2、模態(tài)跳轉(zhuǎn)

二 、導(dǎo)航跳轉(zhuǎn) & 模態(tài)跳轉(zhuǎn) 的介紹

1、導(dǎo)航跳轉(zhuǎn)

導(dǎo)航跳轉(zhuǎn)是通過 UINavigationController 來管理控制器之間的跳轉(zhuǎn)關(guān)系,控制器的創(chuàng)建和釋放都有UINavigationController管理器控制,開發(fā)人員不需要做什么處理。

其優(yōu)點(diǎn):

跳轉(zhuǎn)方便和控制器之間的生存不需要開發(fā)者管理。

2、模態(tài)跳轉(zhuǎn)

模態(tài)跳轉(zhuǎn) 是在當(dāng)前控制器上直接彈出另一個(gè)控制器,控制器之間的跳轉(zhuǎn)需要開發(fā)者管理,同時(shí)控制器的生存也需要開發(fā)者手動(dòng)管理。

其優(yōu)點(diǎn):

跳轉(zhuǎn)簡(jiǎn)單

三 、 導(dǎo)航跳轉(zhuǎn) & 模態(tài)跳轉(zhuǎn) 的使用舉例

1、 導(dǎo)航跳轉(zhuǎn)

1> 添加導(dǎo)航管理控制器
window = UIWindow.init(frame: UIScreen.main.bounds)
window?.backgroundColor = UIColor.white
window?.makeKeyAndVisible()
let RootVC = RootViewController.init()
let RootNavVC = UINavigationController.init(rootViewController: RootVC)
window?.rootViewController = RootNavVC

2> 添加兩個(gè)控制器 FirstNavViewController & SecondNavViewController
763C93F7-3F21-4193-A684-01CA1941145F.png

添加的控制器如上圖藍(lán)色所示。

3> 設(shè)置跳轉(zhuǎn)觸發(fā)按鈕
let NavButton = UIButton.init(type: .custom)
NavButton.frame = CGRect.init(x: 20, y: 70, width: UIScreen.main.bounds.width-40, height: 40)
NavButton.setTitle("Nav Jump 導(dǎo)航跳轉(zhuǎn)", for: .normal)
NavButton.setTitleColor(UIColor.magenta, for: .normal)
NavButton.addTarget(self, action: #selector(navPush), for: .touchUpInside)
self.view.addSubview(NavButton)
4> 導(dǎo)航跳轉(zhuǎn)的方法
func navPush() -> Void {
    let pushVc = FirstNavViewController.init()
    self.navigationController?.pushViewController(pushVc, animated: false)
}

導(dǎo)航跳轉(zhuǎn)完成,進(jìn)入新的頁(yè)面,新頁(yè)面會(huì)帶有導(dǎo)航控制器的返回按鈕(我們也可以修改導(dǎo)航的返回按鈕,我們?cè)谙乱还?jié)再說)

5841D2CB-C539-4134-860D-8E9F81CFDCA8.png

如圖上圖所示。

2、 模態(tài)跳轉(zhuǎn)

1> 添加模態(tài)跳轉(zhuǎn)的觸發(fā)按鈕
let ModalButton = UIButton.init(type: .custom)
ModalButton.frame = CGRect.init(x: 20, y: 120, width: UIScreen.main.bounds.width-40, height: 40)
ModalButton.setTitle("Modal Jump 模態(tài)跳轉(zhuǎn)", for: .normal)
ModalButton.setTitleColor(UIColor.magenta, for: .normal)
ModalButton.addTarget(self, action: #selector(modalJump), for: .touchUpInside)
self.view.addSubview(ModalButton)

2 > 模態(tài)跳轉(zhuǎn)的方法
func modalJump() -> Void {
    let modalVc = SecondNavViewController.init()
    self.present(modalVc, animated: false) { 
        print("模態(tài)跳轉(zhuǎn)完成")
    }
}
3> 模態(tài)跳轉(zhuǎn)的頁(yè)面,不帶返回按鈕,如果想返回需要我們自己管理。方法如下

#######1 、 返回觸發(fā)按鈕

let ModalButton = UIButton.init(type: .custom)
ModalButton.frame = CGRect.init(x: 20, y: 20, width: 140, height: 44)
ModalButton.setTitle("< 返回", for: .normal)
ModalButton.setTitleColor(UIColor.magenta, for: .normal)
ModalButton.contentHorizontalAlignment = .left
ModalButton.addTarget(self, action: #selector(modalJumpBack), for: .touchUpInside)
self.view.addSubview(ModalButton)

####### 2 、 返回觸發(fā)的方法

func modalJumpBack() -> Void {
     self .dismiss(animated: true) {
        print("模態(tài)跳轉(zhuǎn)返回")
     };
}

效果如圖:

C7C8524E-DA00-4C01-87D8-7E6FF1C8C02F.png

四 、故事板的導(dǎo)航跳轉(zhuǎn) & 模態(tài)跳轉(zhuǎn)

1、 首先要添加導(dǎo)航控制器,并設(shè)置為App的入口。如下圖:

9AECD1F3-F92C-4CFF-A45E-A4845187D394.png

2、 給控制器添加RootVC 。如下圖所示:

91B7B6F0-309F-4A99-B4B8-674D05F922DD.png

它與導(dǎo)航控制器的關(guān)系連接如下圖:

6BF470CC-83F2-49B1-AFC0-C5C146A4BBFC.png

操作方法是: 用鼠標(biāo)點(diǎn)擊 " Navigation Controller " 的頁(yè)面,同時(shí)注意上圖小方格標(biāo)記的位置是否是它。然后按住鍵盤上的 " option" 按鍵不放,再點(diǎn)擊鼠標(biāo)的右鍵,然后拖動(dòng)到 上圖 " First Nav View Controller" 頁(yè)面上,就會(huì)出現(xiàn)上圖黑色的方塊,然后選擇 " root view controller" 選項(xiàng)。

3 、 添加要跳轉(zhuǎn)的控制器

4C181061-2818-41E1-8745-30006E345287.png

它與上個(gè)控制器的關(guān)系連接如下圖:

E00CC788-6B2B-4963-A3F9-45EAA3F50FEF.png

操作方法:點(diǎn)擊綠色的 " 導(dǎo)航跳轉(zhuǎn)" 按鈕,在按住鍵盤上的 " option " 鍵不放,再按住鼠標(biāo)右鍵不放,進(jìn)行拖動(dòng)到 " Second Nav View Controller" 頁(yè)面上,就會(huì)出現(xiàn)各色的方框,如上圖所示。然后選擇 " Show" 即可。

故事板的導(dǎo)航跳轉(zhuǎn)的效果圖
Untitled.gif

4、 模態(tài)跳轉(zhuǎn)

1、 添加模態(tài)跳轉(zhuǎn)的觸發(fā)按鈕,如下圖所示:
15D27C97-21FD-4692-B7A6-4F1F308E22B7.png
2、 添加模態(tài)跳轉(zhuǎn)的控制器,如下圖
194C4568-16D3-439C-8F5E-E93284F05562.png
3 、 他們的關(guān)系添連接如下圖:
81A05107-CACA-4320-98FF-C50F3A988197.png

操作方法:點(diǎn)擊紅色的 " 模態(tài)跳轉(zhuǎn)" 按鈕,在按住鍵盤上的 " option " 鍵不放,再按住鼠標(biāo)右鍵不放,進(jìn)行拖動(dòng)到 " Second Nav View Controller" 頁(yè)面上,就會(huì)出現(xiàn)各色的方框,如上圖所示。然后選擇 " Present Modally" 即可。

4、 模態(tài)跳轉(zhuǎn)的效果圖
Untitled.gif
最后編輯于
?著作權(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)容