如何以编程方式编写函数..(对于续集:UIStoryboardSegue,发件人:Any?)
How to write func..(for sequels: UIStoryboardSeque, sender: Any?) programmatically
我在使用故事板时有这些代码:
override func prepare (for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == ProfilePhotoViewController.identifier {
guard let username = usernameTextField.text, let email = emailTextField.text, let password = passwordTextField.text else { return }
let profilePhotoVC = segue.destination as! ProfilePhotoViewController
profilePhotoVC.email = email
profilePhotoVC.username = username
profilePhotoVC.password = password
}
}
如果我不使用故事板并以编程方式编写这些代码以便将信息从当前控制器传递到 ProfilePhotoViewController,我该如何编写这些代码?
编辑:
经过研究,我了解到以编程方式执行此操作是通过委托进行的。但是,我不知道如何完成代码:
需要从 SignUpController 传递用户名、电子邮件和密码 ---> profilePhotoViewController
在 SignUpController 中:
协议 SignUpControllerDelegate {
func handleSignUp(//我应该在这里写什么?)
}
var 委托:SignUpControllerDelegate?
@objc func handleSignUp() {
//...其他代码..//
委托?.handleSignUp(//??)
}
在 ProfileViewController 中:
我应该写什么来接收来自 SignUpController 的用户名、电子邮件和密码信息?
这样试试
guard let username = usernameTextField.text, let email = emailTextField.text, let password = passwordTextField.text else { return }
let profileVC = ProfilePhotoViewController()
profileVC.email = email
我们可以创建一个视图控制器with/without一个NIB文件。
对于 NIB,您使用 init(nibName: String?, bundle: Bundle?)
方法。在这种情况下,view
将使用 Interface Builder 定义。
// bundle = nil denote the main bundle
let viewController = MyViewController(nibName:"MyViewController", bundle: nil)
您也可以在没有 NIB 文件的情况下定义自定义 UIViewController
子类并实现 loadView()
方法。
override func loadView() {
self.view = UIView(...)
}
之后,我们需要将视图添加到视图层次结构中。
self.view.addSubview(viewController.view);
// or
self.present(viewController, animated: false, completion: nil)
// or if we have UINavigationController
self.navigationController?.pushViewController(viewController, animated: false)
我在使用故事板时有这些代码:
override func prepare (for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == ProfilePhotoViewController.identifier {
guard let username = usernameTextField.text, let email = emailTextField.text, let password = passwordTextField.text else { return }
let profilePhotoVC = segue.destination as! ProfilePhotoViewController
profilePhotoVC.email = email
profilePhotoVC.username = username
profilePhotoVC.password = password
}
}
如果我不使用故事板并以编程方式编写这些代码以便将信息从当前控制器传递到 ProfilePhotoViewController,我该如何编写这些代码?
编辑: 经过研究,我了解到以编程方式执行此操作是通过委托进行的。但是,我不知道如何完成代码:
需要从 SignUpController 传递用户名、电子邮件和密码 ---> profilePhotoViewController
在 SignUpController 中:
协议 SignUpControllerDelegate { func handleSignUp(//我应该在这里写什么?) }
var 委托:SignUpControllerDelegate?
@objc func handleSignUp() {
//...其他代码..//
委托?.handleSignUp(//??) }
在 ProfileViewController 中:
我应该写什么来接收来自 SignUpController 的用户名、电子邮件和密码信息?
这样试试
guard let username = usernameTextField.text, let email = emailTextField.text, let password = passwordTextField.text else { return }
let profileVC = ProfilePhotoViewController()
profileVC.email = email
我们可以创建一个视图控制器with/without一个NIB文件。
对于 NIB,您使用 init(nibName: String?, bundle: Bundle?)
方法。在这种情况下,view
将使用 Interface Builder 定义。
// bundle = nil denote the main bundle
let viewController = MyViewController(nibName:"MyViewController", bundle: nil)
您也可以在没有 NIB 文件的情况下定义自定义 UIViewController
子类并实现 loadView()
方法。
override func loadView() {
self.view = UIView(...)
}
之后,我们需要将视图添加到视图层次结构中。
self.view.addSubview(viewController.view);
// or
self.present(viewController, animated: false, completion: nil)
// or if we have UINavigationController
self.navigationController?.pushViewController(viewController, animated: false)