ViewController类在创建视图时会创建新实例,但它应该使用现有实例 [英] ViewController class creates new instance when creating view, but it should use an existing one

查看:63
本文介绍了ViewController类在创建视图时会创建新实例,但它应该使用现有实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在名为ResourcesTableViewController的类中创建TableViewController。我正在使用以下类创建该类:

I'm creating a TableViewController in a class named ResourcesTableViewController. I'm creating the class with:

ViewController.h:

ViewController.h:

@interface ViewController : UIViewController {
    ResourcesTableViewController *resourceTableViewController;

ViewController.m:

ViewController.m:

resourceTableViewController = [[ResourcesTableViewController alloc] initWithStyle:UITableViewStylePlain];
[resourceTableViewController setTableViewObject:localObject];

现在我可以使用它,并且可以打电话

Now I can work with it and I can call

NSLog(@"%@ %@",resourceTableViewController,[resourceTableViewController tableViewObject]);

在ViewController.m的多个部分,输出(如预期)相同

at several parts in my ViewController.m with (as expected) the same output

<ResourcesTableViewController: 0x749f1c0> tableViewObjectContent

。为了打开新的TableView,我在情节提要中创建了一个带有弹出框的按钮,用于弹出到新视图(作为触发的Segue)。但是,一旦按下按钮并出现视图,它将创建一个新实例。奇怪的是,我放入了我的类ResourcesTableViewController的viewDidLoad:

. For switching on the new TableView I created in the storyboard a button with the popover to the new view (as triggered segue). But as soon as the button is pressed and the view shall appear, it creates a new instance. Curiously I put in the viewDidLoad of my class ResourcesTableViewController:

NSLog(@"viewDidLoad: %@: %@", self, self.tableViewObject);

它正在返回

<ResourcesTableViewController: 0x74bbcd0>: (null)

。另一个内存地址,另一个实例。不是我以前使用过的实例,绝对不是我给该tableViewOject提供的实例。
如何从属于我创建并使用过的resourceTableViewController的实例中调用视图?我仍然可以通过与对象resourceTableViewController进行通信来访问旧实例,但是创建的视图肯定是该类型的另一个视图,肯定不会从我给第一个对象提供的对象中使用。

. Another memory adress, another instance. Not the instance I worked with before and absolutely not the instance to which I gave that tableViewOject. How can I call the view from that instance belonging to the resourceTableViewController I created and worked with? I still can access that "old" instance through communicating with the object resourceTableViewController but the created view is another one of that type that certainly doesn't make use from the object I gave to the first one.

推荐答案

如何显示表格视图取决于您要如何显示表格视图以及之前的屏幕内容。通常,您将使用segue从当前控制器(您所按钮所在的控制器)过渡到表格视图。为此,您可以使用performSegueWithIdentifier:sender:,其中identifier参数是您在IB中为segue提供的名称。但是,您可以通过控制从一个视图中的按钮拖动到表格视图控制器的视图并为模式类型选择模式来完全实现代码,然后单击按钮将导致该表格视图出现在屏幕上

How to show the table view depends on how you want to show it, and what is on the screen before that. Normally, you would use a segue to transition from the current controller (the one that you button is in) to the table view. To do that you could use performSegueWithIdentifier:sender: where the identifier parameter is a name you give to the segue in IB. But you can do it with no code at all by control dragging from the button in your one view to the table view controller's view and choosing modal for the type of segue -- then, a button click will cause that table view to appear on screen.

如果使用情节提要,则根本不应该在代码中分配init - segue会为您实例化新的视图控制器。

You shouldn't alloc init it at all in your code if you're using a storyboard -- the segue instantiates the new view controller for you.

这篇关于ViewController类在创建视图时会创建新实例,但它应该使用现有实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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