gpt4 book ai didi

swift - 为什么这个 UITableViewController 会模态关闭?

转载 作者:行者123 更新时间:2023-11-28 06:28:06 25 4
gpt4 key购买 nike

我有一个 UITableViewController,当从一个屏幕显示时,它会从右到左显示标准的“显示”转场,当从另一个屏幕显示时(UIViewController), 从底部模态呈现。我在 question 的帮助下使它正常工作我几个月前问过(有截图)。

关键是创建从我的设置屏幕的 UINavigationController 到共享的 UITableViewController 的转接,而不是从 UITableViewCell 创建它。奇怪的是,即使它从右到左正确显示,关闭它也会以模态方式(从上到下)关闭它。

我正在让呈现 TableView Controller 成为它呈现的 UITableViewController 的委托(delegate),这样它将处理解雇。这是它实现的协议(protocol)和扩展(Swift 2.3):

protocol DismissalDelegate : class {
func selectionDidFinish(controller: UIViewController)
}

extension DismissalDelegate where Self: UIViewController {
func selectionDidFinish(viewController: UIViewController) {
self.dismissViewControllerAnimated(true, completion: nil)
}
}

然后我将它设置在呈现 Controller 中定义的 segue 中:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "goToLifts" {
let destination = segue.destinationViewController as! LiftSelectionTableViewController
destination.dismissalDelegate = self
} else {
return
}
}

当用户进行选择时(在 didSelectRowAtIndexPath 中),呈现的 TableView Controller 调用 delegate?.selectionDidFinish(self):

 override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
guard (dismissalDelegate != nil ) else {
return
}
dismissalDelegate?.selectionDidFinish(self)
}

在呈现 TableView Controller 中调用此方法:

func selectionDidFinish(controller: LiftSelectionTableViewController) {
self.dismissViewControllerAnimated(true, completion: nil)
}

我已经查看了 APIs用于呈现 View Controller 并且无法找到任何公开选项来控制它的东西。 dismiss(animated:completion:) API 甚至说它用于关闭模态呈现的 View Controller ,但我没有看到与关闭有任何其他关系。

当它从我的 UITableViewController(从右到左,再向后)呈现时,我怎样才能让它以同样的方式消失,但在从另一个 View 呈现时保持模态行为(a UIViewController)?

最佳答案

我在这里有点困惑,看起来你正在使用委托(delegate),因为呈现 View Controller 应该知道 LiftSelectionTableViewController 是如何呈现的。

所以在 TableView Controller 中,你会有

func selectionDidFinish(viewController: UIViewController) {
self.navigationController?.popViewControllerAnimated(true)
}

在另一个 View Controller 中,你应该有

func selectionDidFinish(viewController: UIViewController) {
self.dismissViewControllerAnimated(true, completion: nil)
}

如果我错了并且您不知道 View Controller 是如何呈现的,那么我会尝试检查导航 Controller 上的顶 View Controller 是否呈现 View Controller 。如果是,弹出 View Controller ,如果不是,则关闭 View Controller 。

func selectionDidFinish(viewController: UIViewController) {
if self.navigationController?.topViewController == viewController {
self.navigationController?.popViewControllerAnimated(true)
} else {
self.dismissViewControllerAnimated(true, completion: nil)
}
}

关于swift - 为什么这个 UITableViewController 会模态关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41202522/

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