IOS第9天控制器的使用
来源:互联网 发布:防火知多少课件 编辑:程序博客网 时间:2024/06/05 11:56
1、UINavigationController导航控制器
在使用UINavigationController之前一般需要先如下的处理按照代码中的5个步骤来进行处理
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Override point for customization after application launch. //1.注意删除main.storyboard先删除绝对路径在删相对路径 //2.新建window self.window = UIWindow.init() self.window?.frame = UIScreen.mainScreen().bounds //3.设置根控制器 self.window?.rootViewController = ViewController() self.window?.makeKeyAndVisible()//4.把这个window变成主窗口 return true //5.将项目中的main interface设置为空 }这个代码实现了如何将某个控制器作为根控制器显示在界面上。我们在使用UINavigationController时就需要把其设为根控制器。
新建Ios项目修改AppDelegate文件
import UIKit@UIApplicationMainclass AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { self.window?.rootViewController = UINavigationController(rootViewController: ViewController()) return true } func applicationWillResignActive(application: UIApplication) { } func applicationDidEnterBackground(application: UIApplication) { } func applicationWillEnterForeground(application: UIApplication) { } func applicationDidBecomeActive(application: UIApplication) { } func applicationWillTerminate(application: UIApplication) { }}ViewController.swift文件
import UIKitclass ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor = UIColor.whiteColor() self.title = "首页" self.navigationController?.navigationBar.hidden = false//不隐藏导航条 self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(title: "跳", style: UIBarButtonItemStyle.Plain, target: self, action: "jump") } func jump(){ print("jump") } override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { self.navigationController?.pushViewController(VC1(), animated: true)//添加新的视图到导航控制器堆栈 } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. }}VC1.swift
import UIKitclass VC1: UIViewController { override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = UIColor.grayColor() self.title = "VC1" // Do any additional setup after loading the view.// self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(title: "回家", style: UIBarButtonItemStyle.Bordered, target: self, action: "btnBackDidClick") var uiBarBtnBack = UIBarButtonItem.init(title: "回家", style: UIBarButtonItemStyle.Bordered, target: self, action: "btnBackDidClick") var uiBarBtnGo = UIBarButtonItem.init(title: "离开", style: UIBarButtonItemStyle.Bordered, target: self, action: "btnBackDidClick") self.navigationItem.leftBarButtonItems = [uiBarBtnBack, uiBarBtnGo] } func btnBackDidClick(){ print("我不会骑")<pre name="code" class="plain">VC2.swift
import UIKitclass VC2: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. self.view.backgroundColor = UIColor.greenColor() self.title = "VC2" } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {// self.navigationController?.popToRootViewControllerAnimated(true) //关掉自己回到上次的跳转来的 self.navigationController?.popViewControllerAnimated(true) }}//UINavigationController使用的时候必须设置根控制器
2、UITabBarController
import UIKit@UIApplicationMainclass AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? var vc1:VC1? var vc:ViewController? var vc2:VC2? func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Override point for customization after application launch. vc = ViewController() //vc?.tabBarItem.title = "牛牛" var a = ViewController() var item = UITabBarItem.init(title: "小芳", image: UIImage(named: "a3.jpg"), tag: 10000) vc?.tabBarItem = item vc1 = VC1() vc1?.title = "vc1" vc1?.tabBarItem.badgeValue = "99+" vc1?.tabBarItem.badgeValue = nil vc2 = VC2() self.window = UIWindow.init() self.window?.frame = UIScreen.mainScreen().bounds var TC = UITabBarController() self.window?.rootViewController = TC //TC.addChildViewController(vc!)//添加控制器的方式1 TC.viewControllers = [vc!, vc1!, vc2!]//添加控制器的方式2 self.window?.makeKeyAndVisible() return true } func applicationWillResignActive(application: UIApplication) { } func applicationDidEnterBackground(application: UIApplication) { } func applicationWillEnterForeground(application: UIApplication) { } func applicationDidBecomeActive(application: UIApplication) { } func applicationWillTerminate(application: UIApplication) { }}
代码中用到的几个视图文件基本不用添加代码只是设置了title和背景颜色因此不在贴出来
0 0
- IOS第9天控制器的使用
- iOS导航控制器的使用
- IOS-导航视图控制器的使用
- 导航控制器的使用——IOS
- IOS OC tabbarView控制器的使用
- iOS中导航控制器的使用 UINavigationController
- ios中导航控制器的使用
- IOS第10天 UITableView的使用
- iOS 9的自定义导航控制器
- (iOS)控制器的生命周期
- ios控制器的生命周期
- iOS 控制器的生命周期
- ios-day11-06(使用代码的方式创建导航控制器——UINavigationController、使用导航控制器管理其他控制器)
- IOS的基本控件的使用-步进器&分栏控制器
- iOS 获取当前控制器的顶层控制器
- IOS UITableView表视图和导航控制器的交互使用
- iOS-UI-05 导航控制器的使用 UINavigationController
- iOS开发中 UITabBarController--标签控制器的使用
- .Net中的几个小工具
- 配置ext重新抓取
- LabVIEW仿真点阵移动显示字符
- find the safest road(最短路改编,最长路)
- linux热插拔
- IOS第9天控制器的使用
- 数组中重复的数字
- GetDeviceCaps WinAPI
- Ftp服务器搭建
- 搜索+枚举字符串子串问题
- Android图片下载缓存库picasso解析
- 贝叶斯分类算法值贝叶斯网络
- git菜鸟教程
- maven简单配置