标签栏>拆分> NavigationController导致[MasterViewController topViewController]:无法识别的选择器发送到实例:核心数据

时间:2014-05-24 00:51:33

标签: ios iphone objective-c ipad core-data

我正在尝试扩展我的iPhone-Application for iPad,但现在我的头脑出现以下错误

[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个tableviews,这些都应该在iPad版本中分割显示。 如何以简单的方式解决这个问题?

任何帮助都将受到高度赞赏......提前致谢!

EDIT1:

我更新了Master和Detail控制器中的委托指针。结果仍然相同((( 添加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;

1 个答案:

答案 0 :(得分:1)

注意:很酷的阅读所有帖子,了解可能会发生什么。但在这种特殊情况下,情况是更新3。

这是问题所在:

(id)accountsMasterTVCnav.topViewController

首先,(id)演员不需要。

其次,错误表明您的accountsMasterTVCnav类型为MasterViewController且它没有选择器(在您的情况下是@property),称为{{1} }}

所以问问自己:

  • 我在哪个班级有这个属性?
  • 也许我希望topViewController是该类的类型?

<强>更新

我在看你的代码:

accountsMasterTVCnav

UINavigationController *accountsMasterTVCnav = [splitViewController.viewControllers objectAtIndex:0]; splitViewController.delegate = (id)accountsMasterTVCnav.topViewController; topViewController的属性,但是当你这样做时:

UINavigationController

您猜测返回的实例属于UINavigationController *accountsMasterTVCnav = [splitViewController.viewControllers objectAtIndex:0]; 类型,而是UINavigationController类型。

因此MasterViewController splitViewController.viewControllers实例不在索引0 。您必须设置正确的索引。我不了解你的UINavigationController

更新2:

在下面的评论中观看您的屏幕截图,请尝试以下操作:

viewController hierarchy

另一件事。您的代码位于MasterViewController *masterViewController = [splitViewController.viewControllers objectAtIndex:0]; UINavigationController *accountsMasterTVCnav = masterViewController.navigationController; ?那你为什么需要访问topViewController呢?它肯定是第一个视图控制器,所以MasterViewController。

因此,如果返回的实例是applicationDidFinishLaunching,您只需执行以下操作:

MasterViewController

更新3:

审核您的项目,并进行一些测试我猜在故事板中可能存在错误。这是其中一个原因,因为我不使用故事板。

在您的代码中,您有:

splitViewController.delegate = [splitViewController.viewControllers objectAtIndex:0];

但是发生了什么需要4 TabBarController SplitViewController UINavigationController * MasterViewController UINavigationController UINavigationController * UINavigationController * UINavigationController * (参见上面的*****):

enter image description here

出于这个原因:

NavigationController

您正在获取UISplitViewController *splitViewController = [[tabBarController viewControllers] objectAtIndex:0]; 个实例.. (第一个*)并且正在执行:

UINavigationController

显然你直接获得了[splitViewController.viewControllers objectAtIndex:0];

因此,当你这样做时:

MasterViewController

它崩溃了......因为导航堆栈中只有[splitViewController.viewControllers objectAtIndex:1];

您已将masterViewController正确关联到viewControllers。 我认为这是一个Apple漏洞......非常荒谬。

我的建议是用NIB或代码重写所有内容..我知道这很贵......还可以在网上看看这个问题。

享受!