在当前视图中显示警报,以及重定向到另一个视图
Posted
技术标签:
【中文标题】在当前视图中显示警报,以及重定向到另一个视图【英文标题】:Display alert in current view, and as well as Redirect to another view 【发布时间】:2017-05-13 07:23:55 【问题描述】:我想显示警报以通过 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)
【问题讨论】:
请输入带有警报的代码,这会产生问题@Niravs 【参考方案1】:要做到这一点,你需要点击alert 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)
欢迎评论。谢谢
【讨论】:
以上是关于在当前视图中显示警报,以及重定向到另一个视图的主要内容,如果未能解决你的问题,请参考以下文章