Reputation:
First of all, if this is, somehow, a dup post, feel free to point me to the correct one because I have not found it after hours of searching.
I am using a MasterDetail viewController in my app, which for the first week or so of development, had no additional ViewVontrollers or segues other than the default. I wrote my main code, and the Master and Detail viewController are working perfectly. As soon as I added another VC with a push segue from the Detail View, my app crashes instantly. This is the error :
***Terminating app due to uncaught exception 'NSInvalidArgumentException', reason '-[UINavigationController setPlayer:]: unrecognized selector sent to instance ...'
and then a bunch of hex.
In the AppDelegate.m, if I comment out this line:
rightViewController.delegate = rightViewController
Then the app will start up and the push segue will work, but now, obviously, if I were to select a cell in the MasterView, it would crash giving this error:
***Terminating app due to uncaught exception 'NSInvalidArgumentException', reason '-[UINavigationController selectedPlayer:]: unrecognized selector sent to instance ...'
and then a bunch of hex.
Here is all of the code that I think is relevant:
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];
}
}
So, I am doing something wrong, but I can not figure out what it is. I have done a lot of googling and have not found an answer yet. In case someone wants to know, I am new to iOS and Obj C, and the MasterDetail app was based off of the Ray Wenderlich tutorial for iPad SplitViews. I have also checked out some of the Scott Sherwood tutorials on segues, but have not found any answers there.
Let me know if any more code is needed.
Upvotes: 0
Views: 551
Reputation: 539815
The error message
-[UINavigationController setPlayer:]: unrecognized selector ...
indicates that
RightViewController *rightViewController = [splitViewController.viewControllers objectAtIndex:1];
returns a UINavigationController
instance, and not a RightViewController
instance as
expected. The solution depends on the structure of your view controllers hierarchy.
It may be that you have to proceed similar to the left view controller:
UINavigationController *rightNavController = [splitViewController.viewControllers objectAtIndex:1];
RightViewController *rightViewController = (RightViewController *)[rightNavController topViewController];
Upvotes: 1