gpt4 book ai didi

ios - 当应用程序在同一 View iOS Swift 时限制 segue

转载 作者:行者123 更新时间:2023-11-28 10:55:07 24 4
gpt4 key购买 nike

我在我的应用程序中使用推送通知并在点击通知时执行 segue 但问题是如果应用程序在某个 View 上并且通知来自同一 View ,那么 segue 会在同一 View 上执行并且相同的 View 会出现在该 View 上看法。如果 View 相同,我不希望发生这种情况,那么它不应该执行 segue。这是我的代码

 if application.applicationState == UIApplicationState.background{
let moduleName = userInfo.value(forKey: "click_action") as! String
let timestamp = Int64(date.timeIntervalSince1970)
let description = userInfo.value(forKey: "body") as! String
let title = userInfo.value(forKey: "title") as! String

let entity = NSEntityDescription.entity(forEntityName: "Notifications", in: DataBaseController.persistentContainer.viewContext)

let managedObj=NSManagedObject(entity: entity!, insertInto: DataBaseController.persistentContainer.viewContext)

managedObj.setValue(moduleName, forKey: "click")
managedObj.setValue(description, forKey: "body")
managedObj.setValue(timestamp, forKey: "timestamp")
managedObj.setValue(title, forKey: "title")

DataBaseController.saveContext()


if (homeVC != nil){

homeVC?.performSegue(withIdentifier: moduleName, sender: homeVC)

}

}

}

homeView Controller 中的方法

  //MARK:- Navigation Methods
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
let backItem = UIBarButtonItem()
backItem.title = "Home"
navigationItem.backBarButtonItem = backItem // This will show in the next view controller being pushed

if(segue.identifier == "notificationconnew"){
let notObj = segue.destination as! NotificationsTableViewController
notObj.homeVC = self
}


}

override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {

if identifier == segueString && self.navigationController?.viewControllers.last is MyEarningViewController{
return false
}
else{
return true
}
}

App Delegate中homeVc对象的声明

class AppDelegate: UIResponder, UIApplicationDelegate{

var window: UIWindow?
var notification : NSDictionary?
var homeVC : HomeViewController?
lazy var dBhandler = DBHandler()
let date = NSDate()

并在 HomeViewController 的 viewDidLoad 中给出实例

// Giving instance of HomeViewController to AppDelegate
let appDele = UIApplication.shared.delegate as! AppDelegate
appDele.homeVC = self

检查这个 enter image description here

最佳答案

我遇到了这个 SO thread并注意到,当您自己调用 performSegue 时,它永远不会为此调用 shouldPerformSegue。现在只有一个,那就是只有当您当前的 Controller 不是 segue 的目标 Controller 时,您才需要调用 performSegue。所以像这样在 AppDelegate 中进行更改。

if (homeVC != nil && !(homeVC?.navigationController?.viewControllers.last is MyEarningViewController)){
homeVC?.performSegue(withIdentifier: moduleName, sender: homeVC)
}

因此,从您的 HomeViewController 中删除 shouldPerformSegue,现在不再需要它,并通过比较 homeVC 的 navigationStack 调用 performSegue

关于ios - 当应用程序在同一 View iOS Swift 时限制 segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44106682/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com