在当前视图中显示警报,以及重定向到另一个视图

时间:2017-05-13 07:23:55

标签: ios swift swift3 uialertview

我想显示警告,以便从API检查我的应用的新版本。从那个视图来看,如果userDefault数据是存储的,那么我想重定向到另一个视图。我的重定向代码工作正常,但是当我添加警报代码时,重定向不起作用。我认为警告出现在“自我”中,那时我也会尝试从该视图重定向,因此可能会产生问题。这是我的代码..

    override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
            updateUserData() //base on userDefault redirection 
        }
    override func viewDidAppear(_ animated: Bool) {
            super.viewDidAppear(animated)
            checkNewVersion() //Alert function for API calling
        }

func checkNewVersion() -> Void {

    //TODO: For check App new version
       WebRequester.shared.getAppNewVersion { (result, error) in
        if result != nil {
            if result?.value(forKey: "status") as! Bool {
                let strVer = (result?.object(forKey: "data") as! NSDictionary).object(forKey: "platform_version") as! String
                if UserData.getAppVersion() < strVer {
                    let alert = UIAlertController(title: "Alert", message: "New version of App available", preferredStyle: .alert)
                    let ok = UIAlertAction(title: "Ok", style: .default, handler: { (action) in
                    })
                    let AppStore = UIAlertAction(title: "App Store", style: .default, handler: { (action) in
                        if let url = URL(string: "itms-apps://itunes.apple.com/app/id1024941703"),
                            UIApplication.shared.canOpenURL(url){
                            UIApplication.shared.openURL(url)
                        }
                    })
                    alert.addAction(ok)
                    alert.addAction(AppStore)
                    self.present(alert, animated: true, completion: nil)
//                    OperationQueue().addOperation {
//                        // Put queue to the main thread which will update the UI
//                        OperationQueue.main.addOperation({
//                           self.present(alert, animated: true, completion: nil)
//                        })
//                    }

                }
            }
            else {
                if (result?.object(forKey: "data") as! NSArray).object(at: 0) as? String ?? "" == "Unauthorised access." {
                    Model.shared.deleteAllCoreDataRecord(entity: "CartItem")
                    UIApplication.topViewController()?.navigationController?.popToRootViewController(animated: true)
                }
                let msg = result?.value(forKey: "data") as! [String]
                Model.shared.showAlert(title: "Error", msg: msg[0], controller: self)
            }
        }
        else {
            Model.shared.showAlert(title: "Error", msg: error?.localizedDescription ?? "Something went wrong at add new address", controller: self)
        }
    }
}

func updateUserData() -> Void {
    if UserData.getAppVersion() == "1.0" {

        if UserData.getUserData() != nil  {

            //TODO: check for user Updated data
            let params = ["mobile":UserData.getUserMobile()] as [String : Any]
            let propic = UIImage(named: "temp")
            weak var objWeek = self

            Model.shared.showActivity(WithTouchEnable: false,controller: self)
            WebRequester.shared.customerSignup(params: params as NSDictionary, proImg: propic!){ (result,error) -> Void in
                Model.shared.HideActivity(controller: self)
                if (error == nil) {
                    print("login result:",result ?? "")
                    //handle response of sign up
                    let statusstr = result?["status"] as! Bool
                    if (statusstr == false) {
                        //This condition for pepsi welcome offer is expire or not
                        let user = result!["user_info"] as! NSDictionary
                        self.storeUserData(user: user)

                        if UserData.getUserMobileNumVerify() == "No" {
                            let storyboard = UIStoryboard(name: "Main", bundle: nil)
                            let registerScreen = storyboard.instantiateViewController(withIdentifier: "UserRegisterPhoneVC") as! UserRegisterPhoneVC

                            objWeek?.navigationController?.pushViewController(registerScreen, animated: true)
                        }
                        else {
                            if UserData.getPepsiOfferRedim() == "1" || UserData.getPepsiOfferRedim() == "2" {
                                let storyboard = UIStoryboard(name: "Main", bundle: nil)
                                let offerScreen = storyboard.instantiateViewController(withIdentifier: "OfferViewController") as! OfferViewController
                                objWeek?.navigationController?.pushViewController(offerScreen, animated: true)
                            }
                            else {
                                let storyboard = UIStoryboard(name: "Main", bundle: nil)
                                let promoScreen = storyboard.instantiateViewController(withIdentifier: "PromotionViewController") as! PromotionViewController
                                objWeek?.navigationController?.pushViewController(promoScreen, animated: true)
                            }
                        }
                    }
                }
            }
            else {
                Model.shared.showAlert(title: "Error", msg: (error?.localizedDescription)!, controller: self)
            }
        }

    }

}
}

1 个答案:

答案 0 :(得分:0)

要实现这一点,您需要单击警报OK按钮,然后它才会自动导航到其他控制器,但这不可能。

这是代码:

警报控制器块可帮助您实现此目的:

 //show an alert and navigate to previous controller
            let alertController: UIAlertController = UIAlertController(title: "Password updatd", message: "your alert message", preferredStyle: .alert)

            let okAction: UIAlertAction = UIAlertAction(title: "OK", style: .default) { action -> Void in

           //Redirect to new viewcontroler 
           let newVC = self.storyboard.instantiateViewcontroller(identifier: "newvc") as? NewVC
           self.navigationController?.pushViewController(newVC,animated: true)
            }

            alertController.addAction(okAction)
            self.present(alertController, animated: true, completion: nil)

随意发表评论。谢谢