在swift中关闭xib视图

时间:2018-05-10 10:00:43

标签: ios swift

我为登录条件创建了两个xib文件。当我第一次单击一个按钮时,它会显示该文件中的第一个xib文件,还有另一个按钮,当我单击它时,在第一个按钮上显示另一个xib文件。在这里我运行我的API,并且在成功的时候,我试图解雇第二个xib文件,但它并没有解雇第二个xib。我也标记了断点,当我运行它时,它会进入断点,但它不会再次被解雇。这是我展示我的第一个xib文件的方式,

@IBAction func loginBtnTapped(_ sender: Any) {

    let loginAlert = LoginVC()
    loginAlert.modalPresentationStyle = .popover
    present(loginAlert,animated: true,completion: nil)
    // self.revealViewController().revealToggle(animated: true)
}

在这里我打电话给我的第二个xib,

self.dismiss(animated: true, completion: nil)

在我的API成功条件下,我编写了这段代码来解雇xib,

    activityIndicator.startAnimating()
    activityIndicator.isHidden = false
    shadowView.isHidden = false

    let param1 =  "username="+userNameTxt.text!
    let param2 =  param1+"&password="+passwordTxt.text!
    let param  =  param2+"&grant_type=password"

    print(param)
    LoginService.instance.LogInUSer(body: param) { (success) in
        if success{

            let status = LoginService.instance.status

            if status == 500{
                print("500")
                self.showAlert(message: "Error!.Server not found")
                self.activityIndicator.stopAnimating()
                self.activityIndicator.isHidden = true
                self.shadowView.isHidden = true
            }
            else if status == 404{
                print("404")
                self.showAlert(message: "Error!. Data not found try again")
                self.activityIndicator.stopAnimating()
                self.activityIndicator.isHidden = true
                self.shadowView.isHidden = true
            }
            else if status == 400{
                 self.showAlert(message: "Error!. Username or password is incorrect.")
                self.activityIndicator.stopAnimating()
                self.activityIndicator.isHidden = true
                self.shadowView.isHidden = true
            }
            else if status == 401{
                print("401")
                self.activityIndicator.stopAnimating()
                self.activityIndicator.isHidden = true
                self.shadowView.isHidden = true
            }
            else if status == 200{
                self.showsuccessAlert(message: "You are successfully LoggedIn")
                self.dismiss(animated: true, completion: nil)
                self.activityIndicator.stopAnimating()
                self.activityIndicator.isHidden = true
                self.shadowView.isHidden = true

                let userName = LoginService.instance.loginModelInstance[0].userName
                let accessToken = LoginService.instance.loginModelInstance[0].accessToken
                let userId = LoginService.instance.loginModelInstance[0].userID
                UserDefaults.standard.setValue("true", forKey: "status")
                UserDefaults.standard.set(userName, forKey:"name")
                UserDefaults.standard.set(userId, forKey:"userId")
                UserDefaults.standard.set(accessToken, forKey:"accessToken")
                UserDefaults.standard.synchronize()
            }
            else{
                self.showAlert(message: "Error!. Try again")
                self.activityIndicator.stopAnimating()
                self.activityIndicator.isHidden = true
                self.shadowView.isHidden = true
            }
        }else{
            let status = LoginService.instance.status
            print(status)
             self.showAlert(message: "Error!. Try again")
            self.activityIndicator.stopAnimating()
            self.activityIndicator.isHidden = true
            self.shadowView.isHidden = true
        }
    }
}`

这就是我编写API的原因。 `func loginAPICall(){

void interrupt int_tmr1(void) {
if((PIE1&(1<<0))&&(PIR1&(1<<0))) //TMR1 OVERFLOW CONDITION 
{
         GPIO^=(1<<0); //TOGGLE LED 
       PIR1&=~(1<<0); //CLEAR TMR1 INTERRUPT OVER FLOW FLAG
}

但它并没有解雇我的xib,我该怎么解雇呢?

3 个答案:

答案 0 :(得分:2)

在您的登录VC中

protocol LoginDelegate: class {
   func didDismiss()
} 

class LoginVC: UIViewController {
  weak var delegate: LoginDelegate?
  var successAlert: UIAlertController?

替换

if status == 200{
   self.showsuccessAlert(message: "You are successfully LoggedIn")
   self.dismiss(animated: true, completion: nil)

if status == 200 {
  self.onsuccess(message: "You are successfully LoggedIn")

在Login VC中添加以下函数onsuccess

func onsuccess(title: String = "Success!", message: String) {
  successAlert = UIAlertController(title: title, message: message, preferredStyle: .alert) 
  successAlert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: { [weak self] _ -> Void in
       self?.delegate?.didDismiss()
       self?.dismiss(animated: true, completion: nil)
   }))
   present(successAlert, animated: true, completion: nil)

 }

在您的签名VC中

@IBAction func loginBtnTapped(_ sender: Any) {

        let loginAlert = LoginVC()
        // Add delegate to self
        loginAlert.delegate = self
        loginAlert.modalPresentationStyle = .popover
        present(loginAlert,animated: true,completion: nil)
        // self.revealViewController().revealToggle(animated: true)
 }

并添加

extension SigningVC: LoginDelegate {
  func didDismiss() {
    self.dismiss(animated: true, completion: nil)
  }
}

答案 1 :(得分:0)

你只能从主线程中解除屏幕。如果您从异步或非主线程尝试它,那么它将无法工作。

替换

  self.dismiss(animated: true, completion: nil)                

使用

 DispatchQueue.main.async {
      self.dismiss(animated: true, completion: nil)                
 }

答案 2 :(得分:0)

据我了解,你的问题是

HomeClass -> present SigningAlert class -> present LoginVC class

如果在出现第二个xib之前未解除第一个Xib,则会出现问题。

解决方案:  在IBAction func loginBtnTapped(_ sender: Any) {中,您可以调用委托或类似的内容来通知已知的HomeClass。在HomeClass中,你应该看起来像下面的

dismiss(animated: true) {
            let loginAlert = LoginVC()
            loginAlert.modalPresentationStyle = .popover
             present(loginAlert,animated: true,completion: nil)
        }

更新: 在你的alertVC中 @IBAction func loginBtnTapped(_ sender:Any){

let loginAlert = LoginVC()
loginAlert.modalPresentationStyle = .popover

 // Note: post notification
 NotificationCenter.default.post(name: NSNotification.Name(rawValue: "shouldShowLoginVC"), object: nil, userInfo: nil)

// present(loginAlert,animated: true,completion: nil)
// self.revealViewController().revealToggle(animated: true)

}

在你的HomeClass中,你应该实现obsever听取通知:

NotificationCenter.default.addObserver(self, selector: #selector(handleNotif(_:)), name: NSNotification.Name(rawValue: "shouldShowLoginVC"), object: nil)

并添加处理功能     @objc func handleNotif(_ sender:Any){

    // Dismiss first xib
    dismiss(animated: true) {

        // after finish dismiss first, present second
        let loginAlert = LoginVC()
        loginAlert.modalPresentationStyle = .popover
        present(loginAlert,animated: true,completion: nil)
    }
}