原文ide
选中 Button ,而后点击 action 右边拖拽到 第二个页面post
选择 “Show”便可完成跳转关联。code
选中第一个页面,点击manual右边拖拽到第二个页面orm
选中 show便可关联两个页面server
点击中间的关联点,修改 Segue Idblog
经过 Segue Id 跳转get
@IBAction func onButtonClick(_ sender: Any) { self.performSegue(withIdentifier: "ToSecondView", sender: nil) }
设置第二个页面的 Storyboard ID
it
经过 Storyboard 的文件名称和页面的 ID获取到ViewController,经过pushViewController跳转。io
@IBAction func onButtonClick(_ sender: Any) { let sb = UIStoryboard(name: "Moments", bundle:nil) let vc = sb.instantiateViewController(withIdentifier: "SecondView") self.navigationController?.pushViewController(vc, animated: true) }
class SecondViewController: UIViewController { var param:String = "" override func viewDidLoad() { super.viewDidLoad() print("param:\(param)") } }
传输参数很是简单,只要覆盖 prepare 方法,在方法中设置参数便可。form
class FirstViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() } override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if(segue.destination is SecondViewController){ let vc = segue.destination as! SecondViewController vc.param = "Wiki" } } }
运行结果:
param:Wiki
class FirstViewController: UIViewController { var param:String = "" override func viewDidAppear(_ animated: Bool) { // 在这里处理返回值 print("返回值:\(param)") } ... }
class SecondViewController: UIViewController { ... @IBAction func onCloseClick(_ sender: Any) { let vc = self.navigationController?.viewControllers[(self.navigationController?.viewControllers.count)! - 2] as! FirstViewController vc.param = "收到啦" self.navigationController?.popViewController(animated: true) } }
结果
返回值:收到啦
定义监听器
override func viewDidLoad() { super.viewDidLoad() let notificationName = Notification.Name("UploadStatus") NotificationCenter.default.addObserver(self, selector: #selector(updateStatus), name: notificationName, object: nil) } @objc func updateStatus(notification: Notification){ if(notification.object != nil){ print("2上传状态:\(notification.object!)") } if(notification.userInfo != nil){ print("2参数:\(notification.userInfo!)") } }
发送通知
let notificationName = Notification.Name("UploadStatus") NotificationCenter.default.post(name: notificationName, object: "上传失败") NotificationCenter.default.post(name: notificationName, object: nil, userInfo: ["param1":"Wiki","param2":18])