一,导航栏UINavergationController
创建
letviewroot=ViewController()
letnav=UINavigationController(rootViewController:viewroot)
self.window?.rootViewController=nav
入栈
var firstView : UIViewController?
firstView = firstViewController()
self.navigationController?.pushViewController(firstView!,animated:true);
二,底部导航菜单
先自定义UITabBarController,方便管理
1、创建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、创建2个空Controller如HomeViewController、SortViewController、OtherViewController
4、在RootTabBarController类里创建tabbar的子控制器
class RootTabBarController:UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
//创建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
}
三,委托协议
1第一步A中
protocol RentProtocol{
//协议内容
func Renting()
}
第二步A中
var rentDelegate:RentProtocol?
第三步A中
let first = firstViewController()
let second = secondViewController()
second.Renting()
first.rentDelegate=second
rentDelegate?.Renting()
第四步
func Renting(){
print("执行代理方法")
}
四,闭包
Swift对闭包进行了简化:
利用上下文推断参数和返回值类型
隐式返回单表达式闭包,即单表达式闭包可以省略return关键字
参数名称缩写
尾随(Trailing)闭包语法
先来看一个排序的例子,数组的降序排列
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"]
网友评论