0

首先,如果这是,不知何故,一个重复的帖子,请随时将我指向正确的帖子,因为经过数小时的搜索我还没有找到它。

我在我的应用程序中使用 MasterDetail viewController,在开发的第一周左右,除了默认设置之外,没有其他 ViewVontrollers 或 segues。我写了我的主要代码,主视图控制器和细节视图控制器工作得很好。一旦我从 Detail View 添加了另一个带有 push segue 的 VC,我的应用程序立即崩溃。这是错误:

***Terminating app due to uncaught exception 'NSInvalidArgumentException', reason '-[UINavigationController setPlayer:]: unrecognized selector sent to instance ...'然后是一堆十六进制。

在 AppDelegate.m 中,如果我注释掉这一行:

rightViewController.delegate = rightViewController

然后应用程序将启动,push segue 将工作,但现在,显然,如果我要在 MasterView 中选择一个单元格,它会崩溃并给出以下错误:

***Terminating app due to uncaught exception 'NSInvalidArgumentException', reason '-[UINavigationController selectedPlayer:]: unrecognized selector sent to instance ...'然后是一堆十六进制。

以下是我认为相关的所有代码:

AppDelegate.m

#import "AppDelegate.h"
#import "LeftViewController.h"
#import "RightViewController.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
    UINavigationController *leftNavController = [splitViewController.viewControllers objectAtIndex:0];
    LeftViewController *leftViewController = (LeftViewController *)[leftNavController topViewController];
    RightViewController *rightViewController = [splitViewController.viewControllers objectAtIndex:1];

    Player *selectedPlayer = [[leftViewController preclears]objectAtIndex:0];
    [rightViewController setPlayer:selectedPlayer];

    leftViewController.delegate = rightViewController;
    return YES;
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate.   See also applicationDidEnterBackground:.
}

@end

LeftViewController.m(部分)

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    //Re-fetch the feed from the Postgres Database when a user selects an entry

    [JSONHTTPClient getJSONFromURLWithString:@"http://myurl" completion:^(NSDictionary *json, JSONModelError *err) {
    NSError* error = nil;
    _feed = [[PostgresFeed alloc] initWithDictionary:json error:&error];

    //Print the data fethced to NSLog in JSON format

    [[NSNotificationCenter defaultCenter] postNotificationName:@"myNotification" object:nil userInfo:[[json objectForKey:@"player"] objectAtIndex:indexPath.row]];

}];

Player *selectedPlayer = [_players objectAtIndex:indexPath.row];

if (_delegate) 
    {
        [_delegate selectedPlayer:selectedPlayer];
    }
}

所以,我做错了什么,但我无法弄清楚它是什么。我已经做了很多谷歌搜索,但还没有找到答案。如果有人想知道,我是 iOS 和 Obj C 的新手,MasterDetail 应用程序是基于 iPad 拆分视图的 Ray Wenderlich 教程。我还查看了一些关于 segues 的 Scott Sherwood 教程,但在那里没有找到任何答案。

让我知道是否需要更多代码。

4

1 回答 1

1

错误信息

-[UINavigationController setPlayer:]: unrecognized selector ...

表示

RightViewController *rightViewController = [splitViewController.viewControllers objectAtIndex:1];

返回一个UINavigationController实例,而不是RightViewController预期的实例。解决方案取决于视图控制器层次结构的结构。可能您必须进行类似于左视图控制器的操作:

UINavigationController *rightNavController = [splitViewController.viewControllers objectAtIndex:1];
RightViewController *rightViewController = (RightViewController *)[rightNavController topViewController];
于 2013-07-06T19:37:43.980 回答