简体   繁体   English

以编程方式选择TabBarController视图?

[英]Programmatically Selecting a TabBarController View?

In order to get my custom menu up and running, I've ended up using a UITabBarController and need to change the view displayed programmatically, vs the standard tabbed menu on screen. 为了启动和运行我的自定义菜单,我最终使用了UITabBarController并且需要更改以编程方式显示的视图,而不是屏幕上的标准选项卡式菜单。

Everything is working as expected except on thing. 一切都按预期进行,除了在事情上。 I am attempting to use: 我正在尝试使用:

[self setSelectedIndex:index];

This code is inside my UITabBarController subclass in a custom delegate method. 这段代码在自定义委托方法的UITabBarController子类中。 (This is so I can programmatically adjust the view when interacting with my menu). (因此,我可以在与菜单交互时以编程方式调整视图)。 However, while this code is called, it doesn't do anything? 但是,在调用此代码时,它没有执行任何操作吗?

Does it HAVE to be called from one of the tabbed views? 是否必须从选项卡式视图之一调用它? I was hoping to run it from inside the TabBarController to avoid repeating the code in each tabbed sub controller. 我希望从TabBarController内部运行它,以避免在每个选项卡式子控制器中重复代码。

UPDATE: 更新:

Just found that using [self setSelectedIndex:index]; 刚刚发现使用[self setSelectedIndex:index]; works fine in viewDidLoad . viewDidLoad工作正常。 But when it is called inside the delegate method, it doesn't change view. 但是当在委托方法中调用它时,它不会更改视图。 It is using the right index number and getting called, but not doing anything from that method. 它正在使用正确的索引号并被调用,但没有使用该方法执行任何操作。

Also, it seems the tab controller is a different object when I log self in viewDidLoad vs my delegate method. 此外,它似乎标签控制器是一个不同的对象,当我登录selfviewDidLoad VS我的委托方法。 So why would I be loosing the reference to the original controller? 那么,为什么我会丢失对原始控制器的引用?

It's just a UITabBarController in a container in another view controller. 它只是另一个视图控制器中容器中的UITabBarController。

Delegate Code: 代表代码:

@Interface @接口

@protocol SLMenuDelegate <NSObject>

@required -(void)menuDidChangeViewToIndex:(NSInteger)index;

@end

@property (nonatomic, assign) id<SLMenuDelegate>menuDelegate;

@Implementation @实施

@synthesize menuDelegate;

self.menuDelegate = [self.storyboard instantiateViewControllerWithIdentifier:@"TabBarViewController"];

[menuDelegate menuDidChangeViewToIndex:[self.menuItemButtons indexOfObject:sender]];

UITabBarController UITabBarController

-(void)menuDidChangeViewToIndex:(NSInteger)index
{
    [self setSelectedIndex:index];
}

Setting breakpoints and running NSLogs and there is no question that the method gets called and all code runs. 设置断点并运行NSLogs,毫无疑问,该方法将被调用并且所有代码都将运行。

Try using delayed performance: 尝试使用延迟的性能:

 dispatch_async(dispatch_get_main_queue(), ^{
      [self setSelectedIndex:index];
 });

I didn't manage to find a solution to the exact issue, but I found an equally good way of resolving my issue simply. 我没有设法找到确切问题的解决方案,但是我找到了一种简单地解决问题的同样好的方法。

I stopped using a delegate to send my button tap message and change the view. 我不再使用委托来发送按钮点击消息并更改视图。 Instead I did the following: 相反,我做了以下工作:

SLTabBarViewController *tabBar = [self.childViewControllers objectAtIndex:0];
[tabBar setSelectedIndex:[self.menuItemButtons indexOfObject:sender]];

This gets the embedded tab bar controller and I simply directly change the view from the original view controller from which the button tap comes from. 这样就获得了嵌入式选项卡栏控制器,我只需直接从按钮点击来自的原始视图控制器中更改视图即可。

It may not be an intelligent solution, but its a simple and functional one which doesn't create any problems. 它可能不是一个智能解决方案,但它是一种简单实用的解决方案,不会产生任何问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM