在 Xcode 中以编程方式按下 UITabBar 按钮

发布于 2024-08-12 12:18:28 字数 273 浏览 9 评论 0原文

抱歉新手问题。我的主窗口视图中有一个 UITabBar 以及每个选项卡的 UINavigationController 数组。该结构类似于 iPod 应用程序,因为可以通过选择 TabBar 项目来查看主视图,然后用户可以通过将视图推送到堆栈来使用 NavigationController 进一步向下钻取。

我希望能够做的是相当于从代码中的任何子视图按下底部的 TabBar 按钮(即更改 TabBar 的选定属性并显示启动选项卡的第一个视图控制器) 。

任何帮助将不胜感激。

戴夫

Sorry for the newbie question. I have a UITabBar in my main window view as well as an array of UINavigationControllers for each Tab. The structure is similar to the iPod app in that the main views can be seen by selecting TabBar items and then the user can drill down further with the NavigationController by pushing views to the stack.

What I would like to be able to do is to do the equivalent of pressing a TabBar button at the bottom from any of the subviews in code (i.e., change the selected property of the TabBar and display launch the first view controller for the tab).

Any help would be greatly appreciated.

Dave

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

不及他 2024-08-19 12:18:28
[myTabBarController setSelectedIndex:index]

编辑:回答评论中的第 2 部分问题:

您可以在 AppDelegate 中定义一个方法来切换到不同的选项卡。

您可以从任何地方获取 appdelegate 并发送消息。
像这样的东西:

 MyAppDelegate *appDelegate = (MyAppDelegate*) [[UIApplication sharedApplication] delegate];
 [appDelegate SwitchToTab:index]
[myTabBarController setSelectedIndex:index]

EDIT: Answering the part 2 question from the comment:

You can define a method in AppDelegate for switching to a different tab.

And you can get hold of appdelegate from anywhere and send a message..
something like:

 MyAppDelegate *appDelegate = (MyAppDelegate*) [[UIApplication sharedApplication] delegate];
 [appDelegate SwitchToTab:index]
徒留西风 2024-08-19 12:18:28

或者...

[self.parentViewController.tabBarController setSelectedIndex:3];

alternatively...

[self.parentViewController.tabBarController setSelectedIndex:3];
遗心遗梦遗幸福 2024-08-19 12:18:28

我想回复 Prakash,但不知道如何回复。也许我会被封锁,直到我的分数上升。

无论如何,我希望这对某人有帮助:

我正在按照普拉卡什所说的去做,但什么也没发生。这是因为为了获得指向我的应用程序委托的指针,我正在这样做:

AppDelegate_Phone *appDelegate = [[AppDelegate_Phone alloc] init];

当我应该这样做时:

AppDelegate_Phone *appDelegate = (AppDelegate_Phone *) [[UIApplication sharedApplication] delegate];

新手错误。

I'd like to reply to Prakash, but can't figure out how. Maybe I'm blocked until my score goes up.

Anyhow, I hope this helps someone:

I was doing what Prakash said, and nothing was happening. It's because to get a pointer to my app delegate, I was doing this:

AppDelegate_Phone *appDelegate = [[AppDelegate_Phone alloc] init];

When I should have been doing this:

AppDelegate_Phone *appDelegate = (AppDelegate_Phone *) [[UIApplication sharedApplication] delegate];

Newbie mistake.

多彩岁月 2024-08-19 12:18:28

为此,您只需要获取 UITabBar 控制器 -

.h 文件 -

UITabBarController *_pTabBarController;
@property (nonatomic, retain) IBOutlet  UITabBarController *_pTabBarController;

.m File -
// synthesize it 
@synthesize  _pTabBarController;    

At initial load 

// You can  write one function to add tabBar - 

// As you have already mentioned you have created an array , if not 

_pTabBarController = [[UITabBarController alloc] init];
    NSMutableArray *localViewControllersArray = [[NSMutableArray alloc] initWithCapacity:4];
    UINavigationController *theNavigationController;

    _pController = [[Controller alloc] initStart];  
    _pController.tabBarItem.tag = 1;
    _pController.title = @"Baranches";
    theNavigationController = [[UINavigationController alloc] initWithRootViewController:_pController];
    theNavigationController.tabBarItem.tag = 1;
    theNavigationController.tabBarItem.image = [UIImage imageNamed:@"icon_branches.png"];
    [localViewControllersArray addObject:theNavigationController];
    [theNavigationController release];

就可以根据您的需要设置索引

self._pTabBarController.selectedIndex = 0; // as per your requirements

For this, You just need to take UITabBar controller -

.h File -

UITabBarController *_pTabBarController;
@property (nonatomic, retain) IBOutlet  UITabBarController *_pTabBarController;

.m File -
// synthesize it 
@synthesize  _pTabBarController;    

At initial load 

// You can  write one function to add tabBar - 

// As you have already mentioned you have created an array , if not 

_pTabBarController = [[UITabBarController alloc] init];
    NSMutableArray *localViewControllersArray = [[NSMutableArray alloc] initWithCapacity:4];
    UINavigationController *theNavigationController;

    _pController = [[Controller alloc] initStart];  
    _pController.tabBarItem.tag = 1;
    _pController.title = @"Baranches";
    theNavigationController = [[UINavigationController alloc] initWithRootViewController:_pController];
    theNavigationController.tabBarItem.tag = 1;
    theNavigationController.tabBarItem.image = [UIImage imageNamed:@"icon_branches.png"];
    [localViewControllersArray addObject:theNavigationController];
    [theNavigationController release];

than you can set index as per your needs

self._pTabBarController.selectedIndex = 0; // as per your requirements
遗弃M 2024-08-19 12:18:28
[self.parentViewController.tabBarController setSelectedIndex:3];

为我选择了索引,但它只是突出显示了导航栏控制器的索引作为活动索引,但是虽然它突出显示了该索引,但它实际上位于与选项卡菜单项建议的不同的视图控制器上。

只是想补充一点,我从视图控制器中使用了它,它的表现就像有人实际按下了菜单项一样;来自代码:

UITabBarController *MyTabController = (UITabBarController *)((AppDelegate*) [[UIApplication sharedApplication] delegate]).window.rootViewController;
[MyTabController setSelectedIndex:1];

感谢您的这篇文章/答案,它对我的​​项目帮助很大。

[self.parentViewController.tabBarController setSelectedIndex:3];

Selected the index for me but it just highlighted the navbarcontroller's index as the active index, but while it highlighted that index it was actually on a different viewcontroller than was suggested by the tabbarmenu item.

Just wanted to add that I used this from my view controller, and it performed like someone actually pressed the menuitem; from code:

UITabBarController *MyTabController = (UITabBarController *)((AppDelegate*) [[UIApplication sharedApplication] delegate]).window.rootViewController;
[MyTabController setSelectedIndex:1];

Thank you for this post/answers it helped out a lot in my project.

迷鸟归林 2024-08-19 12:18:28

我想做类似的事情,但对于 XCode 6.4 iOS (8.4) setSelectedIndex 本身不会这样做。

将选项卡栏的视图控制器添加到列表中,然后在某个函数中使用类似以下内容,然后调用它:

FirstViewController *firstVC = [[self viewControllers] objectAtIndex:0];
[self.selectedViewController.view removeFromSuperview]
[self.view insertSubview:firstVC.view belowSubview:self.tabBar];
[self.tabBar setSelectedItem:self.firstTabBarItem];
self.selectedViewController = firstVC;

您的 didSelectedItem 中可能已经有类似的代码。

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {

   if (item == self.firstTabBarItem) 
      // Right here
    }
    else if ...
}

I wanted to do something similar but for XCode 6.4 iOS (8.4) setSelectedIndex by itself won't do it.

Add the view controllers of the tab bar to a list and then use something like the following in some function and then call it:

FirstViewController *firstVC = [[self viewControllers] objectAtIndex:0];
[self.selectedViewController.view removeFromSuperview]
[self.view insertSubview:firstVC.view belowSubview:self.tabBar];
[self.tabBar setSelectedItem:self.firstTabBarItem];
self.selectedViewController = firstVC;

You might have similar code already inside your didSelectedItem..

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {

   if (item == self.firstTabBarItem) 
      // Right here
    }
    else if ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文