我想确保用户在导航到另一个控制器之前从表列控制器中显示的行(列表中的行)列表中选择一行。所以在didSelectRow方法中,我设置了一个布尔变量scheduleSelected为true。在我的viewWillDisappear中,我检查了scheduleSelected并且它是false,然后我发出警报并重新加载tableview,所以我停留在相同的tableview而不是导航。它不工作,它反正导航到另一个控制器,但确实提出了一个太晚的警报。在导航前强制用户在tableview中选择一行
如何强制用户在导航出当前tableview控制器之前选择一行?
可能有更简单的方法,而不是这个繁琐的过程。请告诉我。
override func viewWillDisappear(_ animated: Bool) {
if (scheduleSelected == false) {
let alertController = UIAlertController(title: "UIAlertController", message: "Select Row", preferredStyle: .alert)
alertController.message = "Choose a Schedule"
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
present(alertController, animated: true, completion: nil)
self.tableView.reloadData()
}else {
let viewController = storyboard?.instantiateViewController(withIdentifier: "Profiles")
UIApplication.shared.keyWindow?.rootViewController?.navigationController
let tabController = UIApplication.shared.keyWindow?.rootViewController as! ViewTabBarController
let navController = tabController.selectedViewController as! UINavigationController
navController.popToViewController(viewController!, animated: true)
}
}
使用'self.myTableView.selectRow(网址为:<?#牛逼## IndexPath#>,动画:<#T##Bool#>, scrollPosition:<#T ## UITableViewScrollPosition#>)' –
为什么不在'didSelectRow'中做所有的导航过程? –
建议的方法都不起作用,因为我有一个潜在的问题。我希望控制器按此顺序移动A-> B-> C-> B-> A。在prepareSegue中,我通过PushController加载B.所以,当我从A向前移动时,它滑过B并停在C处,当我从C返回时,它会到达B,当我从B返回时,它会返回A.整个事情很好地工作。因为这个,我在C中的ViewDidAppear不会被触发。但是,如果我使用将逻辑放在C的didSelectRow中的方法,并且用户从不选择它只是导航的行。所以我不知道如何嵌入逻辑和在哪里? – vrao