Swift 初學習函數
1、UIViewContorller的切換
學習
首先是手動添加導航欄上面的Button,今天使用的是文字,具體添加的代碼以下動畫
// style 是Button的類型,.Plain 就顯示文字 而後點擊事件寫到自己類中的nextPage函數中 let nextView = UIBarButtonItem(title: "下一頁", style: .Plain, target: self, action: "nextPage") self.navigationItem.rightBarButtonItem = nextView //添加右側的按鈕
下面是nextPage函數的內容,就是切換到SecondContorller第二個頁面的代碼spa
func nextPage() { let nextView = SecondContorller() nextView.delegate = self // 肯定代理就是本身自己,纔會調用 代理中必須實現的函數 // 推出下一個頁面 animated 推出的動畫 self.navigationController?.pushViewController(nextView, animated: true) }
上面的delegate接下來再說。而後就是從SecondContorller返回到第一個ViewContorller代理
self.navigationController?.popViewControllerAnimated(true) //返回上一層
如今就實現了兩個頁面的切換功能!code
2、protocol協議,實現相似Java中Interface的功能事件
首先,要先寫出這個protocol的名稱相似Java中Interface,具體的函數功能不要實現ci
protocol TestDelegate { //定義一個協議 func done(done : Bool) // 使用時要實現的方法 }
而後就須要在SecondContorller中聲明此協議,而後再在使用的時候調用裏面的方法,這個跟Java中的Interface同樣,就再也不多記了。get
值得注意的是,IOS中UIViewContorller跟Android中Acitivity的差別,雖然Activity能夠通訊,可是是沒有辦法直接改變另外一個Activity中的UI的,可是UIViewContorller就能夠作到...完it