拆分视图应用程序,如何调用位于根视图中的函数?

时间:2011-08-15 13:34:58

标签: iphone objective-c uitableview uisplitviewcontroller

我正在制作拆分视图应用程序。在RootController中,有一个函数指向tableview的下一个单元格:

-(void)goToNextCell
{
    NSIndexPath *nextCell = [NSIndexPath indexPathForRow:currentSelection.row+1 inSection:currentSelection.section];

    [self.tableView selectRowAtIndexPath:nextCell animated:YES scrollPosition: UITableViewScrollPositionTop];
    NSLog(@"Went to next Cell!");
}

在详细视图中,有一个按钮NEXT转到下一个单元格:

-(IBAction)goToNextTextClicked:(id)sender
{
    //Should call this function ^^^^ HOW?(((    
}

1 个答案:

答案 0 :(得分:3)

我通常通过在详细视图控制器类头中定义委托协议来完成此类操作,如下所示:

@protocol DetailViewControllerDelegate
- (void)didClickGoToNext;
@end

@interface DetailViewController {
    id<DetailViewControllerDelegate> delegate;
}

@property (nonatomic, assign) id<DetailViewControllerDelegate> delegate;
@end

在我的详细视图控制器的实现中,我将有:

-(IBAction)goToNextTextClicked:(id)sender {
    [delegate didClickGoToNext];
}

最后,我将通过root视图控制器实现DetailViewControllerDelegate协议并将其自身设置为委托。现在,当按下详细视图控制器的按钮时,将通知根视图控制器,并且它可以做出相应的反应。