我正在尝试扩展iPad的iPhone应用程序,但现在遇到以下错误

[MasterViewController topViewController]: unrecognized selector sent to instance

我研究了所有关于类似问题的文章。他们都不符合我的情况。由于我复制了我做得好的教程中的代码,因此我确信代码必须正确。
请阅读以下内容:
    // TAB BAR
    UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;

    // Override point for customization after application launch.
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
    {
        NSLog(@"I'm an iPad");

        // Set up SPLIT VIEW
        UISplitViewController *splitViewController = [[tabBarController viewControllers] objectAtIndex:0];

        // Set up splitted MASTER view
        UINavigationController *accountsMasterTVCnav = [splitViewController.viewControllers objectAtIndex:0];
        splitViewController.delegate = (id)accountsMasterTVCnav.topViewController;
        MasterViewController *accountsTVC = [[accountsMasterTVCnav viewControllers] objectAtIndex:0];
        accountsTVC.managedObjectContext = self.managedObjectContext;

        // Set up Split View DETAIL view
        UINavigationController *accountsVCnav = [splitViewController.viewControllers objectAtIndex:1];
        AccountsVC *accountsVC = [accountsVCnav.viewControllers objectAtIndex:0];
}

这应该只是第一个拆分视图。 iPhone版本具有4个表格视图,所有这些视图应在iPad版本中分开显示。
如何以简单的方式解决该问题?

任何帮助将不胜感激...在此先感谢!

编辑1:

我在主控制器和明细控制器中更新了委托指针。还是一样的结果(((
添加MasterVieWController.h内容...
#import <UIKit/UIKit.h>
#import "Accounts.h"
#import "AccountsMasterDelegate.h"
#import "AccountsDetailDelegate.h"

@interface MasterViewController : UITableViewController <NSFetchedResultsControllerDelegate, UISearchBarDelegate, UISearchDisplayDelegate, AccountsDetailDelegate>

@property (strong,nonatomic) NSMutableArray *filteredAccountsArray;
@property IBOutlet UISearchBar *accountsSearchBar;
@property (nonatomic, weak) id <AccountsMasterDelegate> delegate;
@property (nonatomic, strong) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
@property (strong, nonatomic) Accounts *selectedAccount;

@end

下面的完整错误...
2014-05-24 13:41:02.114 Accounts[35652:60b] -[MasterViewController topViewController]: unrecognized selector sent to instance 0x10c187d50
2014-05-24 13:41:02.117 Accounts[35652:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MasterViewController topViewController]: unrecognized selector sent to instance 0x10c187d50'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000102d85495 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001029ef99e objc_exception_throw + 43
    2   CoreFoundation                      0x0000000102e1665d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x0000000102d76d8d ___forwarding___ + 973
    4   CoreFoundation                      0x0000000102d76938 _CF_forwarding_prep_0 + 120
    5   Accounts                            0x000000010001c1de -[AppDelegate application:didFinishLaunchingWithOptions:] + 2142
    6   UIKit                               0x0000000100ff53d9 -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 264
    7   UIKit                               0x0000000100ff5be1 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1605
    8   UIKit                               0x0000000100ff9a0c -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 660
    9   UIKit                               0x000000010100ad4c -[UIApplication handleEvent:withNewEvent:] + 3189
    10  UIKit                               0x000000010100b216 -[UIApplication sendEvent:] + 79
    11  UIKit                               0x0000000100ffb086 _UIApplicationHandleEvent + 578
    12  GraphicsServices                    0x00000001033fb71a _PurpleEventCallback + 762
    13  GraphicsServices                    0x00000001033fb1e1 PurpleEventCallback + 35
    14  CoreFoundation                      0x0000000102d07679 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 41
    15  CoreFoundation                      0x0000000102d0744e __CFRunLoopDoSource1 + 478
    16  CoreFoundation                      0x0000000102d30903 __CFRunLoopRun + 1939
    17  CoreFoundation                      0x0000000102d2fd83 CFRunLoopRunSpecific + 467
    18  UIKit                               0x0000000100ff92e1 -[UIApplication _run] + 609
    19  UIKit                               0x0000000100ffae33 UIApplicationMain + 1010
    20  Accounts                            0x0000000100013793 main + 115
    21  libdyld.dylib                       0x0000000103f3d5fd start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

如有其他必要信息,请告诉我。
感谢你的帮助!

@Matteo的更新:
// Set up splitted MASTER view
        MasterViewController *mvc = [splitViewController.viewControllers objectAtIndex:0];
        UINavigationController *accountsMasterTVCnav = mvc.navigationController;
//        UINavigationController *accountsMasterTVCnav = [splitViewController.viewControllers objectAtIndex:0];
        splitViewController.delegate = (id)accountsMasterTVCnav.topViewController;
//        MasterViewController *accountsTVC = [[accountsMasterTVCnav viewControllers] objectAtIndex:0];
        mvc.managedObjectContext = self.managedObjectContext;

最佳答案

注意:很酷,请阅读所有文章以了解可能发生的事情。但是在这种情况下,情况就是UPDATE 3。

这就是问题:

(id)accountsMasterTVCnav.topViewController

首先,不需要(id)强制转换。

其次,错误表明您的accountsMasterTVCnav类型为MasterViewController,并且没有名为@property的选择器(在您的情况下为topViewController)。

因此,问问自己:
  • 我在哪个类别中拥有该属性?
  • 也许我希望accountsMasterTVCnav是该类的类型?

  • 更新:

    我正在看你的代码:
    UINavigationController *accountsMasterTVCnav = [splitViewController.viewControllers objectAtIndex:0];
    splitViewController.delegate = (id)accountsMasterTVCnav.topViewController;
    
    topViewControllerUINavigationController的属性,但是当您这样做时:
    UINavigationController *accountsMasterTVCnav = [splitViewController.viewControllers objectAtIndex:0];
    

    您猜测返回的实例类型为UINavigationController,而不是类型MasterViewController

    因此,在splitViewController.viewControllers中,UINavigationController实例不在索引0 处。您必须设置正确的索引。我不知道你的viewController hierarchy

    更新2:

    在下面的评论中观看您的屏幕截图,请尝试以下操作:
    MasterViewController *masterViewController = [splitViewController.viewControllers objectAtIndex:0];
    UINavigationController *accountsMasterTVCnav = masterViewController.navigationController;
    

    还有另一件事。您的代码在applicationDidFinishLaunching中?那么,为什么需要访问topViewController?肯定是第一个视图控制器,因此是MasterViewController。

    因此,如果返回的实例是MasterViewController,则只需执行以下操作:
    splitViewController.delegate = [splitViewController.viewControllers objectAtIndex:0];
    

    更新3:

    审查您的项目,并进行一些测试,我想在情节提要中可能存在一个错误。这是原因之一,因为我不使用情节提要。

    在您的代码中,您有:
    TabBarController
        SplitViewController
            UINavigationController *
                MasterViewController
            UINavigationController
        UINavigationController *
        UINavigationController *
        UINavigationController *
    

    但是发生的是,它需要4个NavigationController(请参阅上面的*****):

    因此,当您这样做时:
    UISplitViewController *splitViewController = [[tabBarController viewControllers] objectAtIndex:0];
    

    您正在获取UINavigationController实例。.(第一个*)并执行以下操作:
    [splitViewController.viewControllers objectAtIndex:0];
    

    显然,您可以直接获取MasterViewController

    因此,当您这样做时:
    [splitViewController.viewControllers objectAtIndex:1];
    

    它崩溃..因为在导航堆栈中只有masterViewController

    您已正确将viewControllers连接到tabViewController
    我认为这是一个Apple错误...真荒谬。

    我的建议是用NIB或代码重写所有内容。.我知道这很昂贵。.也可以在网上查看有关此问题的信息。

    请享用!

    关于ios - 标签栏>拆分> NavigationController在[MasterViewController topViewController]中产生结果:无法识别的选择器发送到实例:核心数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23840211/

    10-10 11:50