一,導(dǎo)航欄UINavergationController
創(chuàng)建
letviewroot=ViewController()
letnav=UINavigationController(rootViewController:viewroot)
self.window?.rootViewController=nav
入棧
var firstView : UIViewController?
firstView = firstViewController()
self.navigationController?.pushViewController(firstView!,animated:true);
二,底部導(dǎo)航菜單
先自定義UITabBarController,方便管理
1、創(chuàng)建UITabBarController的子類RootTabBarController
class RootTabBarController:UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
}
2、在AppDelegate類里指定RootTabBarController為根視圖
class AppDelegate: UIResponder,UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication,didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool{
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.makeKeyAndVisible()
let root= RootTabBarController()
self.window?.rootViewController=root
// Override point for customization after application launch.
return true
}
3露久、創(chuàng)建2個(gè)空Controller如HomeViewController要尔、SortViewController闹击、OtherViewController
4妥畏、在RootTabBarController類里創(chuàng)建tabbar的子控制器
class RootTabBarController:UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
//創(chuàng)建tabbar的子控制器
self.creatSubViewControllers()
}
func creatSubViewControllers(){
let firstVC= HomeViewController()
let item1 : UITabBarItem = UITabBarItem (title: "第一頁(yè)面",image: UIImage(named: "tabbar_home"), selectedImage: UIImage(named:"tabbar_home_selected"))
firstVC.tabBarItem = item1
let secondVC = SortViewController ()
let item2 : UITabBarItem = UITabBarItem (title: "第二頁(yè)面",image: UIImage(named: "tabbar_sort"), selectedImage: UIImage(named:"tabbar_sort_selected"))
secondVC.tabBarItem = item2
let otherVC = OtherViewController ()
let item3 : UITabBarItem = UITabBarItem (title: "第三頁(yè)面",image: UIImage(named: "tabbar_other"), selectedImage: UIImage(named:"tabbar_other_selected"))
otherVC.tabBarItem = item3
let tabArray = [firstVC,secondVC,otherVC]
self.viewControllers = tabArray
}
三,委托協(xié)議
1第一步A中
protocol RentProtocol{
//協(xié)議內(nèi)容
func Renting()
}
第二步A中
var rentDelegate:RentProtocol?
第三步A中
let first = firstViewController()
let second = secondViewController()
second.Renting()
first.rentDelegate=second
rentDelegate?.Renting()
第四步
func Renting(){
print("執(zhí)行代理方法")
}
四,閉包
Swift對(duì)閉包進(jìn)行了簡(jiǎn)化:
利用上下文推斷參數(shù)和返回值類型
隱式返回單表達(dá)式閉包智润,即單表達(dá)式閉包可以省略return關(guān)鍵字
參數(shù)名稱縮寫
尾隨(Trailing)閉包語法
先來看一個(gè)排序的例子国葬,數(shù)組的降序排列
var usernames = ["Lves","Wildcat", "Cc", "Lecoding"]
func backWards(s1: String, s2: String)-> Bool
{
return s1 > s2
}
var resultName1 = usernames.sort(backWards)
//resultName1: ["Wildcat","Lves", "Lecoding", "Cc"]