2014-04-02 74 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

好的,我會檢查一下。 –

相關問題