利用delegate實(shí)現(xiàn)兩個(gè)Controller之間的傳值
RootViewController中用個(gè)lable和一個(gè)按鈕内贮,點(diǎn)擊按鈕跳轉(zhuǎn)到模態(tài)窗口产园。在模態(tài)窗口中有個(gè)TextField和一個(gè)按鈕,輸入文字點(diǎn)擊關(guān)閉模態(tài)按鈕后跳轉(zhuǎn)到RootViewController夜郁,并改變其label為輸入的值什燕。
- AppDelegate.swift
// AppDelegate.swift
import UIKit
@UIApplicationMain
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?.backgroundColor = UIColor.darkGrayColor()
self.window?.makeKeyAndVisible()
let rootVc = RootViewController()
self.window?.rootViewController = rootVc
return true
}
- RootViewController.swift
import UIKit
//實(shí)現(xiàn)ModeViewControlDelegate協(xié)議
class RootViewController: UIViewController,MdoeViewControlDelegate{
var btn:UIButton?
var label:UILabel?
//實(shí)現(xiàn)協(xié)議中的方法
func changeLabel(newString:String) {
self.label!.text = newString
}
func btnOnClick(){
print("Onclick")
let modeView = ModeViewController()
//設(shè)置modeView中的代理為RootViewController自身
modeView.delegate=self
//跳轉(zhuǎn)到ModelView
self.presentViewController(modeView,
animated: true ,
completion: {
print("OK")
})
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.grayColor()
label = UILabel()
label!.frame = CGRectMake(110, 40, 100, 20)
label!.backgroundColor = UIColor.greenColor()
label!.text = "你好!"
label!.textAlignment = .Center
btn = UIButton()
btn!.frame = CGRectMake(110, 80, 100, 20)
btn!.backgroundColor = UIColor.greenColor()
btn!.setTitle("下一視圖", forState: .Normal)
btn!.addTarget(self, action:#selector(RootViewController.btnOnClick), forControlEvents: .TouchUpInside)
self.view.addSubview(label!)
self.view.addSubview(btn!)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
- ModeViewControler.swift
import UIKit
class ModeViewController: UIViewController {
var textF:UITextField?
var delegate:MdoeViewControlDelegate?
//點(diǎn)擊事件
func btnOnClick(){
let str = textF?.text
print(str)
// 調(diào)用代理函數(shù) 改變label值
self.delegate?.changeLabel(str!)
//返回RootView
self.dismissViewControllerAnimated(true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor=UIColor.cyanColor()
textF=UITextField()
textF!.frame=CGRectMake(110,40,100,20)
textF!.backgroundColor=UIColor.greenColor()
textF!.borderStyle = .RoundedRect
let btn=UIButton(frame:CGRectMake(110,80,100,20))
btn.backgroundColor=UIColor.greenColor()
btn.setTitle("返回上視圖",forState:.Normal)
//綁定事件
btn.addTarget(self,action:#selector(ModeViewController.btnOnClick),forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(btn)
self.view.addSubview(textF!)
// Do any additional setup after loading the view.
}
}
- Protocol.swift
import Foundation
//協(xié)議定義要實(shí)現(xiàn)的方法
protocol MdoeViewControlDelegate {
func changeLabel(neString:String)
}