iPhone - UINavigationController 问题

发布于 2024-09-16 01:49:27 字数 781 浏览 4 评论 0原文

我想在表格上显示一些数据,我们将其称为“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

快乐很简单 2024-09-23 01:49:27

首先,在 RootViewController 类中为数组变量创建一个属性。因此,例如,如果您的数组变量名为 myArray,您的代码可能如下所示:

在 RootViewController.h 中:

#import <UIKit/UIKit.h>

@interface RootViewController : UITableViewController {
    NSArray *myArray;
}

@property (nonatomic, retain) NSArray *myArray;

@end

并在 RootViewController.m 中添加相应的 @synthesize 行:

#import "RootViewController.h"

@implementation RootViewController

@synthesize myArray;

// methods here

@end

现在您可以像这样设置 RootViewController 对象的 myArray 成员:

myRootViewController.myArray = [NSArray arrayWithObjects:@"foo", @"bar", nil]; 

现在,在您的应用程序委托类中,您可以使用 self.navigationController 的 viewControllers 属性返回导航堆栈中的视图控制器。根控制器将始终位于索引 0。viewControllers 将返回一个 NSArray,其元素为 NSObject 类型,因此您也需要转换为 RootViewController 指针。这里有两行,为了使转换明确:(

// get a pointer to the root view controller
id obj = [self.navigationController.viewControllers objectAtIndex:0];

// cast it to a RootViewController pointer
RootViewController* rvc = (RootViewController*)obj;

// Now you can set the array
rvc.myArray = someArray;

为了在应用程序委托类中使用 RootViewController 类名,您需要导入相关的头文件 - 将其放在 TableViewAppDelegate.m 的顶部:

#import "RootViewController.h"

这就是 ps 请记住,

将 myArray 属性声明为类型(非原子、保留)意味着您的 RootViewController 对象最终将指向您传递给它的 NSArray 的同一个实例。 :

NSMutableArray *array = [NSMutableArray arrayWithObjects: @"foo", @"bar", nil];
myRootViewController.myArray = array;

[array removeAllObjects]; // myRootViewController.myArray is now empty!

您可以使用 (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:

#import <UIKit/UIKit.h>

@interface RootViewController : UITableViewController {
    NSArray *myArray;
}

@property (nonatomic, retain) NSArray *myArray;

@end

and add the corresponding @synthesize line in RootViewController.m:

#import "RootViewController.h"

@implementation RootViewController

@synthesize myArray;

// methods here

@end

Now you can set the myArray member of a RootViewController object like this:

myRootViewController.myArray = [NSArray arrayWithObjects:@"foo", @"bar", nil]; 

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:

// get a pointer to the root view controller
id obj = [self.navigationController.viewControllers objectAtIndex:0];

// cast it to a RootViewController pointer
RootViewController* rvc = (RootViewController*)obj;

// Now you can set the array
rvc.myArray = someArray;

(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:

#import "RootViewController.h"

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:

NSMutableArray *array = [NSMutableArray arrayWithObjects: @"foo", @"bar", nil];
myRootViewController.myArray = array;

[array removeAllObjects]; // myRootViewController.myArray is now empty!

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.

悸初 2024-09-23 01:49:27

navigationController.myArray = myArray;

之前

[window addSubview:navigationController.view];

navigationController.myArray = myArray;

before

[window addSubview:navigationController.view];

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文