Swift - 使用导航条和导航条控制器来进行页面切换

Swift - 使用导航条和导航条控制器来进行页面切换,第1张

概述通过使用导航条(UINavigationBar)与导航条控制器(UINavigationController)可以方便的在主页面和多层子页面之间切换。下面通过一个简单“组件效果演示”的小例子来说明如何通过代码来进行页面的切换。 功能如下: 1,在AppDelagete.swift入口文件中把首页ViewController做了导航控件的封装 2,首页是一个表格列出几个Swift控件的名称 3,点击 通过使用导航条(UINavigationbar)与导航条控制器(UINavigationController)可以方便的在主页面和多层子页面之间切换。下面通过一个简单“组件效果演示”的小例子来说明如何通过代码来进行页面的切换。
功能如下: 1,在AppDelagete.swift入口文件中把首页VIEwController做了导航控件的封装 2,首页是一个表格列出几个Swift控件的名称 3,点击表格项即切换到对应组件展示页面,顶部的导航条标题变为该控件的名称,同时导航条左侧还有返回按钮 4,在展示页中,给导航条右侧添加了“效果/代码”切换的按钮,点击分别展示组件的效果和代码 效果图如下: 代码如下: --- 入口文件AppDelegate.swift ---
1 2 3 4 5 6 7 @H_301_53@ 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 import UIKit @UIApplicationMain class AppDelegate: UIResponder,UIApplicationDelegate { var window: UIWindow? func application(application: UIApplication, dIDFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // 把起始VIEwController作为导航控件封装,我们在VIEwController里就能调用导航条进行页面切换了 let rootVIEwController = VIEwController() let rootNavigationController = UINavigationController(rootVIEwController: rootVIEwController) self.window!.rootVIEwController = rootNavigationController return true } func applicationWillResignActive(application: UIApplication) { } @H_502_187@func applicationDIDEnterBackground(application: UIApplication) { } func applicationWillEnterForeground(application: UIApplication) { } func applicationDIDBecomeActive(application: UIApplication) { } func applicationWillTerminate(application: UIApplication) { } }
--- 主页面VIEwController.swift --- 11 @H_502_235@ 12 13 14 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
VIEwController: UIVIEwController,UItableVIEwDelegate,UItableVIEwDataSource { // 表格加载 @H_701_301@tableVIEw:UItableVIEw? // 控件类型 ctrls = [ "UILabel" , "UIbutton" "UIImageVIEw" "UiSlider" "UIWebVIEw" ] overrIDe func vIEwDIDLoad() { super .vIEwDIDLoad() self.Title = "Swift控件演示" self.tableVIEw = UItableVIEw(frame:self.vIEw.frame,style:UItableVIEwStyle.Plain) self.tableVIEw!.delegate = self self.tableVIEw!.dataSource = self self.tableVIEw!.registerClass(UItableVIEwCell.self,forCellReuseIDentifIEr: "SwiftCell" ) self.vIEw.addSubvIEw(self.tableVIEw!) @H_502_187@func dIDReceiveMemoryWarning() { .dIDReceiveMemoryWarning() // dispose of any resources that can be recreated. } // UItableVIEwDataSource协议方法 func tableVIEw(tableVIEw: UItableVIEw,numberOfRowsInSection section: Int) -> Int { return self.ctrls.count } // UItableVIEwDataSource协议方法 -> UItableVIEwCell { let cell = tableVIEw.dequeueReusableCellWithIDentifIEr( UItableVIEwCell cell.accessoryType = UItableVIEwCellAccessoryType.disclosureIndicator cell.textLabel?.text = self.ctrls[indexPath.row] cell } // UItableVIEwDelegate协议方法,点击时调用 { // 跳转到detailVIEwController,取消选中状态 self.tableVIEw!.deselectRowAtIndexPath(indexPath,animated: true ) // 创建DetailVIEwController let detailVIEwController = DetailVIEwController() // 传递控件的Title,在detailVIEw里用于判断生成响应的控件 detailVIEwController.Title = self.ctrls[indexPath.row] // navigationController跳转到detailVIEwController self.navigationController!.pushVIEwController(detailVIEwController,monospace!important; min-height:inherit!important">) } --- 子页面DetailVIEwController.swift --- 14 15 16 17 50 51 52 53 54 55 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 @H_404_611@ 110 111 112 113 114 115 116 117 118 119 120
DetailVIEwController: UIVIEwController { func vIEwDIDLoad() { @[email protected]() //设置背景色 self.vIEw.backgroundcolor = UIcolor.whitecolor() //按Title加载控件 loadControl(self.Title!) //设置代码和控件展示切换按钮,增加到导航条的右侧 //这里采用了navigationController不能增加navigationItem let btn = UIbarbuttonItem(Title: "代码" "btnCodeClicked:" ) self.navigationItem.rightbarbuttonItem = btn } func dIDReceiveMemoryWarning() { .dIDReceiveMemoryWarning() @H_502_187@} //构建控件并加载到界面 func loadControl(ctrl: String ) { switch (ctrl) { case : let label = UILabel(frame: self.vIEw.bounds) .backgroundcolor = UIcolor.clearcolor() .textAlignment = NSTextAlignment.Center .Font = UIFont.systemFontOfSize( 36 ) .text = "Hello,hangge.com" self.vIEw.addSubvIEw( ) : let button = UIbutton(frame: CGRectMake( 110 120 100 60 )) button.backgroundcolor = UIcolor.bluecolor() button.setTitlecolor(UIcolor.redcolor(),forState: UIControlState.normal) button.setTitlecolor(UIcolor.whitecolor(),forState: UIControlState.Highlighted) button.setTitle( "点击我" "buttonClicked:" "swift.png" ) let imageVIEw = UIImageVIEw(frame: CGRectMake((CGRectGetWIDth(self.vIEw.bounds) - image!.size.wIDth) / 2.0 120.0 60.0 200.0 30.0 )) self.vIEw.addSubvIEw(slIDer) : let webVIEw = UIWebVIEw(frame:self.vIEw.bounds) let url = NSURL(string: "http://www.hangge.com" let request = NSURLRequest(URL: url!) webVIEw.loadRequest(request) @[email protected](webVIEw) default : print( "control name: \(ctrl)" ) } } //显示控件的代码 func loadCode(ctrl: ) { str: String (ctrl) { : str = "let label = UILabel(frame: self.vIEw.bounds)\n" str += "label.backgroundcolor = UIcolor.clearcolor()\n" "label.textAlignment = NSTextAlignment.Center\n" "label.Font = UIFont.systemFontOfSize(36)\n" "label.text = \"Hello,Ucai\"\n" "self.vIEw.addSubvIEw(label)" : "UIbutton" : "let slIDer = UiSlider(frame:CGRectMake(60.0,120.0,200.0,30.0))\n" "self.vIEw.addSubvIEw(slIDer)" : "other ctrl" } //在导航条下方位置显示源代码 let txt = UITextVIEw( frame: CGRectMake( 0 )) txt.text = str self.vIEw.addSubvIEw(txt) } //清空所有子视图 func clearVIEws() { for v in self.vIEw.subvIEws [UIVIEw] { v.removeFromSupervIEw() } } func buttonClicked(sender:AnyObject) { "you clicked button" ) } //显示控件的代码 func btnCodeClicked(sender:AnyObject) { "Title: \(self.Title)" ) clearVIEws() if self.navigationItem.rightbarbuttonItem!.Title == { loadCode(self.Title!) @[email protected]!.Title = "效果" } else { "代码" loadControl(self.Title!) } } /* func btnBackClicked(sender:AnyObject) { self.navigationController.navigationbar.popNavigationItemAnimated(true) } */ }

如果使用StoryBoard实现更加简单 AppDelegate.swift都不需要修改。打开Main.storyboard。 (1)点击首页的Scene,选择Editor -> Embed In -> Navigation Controller 即可。
(2)从首页单元格拖“show”的关联Segue到详细页,或从首页VIEw Controller拖手动关联Segue到详细页
(3)定义上面刚添加的Segue的IndentifIEr(比如detail)
这样,点击单元格跳转的代码有所改变,是根据刚才定义Segue的IndentifIEr来跳转
1 2 3 4 5 6 7 @H_301_53@ 8 9 10 11 @H_502_235@ 12 13 14 15 16 // UItableVIEwDelegate协议方法,点击时调用 // 跳转到detailVIEwController,取消选中状态 true ) //更具定义的Segue IndentifIEr进行跳转 self.performSegueWithIDentifIEr( "detail" overrIDe func prepareForSegue(segue: UIStoryboardSegue,sender: AnyObject?) { if segue.IDentifIEr == { let controller = segue.destinationVIEwController as ! DetailVIEwController controller.Title = sender ? String } }

原文出自: www.hangge.com 转载请保留原文链接: http://www.hangge.com/blog/cache/detail_586.html 总结

以上是内存溢出为你收集整理的Swift - 使用导航条和导航条控制器来进行页面切换全部内容,希望文章能够帮你解决Swift - 使用导航条和导航条控制器来进行页面切换所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1080449.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)