如何在segue期间从视图控制器引用tableView [英] How to reference tableView from a view controller during a segue

查看:89
本文介绍了如何在segue期间从视图控制器引用tableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是iOS开发的新手,所以请放轻松一下吧。)

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

在iOS 5中使用Xcode 4.2,我在IB中构建了一个视图控制器,其中的tableview通过push segue链接到详细视图控制器(在表视图单元级别)

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)

我想用数据设置详细页面标签来自我的数据源,基于所选单元格的索引。最初我编写了didSelectRowAtIndexPath只是为了发现这是在segue处理之后调用的,并且发现我需要调用prepareForSegue而不是

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

我的问题是,那个无论我尝试什么,因为tableView不是prepareForSegue的传递参数(与我迄今为止使用的大多数其他方法不同),我很难在视图控制器中访问tableView以调用indexPath方法,这样我可以引用我的数据源

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

我看过很多使用self的例子,如下所示,但这会导致一个错误,说明在视图控制器中不存在tableView

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];
}

我也尝试过使用

ViewController *main = [segue viewController];

而不是使用self,但这导致同样的问题

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

我知道我可以将示例重写为Table View Controller,但是想知道这是否可能?正如我所见,人们在开发中使用这两种方法

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

作为参考,我正在学习本教程... http://kurrytran.blogspot.com/2011/10/ios-5-storyboard-and.html

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

当我到达完成详细信息页面的部分时,我想,为什么不使用segue将表格视图单元格链接到详细信息页面,然后编写复制代码在显示详细信息页面之前的州和首都。正如您在本教程中看到的那样,主控制器是一个视图控制器

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

我的理解是,如果不需要使用UITableViewController可以为任何应用程序提供更大的灵活性以后更复杂的事情

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

希望你能帮忙,
Izzy

Hope you can help, Izzy

推荐答案

我也是iOS的新手,但它的价值是什么:

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

听起来你需要在你的UIViewControllerClass中创建一个指向你的表视图对象的指针。

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;

...然后将其连接到IB内的表格视图。这样在prepareForSegue中你可以访问UITableView对象。

...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];
    ...
}

这篇关于如何在segue期间从视图控制器引用tableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆