界面回调

ViewController前往MyViewController:


在MyViewController中定义CustomBlock,并在输入1时触发回调:


import UIKit


class MyViewController: UIViewController ,UITextViewDelegate{

var CustomBlock:((_ originString:String,_ curInput:String)->())?

var myInput:UITextView?


override func viewDidLoad() {


super.viewDidLoad()

view.backgroundColor = .white

myInput = UITextView.init(frame: CGRect(x: 20, y: 40, width: 200, height: 200))

myInput?.backgroundColor = .lightGray

myInput?.delegate = self

view.addSubview(myInput!)

}


func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {


if text == "1" {

if CustomBlock != nil {

CustomBlock!(textView.text,text)

}


self.dismiss(animated: true, completion: nil)

}


return true

}


}

在ViewController跳转时实现此Block:


func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {


let vc:MyViewController = .init()

vc.CustomBlock = {originString,curInputString in

print(String(format: "总字符串:%@,当前输入字符串为:%@", originString,curInputString))

}


self.present(vc, animated: true, completion: nil)

}


加油~