标签:
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 = detail 21 //用导航包装master列表,显示导航条,如果是分割面板也不影响功能 22 let nav = UINavigationController(rootViewController: master) 23 // 如果是iPhone或iPod则只显示列表页,如果是iPad则显示分割面板 24 if (UIDevice.currentDevice().userInterfaceIdiom == .Phone) { 25 self.window!.rootViewController = nav 26 } 27 else { 28 //初始化分割面板 29 let split = UISplitViewController() 30 //设置分割面板的2个视图控制器 31 split.viewControllers = [nav, detail] 32 33 //分割面板作为window的主视图加载 34 self.window!.rootViewController = split 35 } 36 37 return true 38 } 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 }
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 = self 19 self.tableView!.dataSource = self 20 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) -> Int 31 { 32 return self.ctrls.count 33 } 34 35 // UITableViewDataSource协议方法 36 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) 37 -> UITableViewCell 38 { 39 let cell = tableView.dequeueReusableCellWithIdentifier("SwiftCell", 40 forIndexPath: indexPath) as UITableViewCell 41 cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator 42 cell.textLabel?.text = self.ctrls[indexPath.row] 43 44 return cell 45 } 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跳转到detailViewController 59 self.navigationController!.pushViewController(detailViewController!, animated:true) 60 } 61 } 62 }
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.Center 26 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 }
标签:
原文地址:http://www.cnblogs.com/iCocos/p/4671509.html