Having issue with UISplitViewController

喜欢而已 提交于 2019-12-13 02:09:14

问题


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);
    }
}

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;
    }

回答1:


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

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.



来源:https://stackoverflow.com/questions/37377825/having-issue-with-uisplitviewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!