iPhone - UINavigationController 问题
我想在表格上显示一些数据,我们将其称为“TabeView”应用程序。
所以我在 XCode 上创建了一个“基于导航”的应用程序,它在“classes 文件夹”中为我提供了 4 个文件。
- RootViewController.h
- RootViewController.m
- TableViewAppDelegate.h
- TableViewAppDelegate.m
现在,我想使用“didFinishLaunchingWithOptions”方法(我使用数组完成该方法)在 TableViewAppDelegate 中设置数据。
然后,我需要将此数组“发送”到 RootViewController。我在 RootViewController 中有一个数组变量,所以我假设我需要从 TableViewAppDelegate 在 RootViewController 中“设置”数组变量。我该怎么做?
我遇到的问题是,我不知道如何从 TableViewAppDelegate 设置 RootViewController 中的数组变量。我想知道下面的事情是否可能
....
[RootViewController setMyArray:myArray];
[window addSubview:navigationController.view];
[window makeKeyAndVisible];
....
,但我不知道如何调用“RootViewController”。
希望我说得有道理。谢谢。
I want to display some data on table, let's call it "TabeView" app.
So I created a "navigation-based" application on XCode and it gives me 4 files in the "classes folder".
- RootViewController.h
- RootViewController.m
- TableViewAppDelegate.h
- TableViewAppDelegate.m
Now, I wanted to set up the data in TableViewAppDelegate using the "didFinishLaunchingWithOptions" method, which I did using an array.
Then, I need to "send" this array to the RootViewController. I have an array variable in RootViewController so I assume that I need to "set" the array variable in RootViewController from the TableViewAppDelegate. How can I do this?
The problem I am having is, that I don't know how to set the array variable in RootViewController from TableViewAppDelegate. I want to know if something like below is possible
....
[RootViewController setMyArray:myArray];
[window addSubview:navigationController.view];
[window makeKeyAndVisible];
....
But I have no idea how to call "RootViewController".
Hopefully I made some sense. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,在 RootViewController 类中为数组变量创建一个属性。因此,例如,如果您的数组变量名为 myArray,您的代码可能如下所示:
在 RootViewController.h 中:
并在 RootViewController.m 中添加相应的 @synthesize 行:
现在您可以像这样设置 RootViewController 对象的 myArray 成员:
现在,在您的应用程序委托类中,您可以使用 self.navigationController 的 viewControllers 属性返回导航堆栈中的视图控制器。根控制器将始终位于索引 0。viewControllers 将返回一个 NSArray,其元素为 NSObject 类型,因此您也需要转换为 RootViewController 指针。这里有两行,为了使转换明确:(
为了在应用程序委托类中使用 RootViewController 类名,您需要导入相关的头文件 - 将其放在 TableViewAppDelegate.m 的顶部:
这就是 ps 请记住,
将 myArray 属性声明为类型(非原子、保留)意味着您的 RootViewController 对象最终将指向您传递给它的 NSArray 的同一个实例。 :
您可以使用 (nonatomic, copy) 代替,在这种情况下,您的 RootViewController 对象将复制您传入的数组并保留该数组,一旦您将其分配给 RootViewController 对象,对数组的更改将不会产生影响。副本。
First, create a property in your RootViewController class for the array variable. So e.g. if your array variable is called myArray your code might look something like this:
In RootViewController.h:
and add the corresponding @synthesize line in RootViewController.m:
Now you can set the myArray member of a RootViewController object like this:
Now, in your app delegate class, you can use the viewControllers property of self.navigationController to return the view controllers in the navigation stack. The root controller will always be at index 0. viewControllers will return an NSArray, whose elements are of type NSObject, so you need to cast to a RootViewController pointer too. Here it is in two lines, to make the cast explicit:
(In order to use the RootViewController class name in your app delegate class you'll need to import the relevant header file - put this at the top of TableViewAppDelegate.m:
And that's it!
p.s. Bear in mind that declaring your myArray property as type (nonatomic, retain) means that your RootViewController object will end up pointing to the same instance of NSArray that you pass to it. So, for example:
You can use (nonatomic, copy) instead, in which case your RootViewController object will make a copy of the array you pass in and retain that instead. Changes to the array once you've assigned it to your RootViewController object won't affect the copy.
navigationController.myArray = myArray;
之前
在
[window addSubview:navigationController.view];
navigationController.myArray = myArray;
before
[window addSubview:navigationController.view];