Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
283 views
in Technique[技术] by (71.8m points)

uiviewcontroller - How to reference tableView from a view controller during a segue

I'm new to iOS development, so go easy on me please :)

Using Xcode 4.2 for iOS 5, I've built a view controller in IB, with a tableview inside it linked to a detail view controller through a push segue (at table view cell level)

I'm wanting to setup the detail pages labels with data from my datasource based on the index of the cell selected. Initially I coded the "didSelectRowAtIndexPath" only to find out that this is called after the segue processing, and found out that I need to call prepareForSegue instead

My problem is, that whatever I try, as tableView is not a passed parameter to prepareForSegue (unlike most other methods I've used thus far), I'm struggling to get access to the tableView within the view controller in order to call the indexPath method, such that I can reference my datasource

I've seen many examples using self, as below, but this results in an error stating that tableView doesn't exist within the view controller

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

  NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
  DetailViewController *detail = [segue destinationViewController];
  detail.primary = [datasource objectAtIndex:selectedIndexPath.row];
  detail.seconday = [datalist objectForKey:detail.primary];
}

I've also tried using

ViewController *main = [segue viewController];

Instead of using self, but this results in the same issue

I know that I could rewrite the example to be a Table View Controller instead, but wanted to know if it was possible this way ? As I've seen people use both methods in development

For reference, I was following this tutorial ... http://kurrytran.blogspot.com/2011/10/ios-5-storyboard-and.html

When I got to the section for completing the detail page, I thought, why not use a segue to link the table view cell to the detail page, and code up the copying of the state and capital prior to the detail page being displayed. As you can see from this tutorial, the main controller is a view controller

My understanding was that NOT using an UITableViewController would give any app greater flexibility should it need to develop into something more complex later on

Hope you can help, Izzy

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'm also new to iOS, but for what it's worth:

Sounds like you need to create a pointer to your table view object in your UIViewControllerClass.

@interface YourClass : UIViewController
{
    IBOutlet UITableView  *MyTableView;
}
@property ... IBOutlet UITableView *MyTableView;

...and then hook this up to your "table view" within IB. Such that in prepareForSegue you can then access the UITableView object.

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSIndexPath *selectedIndexPath = [self.MyTableView indexPathForSelectedRow];
    ...
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...