2

我想更改从推送通知打开应用程序时打开的选项卡。

目前,我让应用程序在应用程序委托中注册通知并在第一个视图控制器中运行一个方法:

AppDelegate.m

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

NSLog(@"didReceiveRemoteNotification");self.apstype = [[NSMutableString alloc] init];self.apstype = [NSMutableString stringWithFormat:@"%@", [[userInfo objectForKey:@"aps"] objectForKey:@"apstype"]];

    // Add the tab bar controller's current view as a subview of the window
    [[UIApplication sharedApplication] setIdleTimerDisabled:YES];

    HomeTab_Main *controller = [HomeTab_Main alloc];
    [controller receivedNotification];

    if (application.applicationState == UIApplicationStateActive){
        NSLog(@"UIApplicationStateActive");
    } else if (application.applicationState == UIApplicationStateBackground){
        NSLog(@"UIApplicationStateBackground");
    } else if (application.applicationState == UIApplicationStateInactive){
        NSLog(@"UIApplicationStateInactive");
    } else {
        NSLog(@"else");
    }
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UINavigationBar *navigationBarProxy = [UINavigationBar appearance];
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){
        UIImage *backgroundImage = [UIImage imageNamed:@"ge_navigationBar.png"];
        [navigationBarProxy setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault];

    } else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
        UIImage *backgroundImage = [UIImage imageNamed:@"ge_navigationBar~ipad.png"];
        [navigationBarProxy setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault];
    }

    [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];

    UIColor *tintColour = [UIColor colorWithRed:0.000 green:0.600 blue:0.765 alpha:1];
    [[UIBarButtonItem appearance] setTintColor:tintColour];

    [[UINavigationBar appearance] setTitleTextAttributes:
     [NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0],
      UITextAttributeTextColor, [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8], UITextAttributeTextShadowColor, [NSValue valueWithUIOffset:UIOffsetMake(0, -1)], UITextAttributeTextShadowOffset, [UIFont fontWithName:@"Arial-Bold" size:0.0], UITextAttributeFont, nil]];

    return YES;
}

HomeTab_Main.m

- (void)receivedNotification
{
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    NSLog(@"%@", appDelegate.apstype);
    if([appDelegate.apstype isEqualToString:@"inbox"]){
        NSLog(@"goToTab");
        [self performSelector:@selector(goToTab) withObject:nil afterDelay:0.1];
    }
}

- (void)goToTab
{    
    NSLog(@"Go to tab");
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"1 new message" message:@"click to read" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:nil, nil];
    [alert show];

    self.tabBarController.selectedIndex = 2;
}

我遇到的主要问题是警报视图正在显示,但选项卡没有改变。从通知访问应用程序时,我想自动切换到相应的选项卡。有谁知道这怎么可能有任何例子?

添加了 didFinishLaunchingWithOptions

4

2 回答 2

1

Luc 是对的,尽管它self.view.window.rootViewController不是self.rootviewcontroller. 但是我认为这不是完美的解决方案,因为TabBarController并不总是必须是 rootViewController ...

TabBarController在应用程序委托/主窗口或另一个视图控制器中有你的吗?

尝试selectedIndex在应用程序委托或包含 TabBarController 的 viewController 中设置。

我使用它在我的一个应用程序中使用它:

AppDelegate.m

-(void)showReportTab{
    [self.tabBarController setSelectedIndex:1];
}

AppDelegate.h

@property (nonatomic, strong) IBOutlet UITabBarController *tabBarController;
于 2012-06-22T12:09:00.667 回答
0

Can we also get the code from the method application:didFinishLaunchingWithOptions:? I would like to see how you are creating the tabBarController.

Also try to call self.tabBarController.selectedIndex = 2; in the method application:didFinishLaunchingWithOptions: and check if the correct view controller becomes active.

于 2012-06-22T10:50:51.283 回答