按钮只能工作一次

时间:2018-05-05 15:32:12

标签: ios swift

我有一个页面,其中包含两个文本字段,2个标签和一个按钮。单击按钮时,我只是检查字段是否为空并验证它们。这很好用。但我只能点击一次按钮。之后,什么都不会改变,我无法按下按钮或编辑文本字段!

这是我的代码:

@IBAction func loginbtn(_ sender: Any) {
    showActivityIndicator(uiView: view)

    emailerrormsg.isHidden=true
    passworderrormsg.isHidden=true

    if emailtf.text == "" && passwordtf.text == "" {
        emailerrormsg.text = "Email is required!"
        emailerrormsg.isHidden = false
        passworderrormsg.text = "Password is required!"
        passworderrormsg.isHidden = false
        stopActivityIndicator()
    }
    else if emailtf.text == ""{
        emailerrormsg.text = "Email is required!"
        emailerrormsg.isHidden = false
        stopActivityIndicator()

    }
    else if !(isValidEmail(testStr: emailtf.text!)){
        emailerrormsg.text = "Please enter a valid email!"
        emailerrormsg.isHidden = false
        stopActivityIndicator()
    }else if passwordtf.text == "" {
        passworderrormsg.text = "Password is required!"
        passworderrormsg.isHidden = false
        stopActivityIndicator()
    }

    else{
        hashedpass = (passwordtf.text!).sha1()
        self.login(email: emailtf.text!, password: hashedpass)
    }
}

我做错了什么?

    func stopActivityIndicator(){
    DispatchQueue.main.async {
        self.loadingView.isHidden = true
    }
}
func showActivityIndicator(uiView: UIView) {
    let container: UIView = UIView()
    container.frame = uiView.frame
    container.center = uiView.center

    loadingView.frame = CGRect(x: 0,y:  0,width: 80,height: 80)
    loadingView.center = uiView.center
    loadingView.backgroundColor = UIColor(red:0.16, green:0.27, blue:0.60, alpha:1.0)
    loadingView.clipsToBounds = true
    loadingView.layer.cornerRadius = 10

    let actInd: UIActivityIndicatorView = UIActivityIndicatorView()
    actInd.frame = CGRect(x: 0.0,y: 0.0,width: 40.0,height: 40.0);
    actInd.activityIndicatorViewStyle =
        UIActivityIndicatorViewStyle.whiteLarge
    actInd.center = CGPoint(x: loadingView.frame.size.width / 2,y: loadingView.frame.size.height / 2);
    loadingView.addSubview(actInd)
    container.addSubview(loadingView)
    uiView.addSubview(container)
    actInd.startAnimating()
}

1 个答案:

答案 0 :(得分:0)

您正在隐藏加载视图中的stop方法。而是隐藏容器视图。为此,您需要使容器视图成为类级别对象(就像您已加载视图一样)