回到我的第一个快速应用程序,我正在进行登录/注册部分,到目前为止,基本上使用视图控制器的设计是:
欢迎屏幕(登录/注册按钮) - 登录 - 注册 - 主程序
我到达了注册视图控制器中的部分,如果用户错过了一个必需的字段空白并且他们单击按钮"提交",它们将被提示并返回到当前视图控制器(注册)来填补缺少的字段。现在我想设置当所有字段都填满时,我可以使用哪行代码,这样当他们点击按钮时#34;提交"它将返回欢迎屏幕,以便他们可以在之后登录?
同样,我得到了所有部分来检查用户是否输入了正确的用户名和密码,因此当他们输入错误的信息时,他们会被提示并入侵当前的视图控制器(登录),以及如何我这样做,如果他们输入了正确的用户名 - 密码,它将前往主程序视图控制器(第4个)
如果我上面说的很混乱,我会在这里发布我当前的控制器和相关代码。任何人有任何想法?再说一次,这是我的第一个程序,所以如果我能从你那里得到一些帮助,这意味着非常重要。谢谢你的阅读。
答案 0 :(得分:2)
关于如何做的一个例子:
import UIKit
class WelcomeVC: UIViewController {
//make it IBAction or call it from IBAction
func singInButtonPressed(){
performSegueWithIdentifier("sign in", sender: self)
}
//make it IBAction or call it from IBAction
func singUPButtonPressed(){
performSegueWithIdentifier("sign up", sender: self)
}
}
class SignUPVC: UIViewController {
@IBOutlet var userNameField:UITextField!
@IBOutlet var passwordField:UITextField!
//make it IBAction or call it from IBAction
func submit(){
if checkValidity() {
presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
}
}
func checkValidity() ->Bool{
if !userNameField.text!.isEmpty{
if !passwordField.text!.isEmpty {
return true
} else {
//do something to inform passwordfield is missing
}
} else{
//username field is missing
}
return false
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "sign in" {
if let sINvc = segue.destinationViewController.contentViewController as? SignInVC{
sINvc.username = userNameField.text
sINvc.password = passwordField.text
//and pass more of the other info you gathered in this vc
}
}
}
}
class SignInVC: UIViewController {
@IBOutlet var userNameField:UITextField!{
didSet {
if username != nil { userNameField.text = username }
}
}
@IBOutlet var passwordField:UITextField! {
didSet {
if password != nil { passwordField.text = password }
}
}
var password:String?
var username:String?
//make it IBAction or call it from IBAction
func signInPressed(){
if !userNameField.text!.isEmpty && !passwordField.text!.isEmpty {
performSegueWithIdentifier("Main page", sender: self)
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "Main page"{
//your preparation
}
}
}
//this extension is helpful to avoid typing this everytime there is nav vc in your way of segueing
extension UIViewController{
var contentViewController: UIViewController {
if self is UINavigationController{
if let cvc = (self as! UINavigationController).visibleViewController { return cvc }
}
return self
}
}
答案 1 :(得分:1)
simple way to transfer one view to another in swift is this given below:-
let storyboard = UIStoryboard(name: "Main", bundle: nil)
//in "Main" your storyboard name
let secondViewController = storyboard.instantiateViewControllerWithIdentifier("LoginPage") as! UIViewController
//in place of Login Page your storyboard identifier name
navigationController?.pushViewController(secondViewController, animated: true)
答案 2 :(得分:0)
{{1}}
适合我。谢谢!更新swift 3.1和Xcode 8.3.2