如果可能的话,我正在寻找应用代理重定向回主要故事,但我仍然偶然发现应用委托方法无效的错误。
AppDelegate.h
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate,UIAlertViewDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
AppDelegate.m
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Daily Vibes"
message:notification.alertBody
delegate:self cancelButtonTitle:@"Okay"
otherButtonTitles:nil];
[alert show];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:@"Okay"])
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *mainViewController = [storyboard instantiateInitialViewController];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = mainViewController;
[self.window makeKeyAndVisible];
}
}
我想知道点击UIAlertView时可以将它们重定向回主板
答案 0 :(得分:2)
只需在现有窗口上设置根视图控制器即可。您不需要创建新窗口,也不需要将其设置为关键且可见。
如果您想获得幻想,可以使用UIView转换或CATransition为转换设置动画。
这似乎是一种不寻常的尝试,但是 - 您将丢失任何状态或未保存的数据,这可能对您的用户来说很奇怪。
答案 1 :(得分:1)
如果要呈现视图控制器,请尝试以下操作:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:@"Okay"])
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateInitialViewController];
[self.window.rootViewController presentViewController:vc animated:YES completion:nil]
}
}