1

I have created a dictionary application and I am using UISplitViewController to search and select a word, my application crashes with this log :

-[MeaningViewController topViewController]: unrecognized selector sent to instance 0x7f918945bef0

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MeaningViewController topViewController]: unrecognized selector sent to instance 0x7f918945bef0'

Here is my code : in ViewController (MasterView) :

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender   {

    [_searchbar resignFirstResponder];

    if ([segue.identifier isEqualToString:@"Meaning"]) {

//MeaningViewController *meaningVC = (MeaningViewController*)[[segue destinationViewController] topViewController];

        UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
        MeaningViewController *meaningVC = (MeaningViewController *)navController.topViewController;


        NSIndexPath*indexPath = _tableview.indexPathForSelectedRow;




        AppDelegate *appDelegateClass = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        Reader *selectedWord = [[appDelegateClass wordList] objectAtIndex:indexPath.row];

        sqlite3 *database;
        NSString *mean = @"";

        if (sqlite3_open([appDelegateClass.currentDBPath cStringUsingEncoding:NSUTF8StringEncoding], &database) == SQLITE_OK) {

            NSString *sql =[NSString stringWithFormat:@"SELECT MEAN from DICM WHERE id = %ld",(long)selectedWord.oid];
            sqlite3_stmt *compiledStatement;

            if(sqlite3_prepare_v2(database, [sql UTF8String] , -1, &compiledStatement, NULL) == SQLITE_OK) {
                while(sqlite3_step(compiledStatement) == SQLITE_ROW) {


                    const char* f1 = (const char*)sqlite3_column_text(compiledStatement, 0);
                    mean = f1 == NULL ? nil : [[NSString alloc] initWithUTF8String:f1];
                }}}


        readerClass = (Reader *)[appClass.wordList objectAtIndex:indexPath.row];

        [meaningVC setGetWord:readerClass.Name];
        [meaningVC setGetMeaning:mean];
        [meaningVC setGetOid:readerClass.oid];

        dbClass=[[DB alloc]init];
        [dbClass setViewTime:readerClass.oid];

        NSLog(@"mean is %@",readerClass.Name);
    }
}

enter image description here

and in MeaningViewController (Detail) :

 - (void)viewDidLoad {

        [super viewDidLoad];
        // Do any additional setup after loading the view.

        [_word setText:_getWord];
        [_mean setText:_getMean];
        oid = _getOid;
    }

The problem comes with this line :

UINavigationController *navController = (UINavigationController *)segue.destinationViewController; MeaningViewController *meaningVC = (MeaningViewController *)navController.topViewController;

If I replace above lines with this :

MeaningViewController *meaningVC = (MeaningViewController*)segue.destinationViewController;

It works fine but it doesn't replace the detail view it just push view controller on Master View How can I fix this problem ?

here is AppDelegate codes :

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
    splitViewController.preferredDisplayMode = UISplitViewControllerDisplayModeAllVisible;
    splitViewController.delegate = self;
return YES;

}


    - (BOOL)splitViewController:(UISplitViewController *)splitViewController collapseSecondaryViewController:(UIViewController *)secondaryViewController ontoPrimaryViewController:(UIViewController *)primaryViewController
    {

        return YES;
    }
rmaddy
  • 314,917
  • 42
  • 532
  • 579
iOS.Lover
  • 5,923
  • 21
  • 90
  • 162
  • 1
    The segue's destination controller is already your `MeaningViewController`, not a navigation controller. – rmaddy May 22 '16 at 23:04
  • @rmaddy What about this : `MeaningViewController *meaningVC = (MeaningViewController*)[[segue destinationViewController] topViewController];` still crashes ! – iOS.Lover May 24 '16 at 07:57
  • You want: `MeaningViewController *meaningVC = (MeaningViewController*)[segue destinationViewController] ;` – rmaddy May 24 '16 at 15:44
  • @rmaddy I did but this happens : `http://i.imgur.com/8RH4frL.png` The meaning view replaced with Master instead of Detail – iOS.Lover May 24 '16 at 15:51

1 Answers1

0

One problem that I can see is that your DetailViewController is not embedded in its own UINavigationController. and your storyboard flow will be like:

->SplitVCont--UINavCont1--MasterVController--[O]--UINavCont2--DetailVController

->SplitVCont--UINavCont2--DetailViewController

Storyboard

with this, in your prepareForSegue the segue.destinationViewController is guaranteed to be in all cases(collapse/split and in all devices) a kind of UINavigationController-class or you can Introspection to be safe.

[segue.destinationViewController isKindOfClass:[UINavigationController class]]  

-In some cases DetailViewController will be pushed to the NavigationController of MasterViewController (case iPhone 5 )

-In other cases DetailViewController will be in its own NavigationController and the splitViewController will handle all transitions of displayMode or DeviceOrientation using its default behaviour in your situation.

Idali
  • 1,023
  • 7
  • 10
  • Thank you I just add another `Navigation Controller` for Detail View and it doesn't crash now but still have this problem : `ttp://i.imgur.com/8RH4frL.png` – iOS.Lover May 28 '16 at 12:32
  • you welcome, try to tests using iPhone5 simulator see if you can see standard master-detail behaviour..if it does't work then its related to passing data to Detail and nothing to do with splitView – Idali May 28 '16 at 13:55
  • I see your image, so check your delegate : if ([self.window.rootViewController isKindOfClass:[UISplitViewController class]]) { UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController; splitViewController.preferredDisplayMode = UISplitViewControllerDisplayModeAllVisible; splitViewController.delegate = self; } – Idali May 28 '16 at 14:10
  • the delegate get set? i mean [self.window.rootViewController isKindOfClass:[UISplitViewController class]] is true – Idali May 28 '16 at 15:53
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/113221/discussion-between-idali-and-mc-lover). – Idali May 28 '16 at 16:08