ios - 推送到另一个 ViewController

标签 ios swift xcode

我在推送到另一个 View Controller 时遇到问题。我创建了一个简单的登录系统,但问题是即使密码错误,它也会将我推送到默认 View Controller 。

import UIKit
import Parse

class ViewController: UIViewController {

@IBOutlet weak var usernameTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!

func createAlert(title: String, message: String) {

    let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)

    alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (alert) in

    }))
    self.present(alert, animated: true, completion: nil)

}

@IBAction func loginButton(_ sender: Any) {

    // ************ Login Page ************

    // Username error checking

    if usernameTextField.text == "" {

        let usernameAlert = UIAlertController(title: "Username is missing", message: "Please enter your username", preferredStyle: UIAlertControllerStyle.alert)

        usernameAlert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in
            }))
        self.present(usernameAlert, animated: true, completion: nil)

        // Password error checking

    } else {

        if passwordTextField.text == "" {

            let passwordAlert = UIAlertController(title: "Passwors is missing", message: "Please enter your password", preferredStyle: UIAlertControllerStyle.alert)

            passwordAlert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in
                self.dismiss(animated: true, completion: nil)
            }))
            self.present(passwordAlert, animated: true, completion: nil)            }

        else {

        PFUser.logInWithUsername(inBackground: usernameTextField.text!, password: passwordTextField.text!, block: { (user, error) in

            if error != nil {



                var displayErrorMessage = "Please try again later."

                if let errorMessage = error as NSError? {

                    displayErrorMessage = errorMessage.userInfo["error"] as! String

                }


                self.createAlert(title: "Login Error", message: displayErrorMessage)
            } else {

                print ("Logged in")

                let storyBoard = UIStoryboard(name: "Main", bundle: nil)

                let cameraVC = storyBoard.instantiateViewController(withIdentifier: "CameraViewController") as! CameraViewController

                self.navigationController?.pushViewController(cameraVC, animated: true)

            }
            })
        }
    }

}
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.



        }

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}

注意:当用户名和密码有效时,我在调试区域收到“已登录”消息。

最佳答案

您是否在直接按钮中给出了 segue 连接,例如

enter image description here

so it wont check the condition if you click the button it directly segue to another VC.

如果使用的是storyboard ID则去掉VC之间的连接,可以直接访问

enter image description here

 let storyBoard = UIStoryboard(name: "Main", bundle: nil)

            let cameraVC = storyBoard.instantiateViewController(withIdentifier: "CameraViewController") as! CameraViewController

            self.navigationController?.pushViewController(cameraVC, animated: true)

其他

connection oriented segue using direclty class

enter image description here

 [self performSegueWithIdentifier:@"identifierName" sender:self];

调用类似

else {

            print ("Logged in")
            performSegue(withIdentifier: "identifierName", sender: self)

            }

关于ios - 推送到另一个 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43014070/

相关文章:

iphone - iPhone 中询问用户是否允许使用定位服务的对话框弹出并消失

预测语言偏好的 Android/iOS API/机制

ios - 如何从 UITableView 编辑和保存 TextField 数据

ios - XCODE - 链接器命令失败,退出代码为 1 - 自 xcode 更新以来

string - 如何在 Swift 中随机化字符串中字母的大小写?

ios - Swift - 桥接到 objective-c 时出现 26 个错误

ios - 使用 Xcode 手动配置单元测试

ios - 升级 Xcode6 后尝试在 iPhone 模拟器中进行调试时,Xcode 有时会崩溃

ios - 如何使用 Flutter/Dart 打开 Apple map ?

ios - Xcode 6 + iOS 8 SDK 但部署在 iOS 7 (UIWebKit & WKWebKit)