使 TabBarItem 在启动时被选中

发布于 2024-11-26 19:44:46 字数 1583 浏览 1 评论 0原文

我使用 Xcode 4 处理 iPhone iOS4 的项目。

我在 IB 中创建了一个 UITabBar,其中包含三个 UITabBarItem。我没有 UITabBarControllerDelegate 作为委托,但有 MainViewController。

然后我在 MainViewController 中实现:

 - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
     switch (item.tag) {
         case 1:
             [scrollView setContentOffset:(CGPointMake (0,0))];
             break;
         case 2:
            [scrollView setContentOffset:(CGPointMake (320,0))];
             break;
         case 3:
            [scrollView setContentOffset:(CGPointMake (640,0))];
             break;
         default:
             break;
       }
    NSLog(@"didSelectItem: %d", item.tag);
}

这工作正常,正如我想要的那样。

但我无法在启动时选择 TabBarItem(在 viewDidLoad 中)。我尝试了

     [tabBar setSelectedItem:calc];
     [tabBar setSelectedItem:1];

(cal是MainViewController的ivar)和其他人,但没有一个有效。有什么建议吗?

谢谢。


这是MainViewController.h

  #import "FlipsideViewController.h"
  #import <CoreData/CoreData.h>
  #import <QuartzCore/QuartzCore.h>

  @interface MainViewController : UIViewController <FlipsideViewControllerDelegate>   {

     UITabBar *tabBar;
     UITabBarItem *diagBarItem;
     UITabBarItem *calcBarItem;
 }

 @property (nonatomic, retain) IBOutlet UITabBar *tabBar;
 @property (nonatomic, retain) IBOutlet UITabBarItem *diagBarItem;
 @property (nonatomic, retain) IBOutlet UITabBarItem *calcBarItem;

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

 @end

I work on a project for iPhone iOS4 with Xcode 4.

I created a UITabBar, with three UITabBarItem, in IB. I don't have UITabBarControllerDelegate as delegate, but MainViewController.

Then I implement in MainViewController:

 - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
     switch (item.tag) {
         case 1:
             [scrollView setContentOffset:(CGPointMake (0,0))];
             break;
         case 2:
            [scrollView setContentOffset:(CGPointMake (320,0))];
             break;
         case 3:
            [scrollView setContentOffset:(CGPointMake (640,0))];
             break;
         default:
             break;
       }
    NSLog(@"didSelectItem: %d", item.tag);
}

This works OK, as I want it.

But I'm not able to make a TabBarItem selected at startup (in viewDidLoad). I try

     [tabBar setSelectedItem:calc];
     [tabBar setSelectedItem:1];

(cal is an ivar of MainViewController) and others but none works. Any suggestion?

Thank you.


This is MainViewController.h

  #import "FlipsideViewController.h"
  #import <CoreData/CoreData.h>
  #import <QuartzCore/QuartzCore.h>

  @interface MainViewController : UIViewController <FlipsideViewControllerDelegate>   {

     UITabBar *tabBar;
     UITabBarItem *diagBarItem;
     UITabBarItem *calcBarItem;
 }

 @property (nonatomic, retain) IBOutlet UITabBar *tabBar;
 @property (nonatomic, retain) IBOutlet UITabBarItem *diagBarItem;
 @property (nonatomic, retain) IBOutlet UITabBarItem *calcBarItem;

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

 @end

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

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

发布评论

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

评论(3

深者入戏 2024-12-03 19:44:46

在标签栏上调用 -setSelectedItem: 用于以编程方式将 UITabBarItems 移动到标签栏上的不同位置。

要更改选定的选项卡,您需要更改 UITabBarController 实例上的 selectedIndex 属性。 UIViewController 上的属性 tabBarController 由 Interface Builder 自动设置。在 -viewDidLoad 中尝试一下:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tabBarController.selectedIndex = calc;
}

Calling -setSelectedItem: on your tab bar is used to move UITabBarItems to different spots on the tab bar programmatically.

To change the selected tab, you need to change the selectedIndex property on the instance of UITabBarController. The property tabBarController on UIViewController is automatically set by Interface Builder. Try this in -viewDidLoad:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tabBarController.selectedIndex = calc;
}
烟雨扶苏 2024-12-03 19:44:46

您需要在 UITabBarController 上设置选定的选项卡,而不是在选项卡栏本身上:

[self.tabBarController setSelectedIndex:3];

正如您所说,这不起作用,您确定已在 Interface Builder 中连接了插座吗? (假设您使用它来创建选项卡栏)

You need to set the selected tab on the UITabBarController, and not on the tab bar itself:

[self.tabBarController setSelectedIndex:3];

As you said this does not work, are you sure you have connected the outlets in Interface Builder? (Assuming that you used that to create the tab bar)

看春风乍起 2024-12-03 19:44:46

试一试吧!

tabBar.selectedIndex = 1;

Give this a shot!

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