上节地址:http://blog.csdn.net/lwjok2007/article/details/48346719
我们接着上节继续看返回指定VIEwController
首先再创建两个UIVIEwController的子类
分别命名:ThirdVIEwController,ForthVIEwController
然后我们分别在SecondVIEwController 和 ThirdVIEwController上添加button点击后跳转下一个页面
SecondVIEwController代码
import UIKitclass SecondVIEwController: UIVIEwController { overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() // Do any additional setup after loading the vIEw. self.title="第二个页面" let btn=UIbutton(frame: CGRectMake(20,120,320,36)) btn.setTitle("d出第三个视图",forState: UIControlState.normal) btn.setTitlecolor(UIcolor.blackcolor(),forState: UIControlState.normal) btn.addTarget(self,action: "openAct",forControlEvents: UIControlEvents.touchDown) self.vIEw.addSubvIEw(btn) } func openAct(){ let thirdVC=ThirdVIEwController() self.navigationController?.pushVIEwController(thirdVC,animated: true) } overrIDe func dIDReceiveMemoryWarning() { super.dIDReceiveMemoryWarning() // dispose of any resources that can be recreated. } /* // MARK: - Navigation // In a storyboard-based application,you will often want to do a little preparation before navigation overrIDe func prepareForSegue(segue: UIStoryboardSegue,sender: AnyObject?) { // Get the new vIEw controller using segue.destinationVIEwController. // Pass the selected object to the new vIEw controller. } */}
ThirdVIEwController代码
import UIKitclass ThirdVIEwController: UIVIEwController { overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() // Do any additional setup after loading the vIEw. self.title="第三个页面" let btn=UIbutton(frame: CGRectMake(20,36)) btn.setTitle("d出第四个视图",forControlEvents: UIControlEvents.touchDown) self.vIEw.addSubvIEw(btn) } func openAct(){ let forthVC=ForthVIEwController() self.navigationController?.pushVIEwController(forthVC,sender: AnyObject?) { // Get the new vIEw controller using segue.destinationVIEwController. // Pass the selected object to the new vIEw controller. } */}
我们执行代码就发现已经可以跳转了
接下来我们给FortthVIEwController 添加三个button。
let btn=UIbutton(frame: CGRectMake(20,36)) btn.setTitle("回到第一个页面",forState: UIControlState.normal) btn.tag=91 btn.addTarget(self,action: "openAct:",forControlEvents: UIControlEvents.touchDown) self.vIEw.addSubvIEw(btn) let btn2=UIbutton(frame: CGRectMake(20,170,36)) btn2.setTitle("回到第二个页面",forState: UIControlState.normal) btn2.setTitlecolor(UIcolor.blackcolor(),forState: UIControlState.normal) btn2.tag=92 btn2.addTarget(self,forControlEvents: UIControlEvents.touchDown) self.vIEw.addSubvIEw(btn2) let btn3=UIbutton(frame: CGRectMake(20,220,36)) btn3.setTitle("回到第三个页面",forState: UIControlState.normal) btn3.setTitlecolor(UIcolor.blackcolor(),forState: UIControlState.normal) btn3.tag=93 btn3.addTarget(self,forControlEvents: UIControlEvents.touchDown) self.vIEw.addSubvIEw(btn3)
func openAct(btn:UIbutton){ if btn.tag==91{ //回到首页 self.navigationController?.popToRootVIEwControllerAnimated(true) }else if btn.tag==92{ //通过堆栈顺序获取要跳转的视图控制器 let vc=self.navigationController?.vIEwControllers[1] as! UIVIEwController self.navigationController?.popToVIEwController(vc,animated: true) }else if btn.tag==93{ //返回到上一个页面 self.navigationController?.popVIEwControllerAnimated(true) } }
我们点击按钮看下效果
这里要说明的一点是 跳转到指定VIEwController中我们首先调用了NavigationController的vIEwControllers[1]来获取了要跳转到的视图控制器。视图控制器跳转的时候是按照栈的方式来的.
我们例子中的首页是第一次出现在navigationController中的,所以栈底就是我们需要的首页,我们可以使用vIEwControllers[0] 来测试一下看能不能跳转首页
好了 基本简单的跳转都实现了。大家有问题欢迎加群讨论
苹果开发群 :414319235 欢迎加入 欢迎讨论问题
总结以上是内存溢出为你收集整理的UINavigationController视图控制器切换(二)全部内容,希望文章能够帮你解决UINavigationController视图控制器切换(二)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)