我想更改从推送通知打开应用程序时打开的选项卡。
目前,我让应用程序在应用程序委托中注册通知并在第一个视图控制器中运行一个方法:
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