我想要更改从推送通知打开应用时打开的标签页。
目前,我让app在app delegate中注册通知并在第一个视图控制器中运行方法:
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
答案 0 :(得分:1)
Luc是对的,虽然它是self.view.window.rootViewController
而不是self.rootviewcontroller
。
但是我不认为这是完美的解决方案,因为TabBarController
并不总是必须是rootViewController ......
您是否在app delegate / mainwindow或其他viewController中拥有TabBarController
?
尝试在app delegate或包含TabBarController的viewController中设置selectedIndex
。
我使用此功能在我的某个应用中使用此功能:
<强> AppDelegate.m 强>
-(void)showReportTab{
[self.tabBarController setSelectedIndex:1];
}
AppDelegate.h
@property (nonatomic, strong) IBOutlet UITabBarController *tabBarController;
答案 1 :(得分:0)
我们还可以从方法application:didFinishLaunchingWithOptions:
获取代码吗?我想看看你是如何创建tabBarController的。
还尝试在方法self.tabBarController.selectedIndex = 2;
中调用application:didFinishLaunchingWithOptions:
并检查正确的视图控制器是否变为活动状态。