Ios 如何从AppDelegate.m访问tabBarController?
这是我的故事板: 我试图从AppDelegate.m中的方法访问tabBarController 这是AppDelegate.h:Ios 如何从AppDelegate.m访问tabBarController?,ios,objective-c,iphone,xcode5,Ios,Objective C,Iphone,Xcode5,这是我的故事板: 我试图从AppDelegate.m中的方法访问tabBarController 这是AppDelegate.h: #import <UIKit/UIKit.h> #import <FacebookSDK/FacebookSDK.h> #import "STAlertView.h" @interface demo_AppDelegate : UIResponder <UIApplicationDelegate> @property (s
#import <UIKit/UIKit.h>
#import <FacebookSDK/FacebookSDK.h>
#import "STAlertView.h"
@interface demo_AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (nonatomic, strong) STAlertView *av;
@end
我的主要动机是,当调用此方法showFriendReqAlert:
时,第三个选项卡栏项目即Friends
会显示一个红色徽章。但每当我尝试选择tabBarItem时,NSLog中就会显示null
我还尝试了以下方法:
self.window.rootViewController.navigationController.tabBarController
self.window.rootViewController.tabBarController
但什么都不管用。有什么帮助吗
谢谢 我相信(很抱歉在此相信;-)您的第一个控制器应该是TabBarController
,您的navigationBarController
必须在之后出现,因为它对navigationController(如果您更改选项卡)推/弹出的内容没有多大意义
我建议您删除您的第一个DemoViewController
和下面的NavigationController
,并且您的第一个控制器是您的TabBarController
(只需在IB中设置“is Initial View Controller”)
然后,您可以按如下方式访问您的选项卡栏
:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UITabBarController *tbc = (UITabBarController *)self.window.rootViewController;
}
Swift 5:
guard let tabBarController=window?.rootViewController作为?UITabBarController else{return}
谢谢,但是第一个DemoViewController
是带有登录屏幕的。所以,它必须在那里,好的。您正在尝试从登录屏幕访问选项卡栏吗?在您看到它之前,它可能尚未加载。不,不是从登录屏幕,我实际上是在后台获取后尝试更改选项卡栏项。因此,我是从AppDelegate.m内部执行此操作的。您是否尝试过检查哪个变量为nil:UINavigationController*nav=self.window.rootViewController.navigationController;如果导航!=nil{UITabBarController*tbc=nav.tabBarController;…}它真的是tabBarController吗?登录屏幕和选项卡栏之间的导航控制器的用途是什么?您在登录时使用什么类型的segue?您的登录屏幕未嵌入到导航控制器中,因此我希望self.window.rootViewController.navigationController为零。我有完全相同的问题。RootViewController是登录屏幕,我想在收到通知后更改徽章。你解决问题了吗?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UITabBarController *tbc = (UITabBarController *)self.window.rootViewController;
}