我知道这将是一个简单的修复,但出于某种原因,当我尝试用其他解决方案修复我的问题时,它只是不起作用。在我的应用程序中,我设置了推送通知,并且我正在尝试设置从通知打开应用程序时将应用程序打开到某个tabBarItem
的功能。这是我到目前为止的代码,请告诉我,如果我甚至使用正确的方法。我对AppDelegate.m
不是很有经验所以如果完全错误,请原谅我。
AppDelegate.m
#import <CoreLocation/CoreLocation.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate,UITabBarControllerDelegate,CLLocationManagerDelegate>{
CLLocationManager *locationManager;
}
@property (strong, nonatomic) UIWindow *window;
@property(nonatomic,readonly) UITabBar *tabBar;
@property(nonatomic,strong) UITabBarController *tabBarController;
@end
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIApplication* app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = YES;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound)];
return YES;
}
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo{
/* this works just fine*/
NSLog(@"opening from a notification!!");
/*this is what doesnt work at all*/
self.tabBarController = [[UITabBarController alloc] init];
[self.tabBarController ];
self.tabBarController.selectedIndex = 3;
/*also when i do nslog the selectedIndex it gives me this value "2147483647"*/
NSLog(@"%i is the tabbar item",tabBarController.selectedIndex);
}
答案 0 :(得分:3)
因为您从登录控制器推送到标签栏控制器,所以无法从应用程序委托中获取对标签栏控制器的引用,因为它在推送之前不存在。我解决这个问题的方法是改变你的应用程序的结构。虽然你的当前结构没有任何错误(因为它会导致崩溃),但我不喜欢将登录控制器排在第一位,因为它们只用一次登录,那么如果它们会消失会更好。我会将标签栏控制器设置为窗口的根视图控制器,并从第一个选项卡中控制器的viewDidAppear方法以模态方式呈现登录控制器,而不显示动画 - 这将使登录控制器成为用户看到的第一件事。完成登录后,您只需关闭,然后返回第一个标签页。
如果您使用此结构,则可以使用我的建议来设置所选索引:
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo{
NSLog(@"opening from a notification!!");
self.tabBarController = self.window.rootViewController;
self.tabBarController.selectedIndex = 3;
NSLog(@"%i is the tabbar item",tabBarController.selectedIndex);
}
答案 1 :(得分:1)
试试这个
NSUInteger index = [self.tabBarController.tabBar.items indexOfObject:self.tabBarController.tabBar.selectedItem];
NSLog(@"Index: %d", index);
答案 2 :(得分:1)
对于本地通知:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
// it from running if it doesn't exist (such as running under iOS 7 or earlier).
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
#endif
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
if ([UIApplication sharedApplication].scheduledLocalNotifications.count >= 1) {
// Handle local notification received if app wasn't running in background
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
tabBarController.selectedIndex = 1;
}else{
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"tab"];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
}
return YES;
}
推送通知:
(1)。
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo{
NSLog(@"opening from a push/remote notification!!");
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
tabBarController.selectedIndex = 1;
NSLog(@"%i is the tabbar item",tabBarController.selectedIndex);
}
**(OR).** `
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
int badgeCount = [UIApplication sharedApplication].applicationIconBadgeNumber;
if (badgeCount >= 1) {
// Handle local notification received if app wasn't running in background
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
tabBarController.selectedIndex = 1;
}else{
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"tab"];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
}
self.window.backgroundColor = [UIColor whiteColor];
return YES;
}
`
答案 3 :(得分:0)
您应该使用selectedIndex
属性。
selectedViewController
self.tabBarController.selectedViewController = [self.tabBarController objectAtIndex:3];//or whatever the index the VC you want to select is
此外,您不应该从AppDelegate分配tabBarController。你需要获取rootViewController,然后从那里获取已经实例化的tabBarController的实例
在此处查看已接受的答案,了解如何从appDelegate获取VC。
答案 4 :(得分:0)
self.tabBarController = [[UITabBarController alloc]init];
self.tabBarController.delegate = mobileYakAppDelegate;
self.tabBarController.viewControllers = ................your view controllers adding here .........;
self.tabBarController.selectedIndex = 3;
self.window.rootViewController = mobileYakAppDelegate.tabBarController;
尝试这样对我来说效果很好