如何从选项卡栏控制器访问视图控制器?
我有一个选项卡栏控制器,每个选项卡都包含一个导航控制器。
如何访问选项卡中导航控制器中的特定视图控制器,以便访问特定于该视图控制器的属性?
我尝试了以下代码:
//Get the navigation controller of the 3rd tab
self.tabController.selectedViewController
= [self.tabController.viewControllers objectAtIndex:2];
//Attempt to retrieve the viewcontroller I want from the tabcontroller
SomeViewController *svc = (SomeViewController *)self.tabController.selectedViewController;
//Attempting to access a BOOLEAN property in svc viewcontroller
svc.someProperty = YES;
上面的代码失败了,因为似乎“self.tabController.selectedViewController”返回了我一个导航控制器。如何扩展代码以便可以访问“svc.someProperty”?
I have a tab bar controller and each tab consist of a navigation controller.
How can I access a specific view controller in a navigation controller in a tab so that I can access a property specific to the view controller?
I tried the following code:
//Get the navigation controller of the 3rd tab
self.tabController.selectedViewController
= [self.tabController.viewControllers objectAtIndex:2];
//Attempt to retrieve the viewcontroller I want from the tabcontroller
SomeViewController *svc = (SomeViewController *)self.tabController.selectedViewController;
//Attempting to access a BOOLEAN property in svc viewcontroller
svc.someProperty = YES;
The above code failed because it seems that "self.tabController.selectedViewController" returns me a navigation controller. How can I extend the code so that I can access "svc.someProperty"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 < code>-topViewController 从导航控制器获取视图控制器(或 <代码>-visibleViewController< /code> 如果您使用的是模态视图控制器)。
You can use
-topViewController
to get the view controller from the navigation controller (or-visibleViewController
if you are using modal view controllers).如果 self.tabController.selectedViewController 是您的导航视图控制器,那么您可以使用以下方法访问当前可见的控制器:
visibleViewController
和顶视图控制器使用:
topViewController
如果这不是允许您访问
SomeViewController
,您可以迭代导航控制器堆栈上推送的控制器列表:viewControllers
If
self.tabController.selectedViewController
is your navigation view controller, then you can access the currently visible controller using:visibleViewController
and the top view controller using:
topViewController
If this does not allow you to get to
SomeViewController
, you can iterate through the list of controllers pushed on the navigation controller stack:viewControllers
由于视图控制器是返回的 UINavigationController(SVC) 的子视图,因此您只需向其发送以下方法
[svc topViewController];
。这应该返回视图控制器,然后您应该能够访问该属性。Since the view controller is a subview of the returned UINavigationController(SVC), you could just send it the following method
[svc topViewController];
. That should return you the view controller and then you should be able to access the property.