首先,如果这是一个不知何故的dup帖子,请随时指出我正确的帖子,因为我在搜索数小时后仍未找到它。
我在我的应用程序中使用了MasterDetail视图控制器,在开发的第一周左右,除了默认值之外,没有其他ViewVontrollers或segues。我编写了主代码,主视图和细节视图控制器运行良好。一旦我从详细信息视图中添加了另一个带有推送 segue 的 VC,我的应用程序就会立即崩溃。这是错误:
***Terminating app due to uncaught exception 'NSInvalidArgumentException', reason '-[UINavigationController setPlayer:]: unrecognized selector sent to instance ...'
,然后是一堆十六进制。
在AppDelegate.m中,如果我注释掉这一行:
rightViewController.delegate = rightViewController
然后应用程序将启动并且推送 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 (part(
#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 SplitViews的Ray Wenderlich教程。我还查看了一些关于 segues 的 Scott Sherwood 教程,但在那里没有找到任何答案。
让我知道是否需要更多代码。
错误消息
-[UINavigationController setPlayer:]: unrecognized selector ...
表示
RightViewController *rightViewController = [splitViewController.viewControllers objectAtIndex:1];
返回一个UINavigationController
实例,而不是一个RightViewController
实例预期。解决方案取决于视图控制器层次结构的结构。您可能必须与左侧视图控制器类似地进行操作:
UINavigationController *rightNavController = [splitViewController.viewControllers objectAtIndex:1];
RightViewController *rightViewController = (RightViewController *)[rightNavController topViewController];