2、弹出框: import UIKit class ViewController:UIViewController { var label:UILabel! override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = UIColor.brown label = UILabel(frame:CGRect(x:40, y:100,width:240, height:44)) label.text = ”” self.view.addSubview(label) let button = UIButton(frame:CGRect(x:40, y:180,width:240, height:44)) button.setTitle(“打开新的视图控制器”, for:UIControlState()) button.backgroundColor = UIColor.black button.addTarget(self, action:#selector(ViewController.openViewController),fo:.touchUpInside) self.view.addSubview(button) } func openViewController() { let newViewController = NewViewController() newViewController.labelTxt = “传递的参数!” newViewController.viewController = self self.present(newViewController, animated:true,completion:nil) } }
3 弹出框,传递参数 import UIKit
class NewViewController:UIViewController {
var viewController:ViewController? var labelTxt = ”” override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = UIColor.purple let label = UILabel(frame:CGRect(x:40, y:100,width:240, height:44)) label.text = labelTxt self.view.addSubview(label)
let button = UIButton(frame:CGRect(x:40, y:180,width:240, height:44)) button.setTitle(“关闭”, for:UIControlState()) button.backgroundColor = UIColor.black button.addTarget(self, action:#selector(NewViewController.dismissSelf),for:.touchUpInside) self.view.addSubview(button) }
func dismissSelf() { viewController?.label.text = “返回的参数。” self.dismiss(animated:true, completion:nil) } }