iOS Development--device article Swift & judging device type

Source: Internet
Author: User

Determine device type
1,Split View Controller(Uisplitviewcontroller) in the iphone application, the navigation controller is used to enter the next layer of interface from the previous interface. But the ipad screen is large and usually uses Splitviewcontroller for navigation (this is the ipad-specific view controller). On the horizontal screen, a navigation list is displayed on the left, and the corresponding details are displayed on the right. In the case of vertical screen display will be different, the default is only the detailed panel, the original left navigation list is hidden through the floating window, you need to drag from the edge inward to display. 2,develop a compatible iOS appThere are times when you need to develop apps that are compatible with iphone, ipod and ipad, and you need to determine the device type, and you should not use Splitviewcontroller for iphone or ipod. There will also be a change in processing, such as clicking on a list item, displaying details on the right side of the ipad, while the iphone needs to navigate to the detail page. iOS provides the Uidevice class to determine the type of device, its Userinterfaceidiom property returns the device type enumeration 3,Sample ExampleIphone:ipad:
4, sample code
---appdelegate.swift Application entry---
1 Import UIKit2  3 @UIApplicationMain4 classAppdelegate:uiresponder, uiapplicationdelegate {5                              6var Window:uiwindow?7  8 func Application (application:uiapplication,9Didfinishlaunchingwithoptions launchoptions:nsdictionary?) -Bool {TenSelf.window =UIWindow (Frame:UIScreen.mainScreen (). Bounds) One         //Override point for customization after application launch. Aself.window!. BackgroundColor =Uicolor.whitecolor () -self.window!. makekeyandvisible () -           the         //initializing the list panel -Let master =Masterviewcontroller () -         //Initialize the details panel -Let detail =Detailviewcontroller () +         //set the list Panel reference Details panel so that the appropriate method for invoking the details panel when the user taps the list item -Master.detailviewcontroller =Detail +         //use the navigation to wrap the master list, display the navigation bar, if it is a split panel does not affect the function ALet Nav =Uinavigationcontroller (rootviewcontroller:master) at         //if it's an iphone or ipod, only the list page appears, and if it's an ipad, the split panelif (Uidevice.currentdevice (). Userinterfaceidiom = =. Phone) {self.window!. Rootviewcontroller = nav26}27 Else {28//Initialize split panel + let split = Uisplitviewcontrol Ler () 30//Set 2 view Controller for split panel split.viewcontrollers = [nav, detail]32 33//split panel for Loads the self.window! for the main view of window. Rootviewcontroller = Split35}36Panax Notoginseng         return true -     } the   + func applicationwillresignactive (application:uiapplication) { A     } the   + func Applicationdidenterbackground (application:uiapplication) { -     } $   $ func Applicationwillenterforeground (application:uiapplication) { -     } -   the func applicationdidbecomeactive (application:uiapplication) { -     }Wuyi   the func applicationwillterminate (application:uiapplication) { -     } Wu}


---masterviewcontroller.swift list page---
1 Import UIKit2  3 classMasterviewcontroller:uiviewcontroller, Uitableviewdelegate, Uitableviewdatasource {4      5     //Table Loading6var tableview:uitableview?7     //control type8var ctrls = ["UILabel","UIButton","Uiimageview","UISlider"]9     //Tenvar detailviewcontroller:detailviewcontroller? One       A     Overridefunc viewdidload () { - super.viewdidload () -         //additional setup after loading the view, typically from a nib. the           -Self.title ="Swift Control Demo" -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!) A     } at       -     Overridefunc didreceivememorywarning () { - super.didreceivememorywarning () -         //Dispose of any resources the can be recreated. -     } -       in     //Uitableviewdatasource Protocol Method -Func TableView (Tableview:uitableview, numberofrowsinsection section:int)Int to     { +         returnSelf.ctrls.count -     } the       *     //Uitableviewdatasource Protocol Method $ func TableView (Tableview:uitableview, Cellforrowatindexpath Indexpath:nsindexpath)Panax Notoginseng-UITableViewCell -     { theLet cell = Tableview.dequeuereusablecellwithidentifier ("Swiftcell", +Forindexpath:indexpath) asUITableViewCell ACell.accessorytype =Uitableviewcellaccessorytype.disclosureindicator theCell.textlabel?. Text =Self.ctrls[indexpath.row] +           -         returnCell $     } $       -     //Uitableviewdelegate protocol method, call when clicked -Func TableView (tableview:uitableview!, Didselectrowatindexpath indexpath:nsindexpath!) the     { -         //Call the Detailviewcontroller Method Update Detail PageWuyidetailviewcontroller!. LoadControl (Self.ctrls[indexpath.row]) the          53//If it is an iphone, the ipod navigates to the details page, Uidevice.currentdevice (). Userinterfaceidiom = =. Phone) {55//Jump to Detailviewcontroller, uncheck the status of//self.tableview!. Deselectrowatindexpath (Indexpath, animated:true)//Navigationcontroller jump to Detailviewcontroller self.navigationcontroller!. Pushviewcontroller (detailviewcontroller!, animated:true) A     } +}


---detailviewcontroller.swift details page---
1 Import UIKit2  3 classDetailviewcontroller:uiviewcontroller {4      5     Overridefunc viewdidload () {6 super.viewdidload ()7         //additional setup after loading the view, typically from a nib.8          9Self.view.backgroundColor =Uicolor.whitecolor ()TenLet Ctrl = Self.title! = nil? self.title! :"" One LoadControl (ctrl) A     } -       -     Overridefunc didreceivememorywarning () { the super.didreceivememorywarning () -         //Dispose of any resources the can be recreated. -     } -       + func LoadControl (ctrl:string) { - clearviews () +         Switch(ctrl) { A          Case "UILabel": atvar label =UILabel (frame:self.view.bounds) -Label.backgroundcolor =Uicolor.clearcolor () -Label.textalignment =Nstextalignment.center -Label.font = Uifont.systemfontofsize ( $) -Label.text ="Hello, hangge.com." - Self.view.addSubview (label) in          Case "UIButton": -var button = UIButton (Frame:cgrectmake ( the, -, -, -)) toButton.backgroundcolor =Uicolor.bluecolor () + Button.settitlecolor (Uicolor.redcolor (), ForState:UIControlState.Normal) - Button.settitlecolor (Uicolor.whitecolor (), forState:UIControlState.Highlighted) theButton.settitle ("Click on my", Forstate:. Normal) * self.view.addSubview (Button) $         default:Panax Notoginsengprintln"clicked: \ (ctrl)") -         } the     } +       A func clearviews () { the          forVinchSelf.view.subviews { + V.removefromsuperview () -         } $     }     $}

(Note: The project creates a new Master-detail application, which already has the same two-level navigation capabilities as the iphone and ipad)

iOS Development--device article Swift & judging device type

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.