UITableView 在嵌入容器内的控制器之间传递数据 [英] UITableView Passing Data Between Controllers Embedded Inside Containers

查看:56
本文介绍了UITableView 在嵌入容器内的控制器之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的测试项目.一个嵌入在故事板导航控制器中的 UITableViewController (MasterViewController).我不会使用 prepareForSegue 将数据传递给另一个视图控制器 (DetailViewController).相反, didSelectRowAtIndexPath 用于更新 detailviewcontroller 中的标签,如下所示:

I have a simple test project. A UITableViewController (MasterViewController) embedded inside a navigation controller in storyboard. I am NOT segueing using the prepareForSegue to pass data to another view controller (DetailViewController). Instead, didSelectRowAtIndexPath is use to update a label in the detailviewcontroller as below:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailViewController *detailViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"];
    NSMutableString *object = thisArray[indexPath.row];

    detailViewController.passedData = object;
    [self.navigationController pushViewController:detailViewController animated:YES];
}

到目前为止一切正常.

现在我在故事板中添加了另一个视图控制器.使其成为初始视图控制器,在其中添加两个容器,然后在这些容器中嵌入 MasterViewController 和 DetailViewContainer.

Now i have added another view controller in my storyboard. Made it the initial view controller, added two containers in it, then embedded both MasterViewController and DetailViewContainer in these containers.

现在不是在右侧的 DetailViewController 中显示传递的数据,而是通过替换控制器视图在左侧显示传递的数据.

Now instead of showing passed data inside the DetailViewController on the right side, its showing the passed data on the left side by replacing the controller view.

如果我无法澄清我想说的内容,这里是项目的链接https://jumpshare.com/v/UiTFEB6AamIo8qX9sinW ,仅供学习使用.

If i am not able to clarify what i am trying to say, here is the link to the project https://jumpshare.com/v/UiTFEB6AamIo8qX9sinW , its just for learning purpose.

谢谢

推荐答案

你遇到这个问题是因为你还在这样做:

You're getting this problem because you are still doing this:

[self.navigationController pushViewController:detailViewController animated:YES];

您在此处引用的导航控制器是嵌入您的主控制器的控制器,因此您创建一个 detailController 的实例(与屏幕上已有的实例不同)并将其推送到导航控制器上.

The navigation controller you're referencing here is the one your master controller is embedded in, so you create an instance (different than the one that's already on screen) of detailController and push that onto the navigation controller.

您想要做的是获取对屏幕上已经存在的细节控制器的引用——当应用程序启动时,两个子视图控制器(容器视图中的那些)已经被实例化.所以,你需要这样做:

What you want to do, is get a reference to the detail controller that's already on screen -- both child view controllers (the ones in the container views) are already instantiated when the app starts. So, you need to do this:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DetailViewController *detailViewController = [self.navigationController.parentViewController childViewControllers][1];
    NSMutableString *object = thisArray[indexPath.row];
    detailViewController.passedData = object;
}

这会将值传递给详细信息控制器,但是您无法使用代码来更新 viewDidLoad 中的标签,因为该视图已经加载,并且不会再次被调用.相反,覆盖passedData 的setter,并在那里更新标签(请注意,我将参数的名称更改为passedInData,因此它不会与您的属性passedData 冲突):

This will pass the value to the detail controller, but you can't have the code to update the label in viewDidLoad, since that view is already loaded, and won't be called again. Instead, override the setter for passedData, and update the label there (note that I changed the name of the argument to passedInData, so it doesn't conflict with your property passedData):

-(void)setPassedData:(NSString *)passedInData {
    passedData = passedInData;
    detailDescriptionLabel.text = passedData;
}

顺便说一下,除非您计划在主视图控制器之后添加其他控制器,否则没有理由将它嵌入到导航控制器中,考虑到这种设置.如果取出来,那么在拿到detail controller的引用的时候需要去掉self.navigationController的引用.然后就是:

Bu the way, unless you're planning on adding other controllers after your master view controller, there's no reason to have it embedded in a navigation controller at all, given this set up. If you take it out, then you need to remove the reference to self.navigationController when you get the reference to the detail controller. It would then just be:

DetailViewController *detailViewController = [self.parentViewController childViewControllers][1];

这篇关于UITableView 在嵌入容器内的控制器之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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