一 、 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

添加的控制器如上圖藍(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é)再說)

如圖上圖所示。
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)返回")
};
}
效果如圖:

四 、故事板的導(dǎo)航跳轉(zhuǎn) & 模態(tài)跳轉(zhuǎn)
1、 首先要添加導(dǎo)航控制器,并設(shè)置為App的入口。如下圖:

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

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

操作方法是: 用鼠標(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)的控制器

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

操作方法:點(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)的效果圖

4、 模態(tài)跳轉(zhuǎn)
1、 添加模態(tài)跳轉(zhuǎn)的觸發(fā)按鈕,如下圖所示:

2、 添加模態(tài)跳轉(zhuǎn)的控制器,如下圖

3 、 他們的關(guān)系添連接如下圖:

操作方法:點(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)的效果圖
