標籤:
判斷裝置類型
1,分割視圖控制器(UISplitViewController)在iPhone應用中,使用導航控制器由上一層介面進入下一層介面。但iPad螢幕較大,通常使用SplitViewController來實現導航(這個是iPad專用的視圖控制器)。在橫屏下,左側顯示一個導航列表,點擊後右邊顯示對應的詳情。豎屏情況下顯示方式會有所不同,預設只顯示詳細面板,原來左側的導航列表會通過浮動視窗隱藏,需要從邊緣向內拖動來顯示。 2,開發相容的iOS應用有時候需要開發相容iPhone、iPod、iPad的應用,這時候需要判斷裝置類型,如果是iPhone、iPod就不應該使用SplitViewController。另外處理方式也會有變化,如點擊清單項目時,在iPad直接在右側展示詳情,而iPhone卻需要導航到詳細頁。iOS提供了UIDevice類來判斷裝置的類型,其userInterfaceIdiom屬性返回裝置類型枚舉 3,範例 iPhone: iPad:
4,範例代碼
--- AppDelegate.swift 應用入口 ---
1 import UIKit 2 3 @UIApplicationMain 4 class AppDelegate: UIResponder, UIApplicationDelegate { 5 6 var window: UIWindow? 7 8 func application(application: UIApplication, 9 didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {10 self.window = UIWindow(frame: UIScreen.mainScreen().bounds)11 // Override point for customization after application launch.12 self.window!.backgroundColor = UIColor.whiteColor()13 self.window!.makeKeyAndVisible()14 15 //初始化列表面板16 let master = MasterViewController()17 //初始化詳情面板18 let detail = DetailViewController()19 //設定列表面板引用詳情面板,以便使用者點擊清單項目時調用詳情面板的相應方法20 master.detailViewController = detail21 //用導航封裝master列表,顯示導航條,如果是分割面板也不影響功能22 let nav = UINavigationController(rootViewController: master)23 // 如果是iPhone或iPod則只顯示列表頁,如果是iPad則顯示分割面板24 if (UIDevice.currentDevice().userInterfaceIdiom == .Phone) {25 self.window!.rootViewController = nav26 }27 else {28 //初始化分割面板29 let split = UISplitViewController()30 //設定分割面板的2個視圖控制器31 split.viewControllers = [nav, detail]32 33 //分割面板作為window的主視圖載入34 self.window!.rootViewController = split35 }36 37 return true38 }39 40 func applicationWillResignActive(application: UIApplication) {41 }42 43 func applicationDidEnterBackground(application: UIApplication) {44 }45 46 func applicationWillEnterForeground(application: UIApplication) {47 }48 49 func applicationDidBecomeActive(application: UIApplication) {50 }51 52 func applicationWillTerminate(application: UIApplication) {53 }54 }
--- MasterViewController.swift 列表頁 ---
1 import UIKit 2 3 class MasterViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 4 5 // 表格載入 6 var tableView:UITableView? 7 // 控制項類型 8 var ctrls = ["UILabel", "UIButton", "UIImageView", "UISlider"] 9 //10 var detailViewController:DetailViewController?11 12 override func viewDidLoad() {13 super.viewDidLoad()14 // Do any additional setup after loading the view, typically from a nib.15 16 self.title = "Swift控制項示範"17 self.tableView = UITableView(frame:self.view.frame, style:UITableViewStyle.Plain)18 self.tableView!.delegate = self19 self.tableView!.dataSource = self20 self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "SwiftCell")21 self.view.addSubview(self.tableView!)22 }23 24 override func didReceiveMemoryWarning() {25 super.didReceiveMemoryWarning()26 // Dispose of any resources that can be recreated.27 }28 29 // UITableViewDataSource協議方法30 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int31 {32 return self.ctrls.count33 }34 35 // UITableViewDataSource協議方法36 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)37 -> UITableViewCell38 {39 let cell = tableView.dequeueReusableCellWithIdentifier("SwiftCell",40 forIndexPath: indexPath) as UITableViewCell41 cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator42 cell.textLabel?.text = self.ctrls[indexPath.row]43 44 return cell45 }46 47 // UITableViewDelegate協議方法,點擊時調用48 func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!)49 {50 //調用DetailViewController的方法更新詳細頁51 detailViewController!.loadControl(self.ctrls[indexPath.row])52 53 //如果是iPhone、iPod則導航到詳情頁54 if (UIDevice.currentDevice().userInterfaceIdiom == .Phone) {55 // 跳轉到detailViewController,取消選中狀態56 //self.tableView!.deselectRowAtIndexPath(indexPath, animated: true)57 58 // navigationController跳轉到detailViewController59 self.navigationController!.pushViewController(detailViewController!, animated:true)60 }61 }62 }
--- DetailViewController.swift 詳情頁 ---
1 import UIKit 2 3 class DetailViewController: UIViewController { 4 5 override func viewDidLoad() { 6 super.viewDidLoad() 7 // Do any additional setup after loading the view, typically from a nib. 8 9 self.view.backgroundColor = UIColor.whiteColor()10 let ctrl = self.title != nil ? self.title! : ""11 loadControl(ctrl)12 }13 14 override func didReceiveMemoryWarning() {15 super.didReceiveMemoryWarning()16 // Dispose of any resources that can be recreated.17 }18 19 func loadControl(ctrl:String) {20 clearViews()21 switch (ctrl) {22 case "UILabel":23 var label = UILabel(frame: self.view.bounds)24 label.backgroundColor = UIColor.clearColor()25 label.textAlignment = NSTextAlignment.Center26 label.font = UIFont.systemFontOfSize(36)27 label.text = "Hello, Hangge.com"28 self.view.addSubview(label)29 case "UIButton":30 var button = UIButton(frame: CGRectMake(110,120,100,60))31 button.backgroundColor = UIColor.blueColor()32 button.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)33 button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Highlighted)34 button.setTitle("點擊我", forState: .Normal)35 self.view.addSubview(button)36 default:37 println("clicked: \(ctrl)")38 }39 }40 41 func clearViews() {42 for v in self.view.subviews {43 v.removeFromSuperview()44 }45 } 46 }
(注意:項目直接建立一個Master-Detail Application,就已經具有同上述一樣的相容iPhone、iPad的二級導航功能)
iOS開發——裝置篇Swift篇&判斷裝置類型