从视图控制器获取当前选定选项卡的 selectedIndex

发布于 2024-08-09 22:01:26 字数 1021 浏览 8 评论 0原文

我目前有一个带有选项卡栏和多个视图控制器的 iPhone 应用程序。所有视图都是在 Interface Builder 中设计的。我希望能够从视图控制器获取选项卡栏当前选定的索引,但由于某种原因该属性返回(null)。

我在视图控制器的 viewDidLoad 函数中调用了以下内容:

self.tabBarController.selectedIndex

执行此操作的正确方法是什么?

使用 AppDelegate 类的代码进行了更新。

MyAppDelegate.h

#import <UIKit/UIKit.h>

@interface MyAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
  UIWindow *window;
  UITabBarController *tabBarController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;

@end

MyAppDelegate.m:

#import "MyAppDelegate.h"

@implementation MyAppDelegate

@synthesize window, tabBarController;

- (void)applicationDidFinishLaunching:(UIApplication *)application {
  [window addSubview:tabBarController.view];
}

- (void)dealloc {
    [tabBarController release];
    [window release];
    [super dealloc];
}

@end

I currently have an iPhone application with a tabbar and multiple viewcontrollers. All the views are designed in Interface Builder. I'd like to be able to get the currently selected index of the tabbar from the viewcontroller, but for some reason this property returns (null).

I called the following in the viewDidLoad function of my viewcontroller:

self.tabBarController.selectedIndex

What would be the correct way to do this?

Updated with the code of the AppDelegate class.

MyAppDelegate.h

#import <UIKit/UIKit.h>

@interface MyAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
  UIWindow *window;
  UITabBarController *tabBarController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;

@end

MyAppDelegate.m:

#import "MyAppDelegate.h"

@implementation MyAppDelegate

@synthesize window, tabBarController;

- (void)applicationDidFinishLaunching:(UIApplication *)application {
  [window addSubview:tabBarController.view];
}

- (void)dealloc {
    [tabBarController release];
    [window release];
    [super dealloc];
}

@end

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

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

发布评论

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

评论(2

人海汹涌 2024-08-16 22:01:26

您的 appDelegate 类中应该有一个指向选项卡栏的指针。您的视图没有选项卡栏,因此您从 [self.tabBarController selectedIndex] 收到 nil 。

You should have a pointer to your tabbar in your appDelegate class. Your view has no tabbar, so you recieve nil from [self.tabBarController selectedIndex].

完美的未来在梦里 2024-08-16 22:01:26

我想我已经明白了。使用以下命令会返回正确的索引:

  MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
NSLog(@"%d", appDelegate.tabBarController.selectedIndex);

应用程序崩溃的原因是我在 NSLog 部分中使用了 %@ 而不是 %d。我可以发誓我之前尝试过%d,奇怪...

索引现在返回,但只返回一次。点击选项卡部分后,会返回索引号,但是当您再次点击另一个部分时,不会打印任何数字。可能是因为视图已经加载过一次。有什么办法可以解决这个问题吗?

I think I've got it. Using the following returns the correct index:

  MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
NSLog(@"%d", appDelegate.tabBarController.selectedIndex);

The reason the application was crashing was the fact that I used %@ instead of %d in the NSLog part. I could have sworn I tried %d before, strange...

The index is now returned, but only once. After you tap the tab section an index number is returned, but when you tap another section again no number is printed. Probably because the view has already been loaded once. Is there any way to work around this?

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