ios - 快速关闭 xib View

标签 ios swift

我已经为我的登录条件创建了两个 xib 文件。当我第一次单击一个按钮时,它会显示该文件中的第一个 xib 文件,还有另一个按钮,当我单击它时会在第一个按钮上显示另一个 xib 文件。在这里,我运行我的 API 并成功,我试图关闭第二个 xib 文件,但它并没有关闭第二个 xib。我也标记了断点,当我运行它时,它到达了断点,但它并没有再次消失。这就是我展示我的第一个 xib 文件的方式,

let signingAlert = SigningAlert()
signingAlert.modalPresentationStyle = .custom
present(signingAlert,animated: true,completion: nil)

在这里我调用了我的第二个 xib,

@IBAction func loginBtnTapped(_ sender: Any) {

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

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

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

这就是我正在编写的 API。 ` func loginAPICall(){

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

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

最佳答案

在您的登录 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")

在您的登录 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)
  }
}

关于ios - 快速关闭 xib View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50270291/

相关文章:

ios - 如何为 TableView 标题实现 UIVibrancyEffect?

ios - 获取 native 应用程序 objective-c 中当前播放的非音乐应用程序轨道的信息

ios - Xcode .bundle 和 .dylib

swift - 二元运算符 '>= & <=' 不能应用于 Swift 中 CGFloat 和 Int 类型的操作数

ios - 如何为 UIView 制作基于图像的平铺背景,使其无法缩放?

ios - layoutMarginsGuide 是不是有点过分了?

swift - 执行异步 Cloud Function 的问题

ios - 从 UITableViewController 的 View 模型呈现模态

ios - 使用自动布局 Xcode 居中

ios - 如何向 UITabBarController 添加 9 个或更多选项卡?