首先,如果这是一个重复的帖子,请随时指出正确的帖子,因为我在搜索数小时后没有找到它。
我在我的应用程序中使用MasterDetail viewController,在开发的第一周左右,没有其他ViewVontrollers或除默认值之外的segue。我编写了我的主要代码,Master和Detail viewController工作正常。一旦我从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 SplitViews的Ray Wenderlich教程。我还查看了一些关于segues的Scott Sherwood教程,但是没有找到任何答案。
如果需要更多代码,请告诉我。
答案 0 :(得分:1)
错误消息
-[UINavigationController setPlayer:]: unrecognized selector ...
表示
RightViewController *rightViewController = [splitViewController.viewControllers objectAtIndex:1];
返回UINavigationController
实例,而不是RightViewController
实例
预期。解决方案取决于视图控制器层次结构的结构。
可能您必须继续类似于左视图控制器:
UINavigationController *rightNavController = [splitViewController.viewControllers objectAtIndex:1];
RightViewController *rightViewController = (RightViewController *)[rightNavController topViewController];