2014-04-02 71 views
1

我正在研究一个应用程序,在其主视图中显示rss提要。它有一个详细视图,显示该rss供稿项目的web视图。我正在向左侧添加一个滑出式导航栏,该导航栏将打开。当我尝试启动它时,我的应用程序不断崩溃。这是我的代码。我还使用了一个名为SWRevealViewController的第三方库,它提供了滑出机制。*** - [__ NSArrayM insertObject:atIndex:]:object can not be nil?我一直得到这个错误

我的MasterViewController.h有一个插座和它的切换滑出菜单。我宣称它是“sidebarButton”。

这是我MasterViewController.m

#import "JSSMasterViewController.h" 
#import "SWRevealViewController.h" 
#import "JSSDetailViewController.h" 

@interface JSSMasterViewController() { 
    NSMutableArray *_objects; 
} 
@end 

@implementation JSSMasterViewController 

- (void)awakeFromNib 
{ 
    [super awakeFromNib]; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    self.navigationItem.leftBarButtonItem = self.editButtonItem; 

    // Change button color 
    _sidebarButton.tintColor = [UIColor colorWithWhite:0.96f alpha:0.2f]; 

    // Set the side bar button action. When it's tapped, it'll show up the sidebar. 
    _sidebarButton.target = self.revealViewController; 
    _sidebarButton.action = @selector(revealToggle:); 

    // Set the gesture 
    [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (void)insertNewObject:(id)sender 
{ 
    if (!_objects) { 
     _objects = [[NSMutableArray alloc] init]; 
    } 
    [_objects insertObject:[NSDate date] atIndex:0]; 
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
} 

#pragma mark - Table View 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return _objects.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 

    NSDate *object = _objects[indexPath.row]; 
    cell.textLabel.text = [object description]; 
    return cell; 
} 

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Return NO if you do not want the specified item to be editable. 
    return YES; 
} 

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     [_objects removeObjectAtIndex:indexPath.row]; 
     [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } else if (editingStyle == UITableViewCellEditingStyleInsert) { 
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view. 
    } 
} 

/* 
// Override to support rearranging the table view. 
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath 
{ 
} 
*/ 

/* 
// Override to support conditional rearranging of the table view. 
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Return NO if you do not want the item to be re-orderable. 
    return YES; 
} 
*/ 

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

} 

@end 

构建总是成功的,但是当应用程序在iPhone模拟器启动它崩溃。

似乎获得错误的部分是下面的代码:

- (void)insertNewObject:(id)sender 
{ 
    if (!_objects) { 
     _objects = [[NSMutableArray alloc] init]; 
    } 
    [_objects insertObject:[NSDate date] atIndex:0]; 
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
} 

的地方,断点保持停止是:

// Set the gesture 
    [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer]; 

,我的控制台日志是唯一的错误:

*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil 

有什么我在这里做错了吗? 感谢您的帮助!

+0

你应该添加一个异常断点来定位哪一行导致错误。 – rdelmar

+0

我在这页上输入的第三块代码就是发生错误的地方...... @rdelmar –

回答

0

该错误消息告诉您self.revealViewController.panGestureRecognizer为零,这可能是由self.revealViewController为零引起的。

+0

我如何让它不零。对不起,我是iOS的初学者。 –

+0

你必须看看'SWRevealViewController'的文档,看看你应该如何实例化和初始化控制器。 – user3386109

+0

好的,我会检查一下。 –

相关问题