Determine viewWillAppear from Popped UINavigationController or UITabBarController(从弹出的 UINavigationController 或 UITabBarController 确定 viewWillAppear)
问题描述
我无法找到一种方法来区分从导航控制器堆栈弹出和从 UITabBarController 进入视图控制器.
I am unable to find a way to distinguish between popping from the Nav controller stack and entering the view controller from the UITabBarController.
我只想在视图从 TabBar 呈现时调用 ViewWillAppear 中的方法,而不是当有人在导航控制器中按下回时.
I want to call a method in ViewWillAppear only when the view is presented from the TabBar, not when someone presses back in the navigation controller.
如果我不使用 TabBarController,我可以使用 viewDidLoad 轻松获得此功能.
If I wasn't using a TabBarController, I could easily get this functionally using viewDidLoad.
我试过了,
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
println("View Will Appear")
if isBeingPresented() {
println("BP")
}
if isMovingFromParentViewController() {
println("from")
}
if isMovingToParentViewController() {
println("to")
}
}
但是当我按下 Tab 按钮或按下返回按钮时没有区别.
But there is no difference when I present from pressing the Tab Button or when press back button.
只有视图将出现"被调用.
Only the "View Will Appear" is getting called.
使用 iOS 8.4/Swift
Using iOS 8.4 / Swift
推荐答案
听起来很好用 UITabBarControllerDelegate.
Sounds like a good use of the UITabBarControllerDelegate.
首先,在 ViewController comingFromTab 上添加一个 Bool 属性:
First, add a Bool property on your ViewController comingFromTab:
class MyViewController: UIViewController {
var comingFromTab = false
// ...
}
将您的 UITabBarControllerDelegate 设置为您想要的任何类并实现方法 shouldSelectViewController.您可能还想继承 UITabBarController 并将它们放在那里.
Set your UITabBarControllerDelegate to whatever class you want and implement the method shouldSelectViewController. You may also want to subclass UITabBarController and put them in there.
func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {
if let myViewController = viewController as? MyViewController {
myViewController.comingFromTab = true
}
如果您的选项卡的初始视图控制器是 UINavigationController,您将必须打开它并访问它的第一个视图控制器:
If your tab's initial view controller is a UINavigationController, you will have to unwrap that and access it's first view controller:
if let navController = viewController as? UINavigationController {
if let myViewController = navController.viewControllers[0] as? MyViewController {
// do stuff
}
}
最后,在视图控制器的 viewWillAppear 中添加您需要的任何功能:
Lastly, add whatever functionality you need in viewWillAppear in your view controller:
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
// ...
if comingFromTab {
// Do whatever you need to do here if coming from the tab selection
comingFromTab = false
}
}
这篇关于从弹出的 UINavigationController 或 UITabBarController 确定 viewWillAppear的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从弹出的 UINavigationController 或 UITabBarController 确定 viewWillAppear
- GPS状态的广播接收器? 2022-01-01
- 如何在 iPhone 模拟器中重置 NSUserDefaults 数据? 2022-01-01
- URL编码Swift iOS 2022-01-01
- 在 Iphone SDK 的导航栏上添加多个按钮 2022-01-01
- SetOnItemSelectedListener上的微调程序错误 2022-01-01
- 使用自动布局向 UIScrollView 添加动态大小的视图 2022-01-01
- 网上有没有好的 UIScrollView 教程? 2022-01-01
- 类似于 Mail.app 的 iPad 模态视图控制器? 2022-01-01
- Xcode 7.3 中带有 UILabel 的 UIStackView 2022-01-01
- UITextView 内容插图 2022-01-01
