iPhone 7
iPhone 7

Reputation: 1741

Handle push notifications via UIAlertview

Receiving push notifications and trying to handle through alert view and on selection of a button of alert view want to navigate to a particular view controller.

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {
    NSString *cancelTitle = @"Close";
    NSString *showTitle = @"Show";
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"App"
                                                        message:@"You got a new request."
                                                       delegate:self
                                              cancelButtonTitle:cancelTitle
                                              otherButtonTitles:showTitle, nil];
    [alertView show];

} else {
    //Do stuff that you would do if the application was not active
}
}

I want to move on the click of show button to another view controller.

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 0)
{

}
if(buttonIndex == 1)
{
    [self showNewContact];
}
}

-(void)showNewContact
{

Edit_app_viewController *detail=[[Edit_app_viewController alloc]init];

UIViewController *c = [self topViewController];
NSLog(@"View iS %@",c);

[c.navigationController pushViewController:detail animated:YES];

}

Receiving error: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_UIModalItemsPresentingViewController pushViewController:animated:]: unrecognized selector sent to instance 0x169f5f10' *** First throw call stack:

Please guide.

Update:

 - (UIViewController*)topViewController
{
return [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}

Upvotes: 1

Views: 125

Answers (3)

Mahmoud Adam
Mahmoud Adam

Reputation: 5852

I think the problem is that you are pushing a ViewController while you don't have a NavigationContoller

you should put your modal view that is presented into a UINavigationController

update:

the problem is that you need to grantee that the rootViewController is always a UINavigationController as you can get the push notification any time while using the app, so i suggest to enclose your rootViewController into a UINavigationController

UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:myViewController]; 
self.window.rootViewController = nav;

Upvotes: 0

arturdev
arturdev

Reputation: 11039

Use this method to calculate your top view controller.

+ (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController
{
    if ([rootViewController isKindOfClass:[UITabBarController class]]) {
        UITabBarController* tabBarController = (UITabBarController*)rootViewController;
        return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
    } else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
        UINavigationController* navigationController = (UINavigationController*)rootViewController;
        return [self topViewControllerWithRootViewController:navigationController.visibleViewController];
    } else if (rootViewController.presentedViewController) {
        UIViewController* presentedViewController = rootViewController.presentedViewController;
        return [self topViewControllerWithRootViewController:presentedViewController];
    } else {
        return rootViewController;
    }
}

Upvotes: 1

Bhumit Mehta
Bhumit Mehta

Reputation: 16318

I think since UIAlertView is now a UIAlertViewController since iOS8 , may be the problem is that you need to wait till the alert is dismissed and than push a new controller you can do like

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 0)
{

}
if(buttonIndex == 1)
{
    [self performSelector:@selector(showNewContact) withObject:nil afterDelay:1.0];
}
}

This way first alert will be dismissed and than new VC will be pushed.

Upvotes: 0

Related Questions