ios - 当点击按钮或文本字段时, View 中的对象会消失

标签 ios swift uiview uibutton uitextfield

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

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
    }
}

最佳答案

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

关于ios - 当点击按钮或文本字段时, View 中的对象会消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38422768/

相关文章:

ios - UISearchController 没有响应

ios - 将 ViewController 的 View 分配给自定义 View 类不会显示自定义 View 属性

swift - 将 [AnyObject] 转换为可能是也可能不是集合类型的泛型

ios - 如何在 UIView subview 中应用自动布局 NIB 实例

ios - 如何从 View 中显示 View Controller ?

ios - 来自 Firebase 的数组不会附加

ios - 如何使标签的文本在图像之上看起来不错?

iphone - 如何在iPhone上放大/放大/缩放UIImageView的特定部分?

android - 如何搜索 HTML 中的内容而不是标签

iphone - 如何在Ipad中自定义SubView为PopuPView?