swift3.0之導(dǎo)航欄,底部導(dǎo)航菜單,委托

一,導(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: "第一頁面",image: UIImage(named: "tabbar_home"), selectedImage: UIImage(named:"tabbar_home_selected"))

firstVC.tabBarItem = item1

let secondVC = SortViewController ()

let item2 : UITabBarItem = UITabBarItem (title: "第二頁面",image: UIImage(named: "tabbar_sort"), selectedImage: UIImage(named:"tabbar_sort_selected"))

secondVC.tabBarItem = item2

let otherVC = OtherViewController ()

let item3 : UITabBarItem = UITabBarItem (title: "第三頁面",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對閉包進(jì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"]

原文鏈接:http://m.itdecent.cn/p/b71c740018b5

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容