3

我是一个新手iOS开发人员,最近开发了几个Android应用程序,但我不熟悉iOS术语。让我解释一下我的问题。

我想使用两个不同的UIViewController. 我已经为两个控制器创建了.h文件.m。我的计划是在第一个视图控制器出现在屏幕上五秒钟后将第二个视图控制器推到第一个视图控制器的顶部。我的意思是第一个视图控制器类似于闪屏或类似的东西。

这是我的贡献。在第一个视图控制器中,我定义了(当然其中一个实现了)这两种方法:

-(void) pushSecondController {
    SecondViewController *secondController = [[SecondViewController alloc]
                                              initWithNibName: nil
                                              bundle: NULL];
    [self.navigationController pushViewController: secondController animated: YES];
}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self performSelector: @selector(pushViewController:animated:)
                withObject: nil
                afterDelay: 5.0f];
}

第二个视图控制器如下所示:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"Second Controller";
}

我只改变了viewDidLoad方法。当我运行模拟器时,第一个视图控制器运行良好,等待 5 秒后崩溃。输出如下所示:

2012-08-24 10:46:34.104 NavApplication[20355:f803] -[ViewController pushViewController:]: unrecognized selector sent to instance 0x6e7f780
2012-08-24 10:46:34.107 NavApplication[20355:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ViewController pushViewController:]: unrecognized selector sent to instance 0x6e7f780'

methodName让我再问一个问题:我知道和之间存在差异methodName:。谁能解释一下有什么区别?

任何帮助,将不胜感激。

更新:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
    [self.window makeKeyAndVisible];
    [self.window addSubview: self.navigationController.view];
    return YES;
}
4

3 回答 3

3

更改@selector(pushViewController:animated:)@selector(pushSecondController)

于 2012-08-24T08:22:28.777 回答
3

如上所述,您可能希望将performSelector命令更改为:

[self performSelector: @selector(pushSecondController)
            withObject: nil
            afterDelay: 5.0f];

因为您希望它调用您的pushSecondController方法,而不是pushViewController:animated:.

methodName关于您的第二个问题:和之间的区别在于methodName:末尾:methodName:表示此方法带有参数。所以,你可以有以下方法:

- (void)listItems
{
    // ...
}

- (void)insertItem:(NSDictionary *)item
{
    // ...
}

将它们的引用传递给 时@selector,对于第一种方法,您只需执行@selector(listItems),因为它不带参数,而对于后者,您会这样做,@selector(insertItem:)因为它带有参数。

更新

刚看到你的applicationDidLaunch代码。您可能想要重新排列事物,以便将您添加ViewController到您的UINavigationController中,然后将 设置UINavigationController为窗口的 rootViewController。像这样:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.

    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];

    self.window.rootViewController = self.navigationController;

    [self.window makeKeyAndVisible];
    return YES;
}
于 2012-08-24T08:30:30.340 回答
0

请将您的 didFinishLaunchingWithOptions 方法更改为:

   - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        // Override point for customization after application launch.
        self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
        self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];

            self.window.rootViewController = self.navigationController;

            [self.window makeKeyAndVisible];

            return YES;
    }
于 2012-08-24T08:32:55.817 回答