获取之前的 UINavController
Get the previous UINavController
如何引用之前的view controller。我想使用类似下面的 if 语句:
if previousViewController = searchController {
//preform a method
}else if previousViewController = notificationController {
//preform another method
}
但是如何获取对前一个视图控制器的引用?因此,例如,如果我单击视图控制器 A 上的一个按钮将我带到视图控制器 B,我想以某种方式获得对视图控制器 A 的引用。我看过类似的问题,但我似乎找不到有帮助的答案我够了提前致谢
我的首选解决方法是在 ViewControllerB
中添加一个 属性 以显示它的用法。例如……
class MyViewController: UIViewController {
enum Mode {
case search, notify
}
var mode = Mode.search
override viewDidLoad() {
super.viewDidLoad() {
configure()
}
}
func configure() {
switch mode {
case .search:
// configure for search results
case .notify:
// configure for notification
}
}
}
然后在呈现视图控制器中:
class SearchViewController: UIViewController {
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let mvc = segue.destination as? MyViewController {
mvc.mode = .search
}
}
}
或设置 mvc.mode = .notify
如果从您的 NotificationViewController
展示
使用此方法,MyViewController
不需要知道它是如何呈现的或呈现的内容。您可以将其推入导航堆栈,或将 SearchViewController
更改为 DownloadViewController
,这都没有关系。
如何引用之前的view controller。我想使用类似下面的 if 语句:
if previousViewController = searchController {
//preform a method
}else if previousViewController = notificationController {
//preform another method
}
但是如何获取对前一个视图控制器的引用?因此,例如,如果我单击视图控制器 A 上的一个按钮将我带到视图控制器 B,我想以某种方式获得对视图控制器 A 的引用。我看过类似的问题,但我似乎找不到有帮助的答案我够了提前致谢
我的首选解决方法是在 ViewControllerB
中添加一个 属性 以显示它的用法。例如……
class MyViewController: UIViewController {
enum Mode {
case search, notify
}
var mode = Mode.search
override viewDidLoad() {
super.viewDidLoad() {
configure()
}
}
func configure() {
switch mode {
case .search:
// configure for search results
case .notify:
// configure for notification
}
}
}
然后在呈现视图控制器中:
class SearchViewController: UIViewController {
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let mvc = segue.destination as? MyViewController {
mvc.mode = .search
}
}
}
或设置 mvc.mode = .notify
如果从您的 NotificationViewController
使用此方法,MyViewController
不需要知道它是如何呈现的或呈现的内容。您可以将其推入导航堆栈,或将 SearchViewController
更改为 DownloadViewController
,这都没有关系。