Pass variable to a detail view controller using a table in UIViewController (not UITableViewController!!)

醉酒当歌 提交于 2019-12-08 13:05:45

问题


I currently have 2 view controllers: ViewController and DetailViewController. Neither of them are Table View Controllers, they are both just View Controllers. However, I do have a table on ViewController which is otherwise working great. I want to segue/pass a variable to DetailViewController when a cell is selected.

Using the storyboard I set up a modal segue from the cell to DetailViewController with the identifier toDetailView.

I tried to use this method to pass my variable (in this example called play) from VC to DVC (via http://developer.apple.com/library/ios/#samplecode/SimpleDrillDown/Listings/Classes_RootViewController_m.html#//apple_ref/doc/uid/DTS40007416-Classes_RootViewController_m-DontLinkElementID_10):

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

    if ([[segue identifier] isEqualToString:@"toDetailView"]) {

        NSIndexPath *selectedRowIndex = [self.tableView indexPathForSelectedRow];
        DetailViewController *detailViewController = [segue destinationViewController];
        detailViewController.play = [dataController objectInListAtIndex:selectedRowIndex.row];
    }
}

However I get an error: Property 'tableView' not found on object type 'ViewController *'. I think this is because it's not a Table View Controller? How can I get around this? Or is there another method I could use to segue and pass the variable using (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath?


回答1:


On the ViewController, create a new property like that :

@property (nonatomic, assign) IBOutlet UITableView *tableView;

And synthesize it. Then, in Interface Builder, you should be able to assign the outlet to the tableview you are using. This should do the trick



来源:https://stackoverflow.com/questions/11603904/pass-variable-to-a-detail-view-controller-using-a-table-in-uiviewcontroller-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!