3

我知道这将是一个简单的解决方案,但由于某种原因,当我尝试使用其他解决方案解决我的问题时,它只是不起作用。在我的应用程序中,我设置了推送通知,并且我试图设置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);
}
4

5 回答 5

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);
}
于 2013-08-20T05:23:05.467 回答
1

尝试这个

NSUInteger index = [self.tabBarController.tabBar.items indexOfObject:self.tabBarController.tabBar.selectedItem];    
NSLog(@"Index: %d", index);
于 2013-10-25T07:46:25.743 回答
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;
}

`

于 2016-03-29T12:35:32.470 回答
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;  

像这样尝试它对我来说很好

于 2013-08-20T04:34:07.727 回答
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 的示例。

如何从我的 appDelegate 访问我的 viewController?iOS

于 2013-08-19T23:55:37.473 回答