【问题标题】:Objects in view dissapear when tap on button or text fields点击按钮或文本字段时,视图中的对象消失
【发布时间】:2016-11-20 05:42:05
【问题描述】:

我刚刚更新到 Xcode 7.3。每当我点击按钮或文本字段时,视图中的所有对象都会消失。我不知道为什么会发生这种情况或导致它的原因。 顺便说一句:我知道当我按下按钮时代码运行,因为我看到了弹出的警报。 此外,当我点击“没有帐户”按钮时,视图不会消失。

The view

The view when I tap on the login button or either text field

这是我的代码:

import UIKit
import Firebase
import Presentr
import SWMessages

class LoginViewController: UIViewController {

    @IBOutlet weak var emailField: UITextField!
    @IBOutlet weak var passwordField: HideShowPasswordTextField!
    @IBOutlet weak var loginButton: UIButton!


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

    }

    override func viewWillAppear(animated: Bool) {

       if ((FIRAuth.auth()?.currentUser) != nil) {
       } else {
         self.performSegueWithIdentifier("loginSegue", sender: nil)
       }
    }

    @IBAction func didPressLogin(sender: AnyObject) {
        func show() {}
        loginButton.selected = true
        if emailField.text == "" {
            delay(2, closure: { () -> () in
                func showFail() {}
                self.loginButton.selected = false
                let title = "Oops!"
                let body = "You didn't enter an email!"
                let controller = Presentr.alertViewController(title: title, body: body)
                let tryAgainAction = AlertAction(title: "Try again", style: .Cancel){}
                controller.addAction(tryAgainAction)
                let presenter = Presentr(presentationType: .Alert)
                self.customPresentViewController(presenter, viewController: controller, animated: true, completion: nil) })
        } else if passwordField.text == "" {
            delay(2, closure: { () -> () in
                func showFail() {}
                self.loginButton.selected = false
                let title = "Oops!"
                let body = "You didn't enter a password!"
                let controller = Presentr.alertViewController(title: title, body: body)
                let tryAgainAction = AlertAction(title: "Try again", style: .Cancel){}
                controller.addAction(tryAgainAction)
                let presenter = Presentr(presentationType: .Alert)
                self.customPresentViewController(presenter, viewController: controller, animated: true, completion: nil) })
        } else {
            FIRAuth.auth()?.signInWithEmail(emailField.text!, password: passwordField.text!, completion: { (user, error) -> Void in
                if error == nil {
                    func showSuccess() {}
                    self.loginButton.selected = false
                    self.performSegueWithIdentifier("loginSegue", sender: nil)
                } else  {
                    func showFail() {}
                    self.loginButton.selected = false
                    SWMessage.sharedInstance.showNotificationInViewController ( self, title: "Oops!", subtitle: error?.localizedDescription, image: nil, type: .Error, duration: .Automatic, callback: nil, buttonTitle: "Try again", buttonCallback: {}, atPosition: .Top, canBeDismissedByUser: true )
                }
            })
        }
    }
    @IBAction func noAccount(sender: UIButton) {
        // TODO: Decide whether to allow users to create an account or not.

        UIApplication.sharedApplication().openURL(NSURL(string: "http://kalissaac.github.io/SmaLert")!)
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

// MARK: UITextFieldDelegate
extension LoginViewController: UITextFieldDelegate {
    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, textField string: String) -> Bool {
        return passwordField.textField(textField, shouldChangeCharactersInRange: range, replacementString: string)
    }

    func textFieldDidEndEditing(textField: UITextField) {
        passwordField.textFieldDidEndEditing(textField)
    }

    func textFieldShouldReturn(textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        if textField == emailField { // Switch focus to other text field
            passwordField.becomeFirstResponder()
        }
        return true
    }
}

// MARK: HideShowPasswordTextFieldDelegate
// Implementing this delegate is entirely optional.
// It's useful when you want to show the user that their password is valid.
extension LoginViewController: HideShowPasswordTextFieldDelegate {
    func isValidPassword(password: String) -> Bool {
        return password.characters.count > 6
    }
}

【问题讨论】:

  • 尝试在viewWillApear(animated: Bool) 中调用super。能否也显示customPresentViewController 方法的代码?
  • @binchik 打电话给 super 没有帮助。 customPresentViewController 的代码已经存在。 (它是 Presentr 警报控制器)

标签: ios swift uiview uibutton uitextfield


【解决方案1】:

我通过删除自定义类并重新添加它来解决这个问题。这一定是个bug。感谢@binchik 的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-20
    • 2012-12-29
    • 1970-01-01
    • 2014-08-20
    • 1970-01-01
    • 2015-10-27
    相关资源
    最近更新 更多