Swift代理方法反向傳值
阿新 • • 發佈:2018-12-23
// AppDelegate.swift // ReverseSendValue import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { let first=FirstViewController() first.view.backgroundColor=UIColor.whiteColor() let navi=UINavigationController(rootViewController: first) window?.rootViewController=navi return true } }
// FirstViewController.swift // ReverseSendValue import UIKit //螢幕尺寸 let kWidth=UIScreen.mainScreen().bounds.size.width let kHeight=UIScreen.mainScreen().bounds.size.height //遵守SecondViewControllerDelegate協議 class FirstViewController: UIViewController,SecondViewControllerDelegate { override func viewDidLoad() { super.viewDidLoad() //搭建檢視 createUI() } //搭建檢視 func createUI(){ let frame=CGRectMake(UIScreen.mainScreen().bounds.size.width/2-120, 200, 240, 50) let resultLb=UILabel(frame: frame) resultLb.text="[email protected]" resultLb.textAlignment=NSTextAlignment.Center self.view.addSubview(resultLb) resultLb.tag=100 let btn=UIButton() btn.frame=CGRectMake(kWidth/2-40, CGRectGetMaxY(resultLb.frame)+20, 100, 40) btn.setTitle("修 改", forState: .Normal) btn.backgroundColor=UIColor.blueColor() btn.addTarget(self, action: Selector("gotoChangeValue"), forControlEvents: .TouchUpInside) self.view.addSubview(btn) } //跳轉到修改的檢視 func gotoChangeValue(){ let second=SecondViewController() let lb=self.view.viewWithTag(100) as! UILabel second.text=lb.text //設定代理 second.delegate=self self.navigationController?.pushViewController(second, animated: true) } //代理方法 func sendValue(value:String?){ let lb=self.view.viewWithTag(100) as! UILabel lb.text=value } }
// SecondViewController.swift // ReverseSendValue import UIKit //協議 protocol SecondViewControllerDelegate{ //協議方法 func sendValue(value:String?) } class SecondViewController: UIViewController { var text:String? //代理屬性 var delegate:SecondViewControllerDelegate? override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor=UIColor.whiteColor() //搭建介面 createUI() } //搭建介面 func createUI(){ let textField=UITextField() textField.frame=CGRectMake(kWidth/2-120, 200, 240, 50) textField.text=text textField.borderStyle=UITextBorderStyle.RoundedRect textField.keyboardType=UIKeyboardType.EmailAddress textField.textAlignment=NSTextAlignment.Center self.view.addSubview(textField) textField.tag=101 let btn=UIButton() btn.frame=CGRectMake(UIScreen.mainScreen().bounds.size.width/2-40, CGRectGetMaxY(textField.frame)+20, 100, 40) btn.setTitle("修改完成", forState: .Normal) btn.backgroundColor=UIColor.orangeColor() btn.addTarget(self, action: Selector("doneAction"), forControlEvents: .TouchUpInside) self.view.addSubview(btn) } //完成修改 func doneAction(){ let tf=self.view.viewWithTag(101) as! UITextField if((delegate) != nil) { delegate?.sendValue(tf.text) self.navigationController?.popViewControllerAnimated(true) } } //隱藏鍵盤 override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { self.view.endEditing(true) } }