gpt4 book ai didi

ios - 使用实例变量来指示 View 是 Edit 还是 Show

转载 作者:行者123 更新时间:2023-12-01 21:59:04 25 4
gpt4 key购买 nike

Start Developing iOS Apps (Swift): Implement Edit and Delete Behavior , 官方教程告诉我应该使用presentingViewController 和navigationController 来指示指定的 View 是Edit 还是Show,如下代码:

@IBAction func cancel(_ sender: UIBarButtonItem) {
// Depending on style of presentation (modal or push presentation), this view controller needs to be dismissed in two different ways.
let isPresentingInAddMealMode = presentingViewController is UINavigationController

if isPresentingInAddMealMode {
dismiss(animated: true, completion: nil)
}
else if let owningNavigationController = navigationController{
owningNavigationController.popViewController(animated: true)
}
else {
fatalError("The MealViewController is not inside a navigation controller.")
}
}

添加 View 由modal呈现,编辑 View 由embed导航 Controller 呈现,但是我觉得这种方式不好理解和维护,不如引入一个 isEditOrShow View 中的实例变量来指示状态?如下所示:
@IBAction func cancel(_ sender: UIBarButtonItem) {
// Depending on style of presentation (modal or push presentation), this view controller needs to be dismissed in two different ways.

if isEditingOrShow = .edit{
dismiss(animated: true, completion: nil)
}
else isEditingOrShow = .show{
owningNavigationController.popViewController(animated: true)
}
}

最佳答案

本教程解释说,有两种方法可以关闭 View Controller 。

例如,当您将 View Controller 呈现为模态时,您可以使用以下代码将其关闭。

dismiss(animated: true, completion: nil)

但是,如果您使用的是推送演示(导航 Controller ),那么您应该使用以下代码将其关闭。
owningNavigationController.popViewController(animated: true)

How about introduce a bool isEditOrShow instance varible in the view to indicate the state?



据我了解,您将不需要 isEditOrShow 变量。如果您有任何问题,请告诉我。

关于ios - 使用实例变量来指示 View 是 Edit 还是 Show,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60665905/

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