協(xié)議委托
protocol SecondViewControllerDelegate {
func didTouched(data: String?)
func fetchData() -> String
}
頁面一中用present顯示
import UIKit
class ViewController: UIViewController, SecondViewControllerDelegate {
@IBOutlet weak var titleLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func didClick(sender: UIButton) {
//1. 創(chuàng)建第二個頁面的對象
let secondCtrl = SecondViewController()
//耦合:松/緊 松點比較好
//關(guān)聯(lián)使用協(xié)議的對象和調(diào)用協(xié)議的對象,如下
secondCtrl.delegate = self //self代表了整個第一個ViewContronller
//把第二個文件賦值給當前這個類的對象埠况,使他們聯(lián)系起來
//2. 顯示
self.presentViewController(secondCtrl, animated: true, completion: nil)
//用present顯示它
}
//通過函數(shù)參數(shù)從第二個頁面返回數(shù)據(jù)
func didTouched(data: String?) {
print(data!)
}
//通過返回值給第二個頁面?zhèn)鬟f數(shù)據(jù)
func fetchData() -> String {
return "yyyyy"
}
}
頁面二中就用dismiss讓它銷毀
import UIKit
protocol SecondViewControllerDelegate {
func didTouched(data: String?)
func fetchData() -> String
}
class SecondViewController: UIViewController {
var delegate: SecondViewControllerDelegate!
override func viewDidLoad() {
super.viewDidLoad()
if delegate != nil {
let s = delegate.fetchData()
print("第二個頁面: ", s)
}
self.view.backgroundColor = UIColor.redColor()
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if delegate != nil {
delegate.didTouched("xxxx")
}
self.dismissViewControllerAnimated(true, completion: nil)
//用dismiss銷毀
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者