无法识别的选择器发送到实例,pushViewController:动画

时间:2014-09-12 14:33:42

标签: ios uiviewcontroller unrecognized-selector

我不知道如何解决这个问题:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ViewController pushViewController:animated:]: unrecognized selector sent to instance 0x8e5fc70'

此问题与屏幕超时有关。没有活动30秒后,我的应用程序必须返回登录屏幕,如果用户回来,他必须再次登录。这是我的代码:

AppDelegate.h: 

#import <UIKit/UIKit.h> 

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@end


AppDelegate.m:
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidTimeout:) name:kApplicationDidTimeoutNotification object:nil];

    return YES;
}

-(void)applicationDidTimeout:(NSNotification *) notif
{
    NSLog (@"time exceeded!!");
    UIViewController *controller = [[UIStoryboard storyboardWithName:@"Main" bundle:NULL] instantiateViewControllerWithIdentifier:@"mainView"];

    [(UINavigationController *)self.window.rootViewController pushViewController:controller animated:YES];

正如您所看到的,问题出现在此代码的最后一行。我使用的这部分代码取自: iOS perform action after period of inactivity (no user interaction)

2 个答案:

答案 0 :(得分:1)

pushViewController:animated:方法是UINavigationController的方法。

但您的根视图控制器属于ViewController类型,很可能是UIViewController

您需要更改设置,以便主视图控制器位于UINavigationController内,并且您需要将导航控制器设置为窗口的根视图控制器。

答案 1 :(得分:0)

这意味着在storyboard中,rootViewController是一个navigationController sample