NSArrayM replaceObjectAtIndex:withObject:object不能为nil'

时间:2014-11-24 02:16:33

标签: ios objective-c xcode5

我有一个项目与this相同的菜单,但子菜单是相同的幻灯片动画。我创建一个xib文件名称为SecondMenu.xib。文件所有者是UIViewController并将其命名为SecondMenuController。如果检查项目REFrosted(check the link),则有DEMOMenuViewController(UIViewController)。在方法didSelectRowAtIndexPath:。我在我的代码中更改了它

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSMutableArray *controllers = [[NSMutableArray alloc] init];
    for (unsigned i = 0; i < 1; i++) {
        [controllers addObject:[NSNull null]];
    }
    self.viewControllers = controllers;
    SecondMenuController  *controller = [self.viewControllers objectAtIndex:0];

    if ((NSNull *)controller == [NSNull null]) {
        controller = [[SecondMenuController  alloc] initWithNibName:@"SecondMenu" bundle:nil];
        [self.viewControllers replaceObjectAtIndex:0 withObject:controller];

    }

    controller.view.frame = CGRectMake(-self.view.frame.size.width, 0,self.view.frame.size.width,self.view.frame.size.height);

    [self.view addSubview:controller.view];


    [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        controller.view.frame = CGRectMake(0, 0,self.view.frame.size.width,self.view.frame.size.height);
    } completion:^(BOOL finished) {
        NSLog(@"Done!");
    }];

    //[self hideMenu];

}

当连接到子菜单(即SecondMenuController)时,该代码对我有用。在我的SecondMenuController中,我添加了一个按钮(这将连接到DEMOHomeViewController,故事板标识符是homeController)。这是我按钮的代码

- (IBAction)buttonConnect:(id)sender {
    DEMONavigationController *navigationController = [self.storyboard instantiateViewControllerWithIdentifier:@"contentController"];

    DEMOHomeViewController *homeViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"homeController"];
    navigationController.viewControllers = @[homeViewController];

    self.frostedViewController.contentViewController = navigationController;
    [self.frostedViewController hideMenuViewController];
}

我在REFrosted中获取此代码,然后将其更改为didSelectRowAtIndexPath中的代码:(正如我在上面的第一段代码中提到的那样)。为什么我会收到此错误

'NSInvalidArgumentException', reason: '*** -[__NSArrayM replaceObjectAtIndex:withObject:]: object cannot be nil'

我导入所有文件。正如我所理解的,当您导入连接到文件所有者的文件时。这意味着当你将它实例化到我的SecondMenuController时,我可以控制该文件中的每个方法或函数(这只是基于我的经验,如果我错了就纠正我。我只是想知道系统是如何工作的)。请看我的代码。我在这里错过了什么吗?希望你能告诉我如何解决这个或解释为什么我会收到这个错误。

    DEMONavigationController *navigationController = [self.storyboard instantiateViewControllerWithIdentifier:@"contentController"];

    DEMOHomeViewController *homeViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"homeController"];
    navigationController.viewControllers = @[homeViewController];

2 个答案:

答案 0 :(得分:0)

为此寻找答案2天。我终于明白了。

单击按钮时DEMOHomeViewController为零的原因是因为SecondMenuController没有连接到DEMOHomeViewController,即使我将它实例化为SecondMenuController类。我现在学习。这是代码

我做的第一个代码。我添加了这个SecondMenuController.h

@property (nonatomic, weak) UIStoryboard * myself;
@property (nonatomic, strong) REFrostedViewController * ref;   
 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil myStoryBoard:(UIStoryboard *) xStoryBoard ref: (id) xRef;

在SecondMenuController.m中

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil myStoryBoard:(UIStoryboard *) xStoryBoard ref: (id) xRef
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.myself = xStoryBoard;
        self.ref = xRef;
    }
    return self;
}

在我的按钮

DEMONavigationController *navigationController = [self.myself instantiateViewControllerWithIdentifier:@"contentController"];

DEMOSecondViewController *secondViewController = [self.myself instantiateViewControllerWithIdentifier:@"homeController"];

navigationController.viewControllers = @[secondViewController];


self.ref.frostedViewController.contentViewController = navigationController;
    [self.ref.frostedViewController hideMenuViewController];

现在它正在运作。您需要做的就是将initWithNibName:方法调用到DEMOMenuViewController。

答案 1 :(得分:0)

控制台日志意味着您尝试在NSArray中插入nil对象,这是不允许的。如果由于某种原因需要添加空对象,请使用(NSArray *)[NSNull null]