標籤:android winform style blog http color
搭架子
首先這次我們會主要使用IOS內建的導航Controller為一個APP建立一個簡單的基石,
建立一個空的Application並建立3個swift檔案,分別命名為:FirstViewController,SecondViewController,ThirdViewController;
同時在三個Swift的Controller中重寫繼承類的viewDidLoad()方法:
override func viewDidLoad(){ super.viewDidLoad();}
在整個程式中的AppDelegate.swift檔案,是整個APP的事件代理類,這裡可以展現整個APP的生命特徵,同時有.net程式中Globle檔案的作用,可以作為主程式聲明用。
在此檔案中我們加入如下代碼,將建立的3個Controller註冊到Application中:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool { self.window = UIWindow(frame: UIScreen.mainScreen().bounds) // Override point for customization after application launch. self.window!.backgroundColor = UIColor.whiteColor() let firstVC=FirstController(); let nav1=UINavigationController(rootViewController:firstVC); let image1=UIImage(named:"h.png"); nav1.tabBarItem=UITabBarItem(title:"完美食材",image:image1,tag:1); let secondVC=SecondConreoller(); let nav2=UINavigationController(rootViewController:secondVC); let image2=UIImage(named:"o.png"); nav2.tabBarItem=UITabBarItem(title:"國色天香",image:image2,tag:2); let thirdVC=ThirdController(); let nav3=UINavigationController(rootViewController:thirdVC); let image3=UIImage(named:"s.png"); nav3.tabBarItem=UITabBarItem(title:"實惠經典",image:image3,tag:3); let navArr=[nav1,nav2,nav3]; let tabBarController=UITabBarController(); tabBarController.viewControllers=navArr; self.window!.rootViewController=tabBarController; self.window!.makeKeyAndVisible() return true }
簡單說明:
我們通過定義3個變數,聲明了我們建立的3個Controller,通過設定UINavigationController的RootViewController,將3個Controller裝載在3個UINavigationController中。
我們同時決定再將3個NavigationController放在另一個容器:UITabBarViewController中,
如:
let navArr=[nav1,nav2,nav3]; let tabBarController=UITabBarController(); tabBarController.viewControllers=navArr; self.window!.rootViewController=tabBarController;
由於我們同樣需要再TabBar上友好的顯示對應的導航資訊,所以我們給對應的UINavigationController設定了帶圖片帶TabBarItem:
let image1=UIImage(named:"h.png"); nav1.tabBarItem=UITabBarItem(title:"完美食材",image:image1,tag:1);
程式跑起來,看看效果:
不加TabBarItem圖片的效果:
設定TabBarItem後的效果:
(由於沒有去網上找對應的圖片,所以隨便用了以前的一些圖片將就下哈。)
雖然我們只是動了AppDelegate檔案,但是一個常見的架子就呈現在我們面前了,
簡單的通過IOS內建的一些預製的Controller容器搭起來了一個常見的APP架子,相比於另外的兩個移動平台Android和wp,iOS在這方面做的確實不錯,因為在移動這個小的表單面前,我們的操作是有限的,預製的這20%的功能卻減少了80%的事情,同時不會像Android的看著那麼碎。
填充內容:
既然架子起來了,就該給對應的Controller填充內容了。
我們希望使用者在點擊下方3個有表徵圖的TabBar的時候可以看到對應的3個建立的Controller的變化。
於是我們在每個的Controller中實現TableView:
class ThirdController:UIViewController,UITableViewDelegate,UITableViewDataSource
首先,按照國際慣例,我們對3個自建的Controller繼承UITableVIewDelegate和UITableDataSource
熟悉.net的朋友看見datasource就會想到之前在webform或者winform使用Gride的時候都會定義對應的DataSource,所以只要記住,當我們想要呈現並使用Gride時就需要繼承DataSource了,繼承這兩個代理之後就必須去實現兩個方法:
//實現繼承的委託中的兩個方法 func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int{ return 100; } //返回Cell func tableView(tableView:UITableView!, cellForRowAtIndexPath indexPath:NSIndexPath!)->UITableViewCell!{ let cellID="Cell ID"; var cell=tableView.dequeueReusableCellWithIdentifier(cellID) as? UITableViewCell; return cell; }
第一個是返回table的行數(Int);
第二個是返回table中每一個的Cell;
接下來,我們類比填充一個數組將TableView呈現出來:
//預製資料 var dataList=NSMutableArray(); //放置並顯示資料的TableView容器 var _tableView:UITableView?; override func viewDidLoad(){ super.viewDidLoad(); self.title="實惠經典" for(var i=0;i<100;i++){ dataList.addObject("第\(i)個實惠"); } //拿到當前視圖(view)的邊界 var rect:CGRect=self.view.bounds; _tableView=UITableView(frame:rect,style:.Plain); _tableView!.delegate=self; _tableView!.dataSource=self; self.view.addSubview(_tableView); } //實現繼承的委託中的兩個方法 func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int{ return dataList.count; } //返回Cell func tableView(tableView:UITableView!, cellForRowAtIndexPath indexPath:NSIndexPath!)->UITableViewCell!{ let cellID="Cell ID"; var cell=tableView.dequeueReusableCellWithIdentifier(cellID) as? UITableViewCell; if(cell == nil){ cell=UITableViewCell(style:.Default,reuseIdentifier:cellID); } var str=dataList.objectAtIndex(indexPath.row) as? String; cell!.textLabel.text=str; return cell; }
呈現結果:
這樣我們就會看到點擊不同的TabBar呈現了不同的Controller了。
我們想看看如何擷取TableView對應的點擊行,怎麼辦?
//點中行事件 func tableView(tableView:UITableView!,didSelectRowAtIndexPath indexpath:NSIndexPath!){ println("第 \(indexpath.row)行被點擊了"); }
這樣可以通過Console看到點擊不同行之後的輸出。
目錄結構,通過檔案夾建立一種MVC的趕腳:
整個簡單的基於IOS內建的幾種容器型Controller就這樣出現了,結合IOS提供的便利,在加上可讀性很強的swift使得iOS開發較以前真的方便好多啊。