gpt4 book ai didi

ios - 通过 UIAlertview 处理推送通知

转载 作者:行者123 更新时间:2023-11-29 12:20:04 25 4
gpt4 key购买 nike

接收推送通知并尝试通过警报 View 进行处理,并在选择警报 View 按钮时想要导航到特定的 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
}
}

我想在点击显示按钮时移动到另一个 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];

}

接收错误:由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[_UIModalItemsPresentingViewController pushViewController:animated:]: 无法识别的选择器发送到实例 0x169f5f10”*** 首先抛出调用栈:

请指导。

更新:

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

最佳答案

使用此方法计算您的顶 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;
}
}

关于ios - 通过 UIAlertview 处理推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30908727/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com