Iphone 使用位于app delegate中的NSMutableArray将数据加载到uipickerview中

Iphone 使用位于app delegate中的NSMutableArray将数据加载到uipickerview中,iphone,xml,parsing,uipickerview,Iphone,Xml,Parsing,Uipickerview,基本上,我已经将XML中的一些数据解析为一个NSMutableArray,该数组在appDelegate中共享 在我的secondViewController中,我有一个uiPickerView,希望将数组的详细信息加载到其中 我的问题是。。。怎么做 我之前曾短暂地使用过uiPickerView,必须首先加载数据才能分配给uiPickerView,如下所示: titleDB = [[NSMutableArray alloc] init]; [titleDB addObject:@"MR"]; [

基本上,我已经将XML中的一些数据解析为一个NSMutableArray,该数组在appDelegate中共享

在我的secondViewController中,我有一个uiPickerView,希望将数组的详细信息加载到其中

我的问题是。。。怎么做

我之前曾短暂地使用过uiPickerView,必须首先加载数据才能分配给uiPickerView,如下所示:

titleDB = [[NSMutableArray alloc] init];
[titleDB addObject:@"MR"];
[titleDB addObject:@"Mrs"];
[titleDB addObject:@"Ms"];
[titleDB addObject:@"Miss"];
[titleDB addObject:@"DR"];
[titlePickerView selectRow:1 inComponent:0 animated:YES];
但是由于数据来自appDelegate,我不知道应该如何将其加载到uiPickerview中,这与数据源有关吗

我在要求向我扔代码我只是在要求最好的方法

这方面的任何帮助都会很好

谢谢
Jonn4y基本上,您希望从代码中的随机位置访问appDelegate对象。这不是一个不寻常的要求。记住,Objective C是C语言的超集。因此,您可以使用全局变量。出于上述原因,Cocoa程序中还有什么比AppDelegate更自然的变量呢。因此,在appDelegate.h文件中,添加:

<MyAppDelegateClass> * appDelegate;

这是一种常见的模式。您将希望访问UIApplication的sharedApplication实例。因此,假设您的appDelegate类名为YourAppDelegate,那么您的appDelegate和viewController中的数组ivar是titleDB,那么您可以在viewController的viewDidLoad方法中执行此操作

YourAppDelegate *appDelegate=(YourAppDelegate *)[[UIApplication sharedApplication] delegate];
// assuming you are using @property and @synthesize for your ivars
self.titleDB=appDelegate.titleDB;

祝您好运

您好,谢谢您的帮助,我已经尝试了此功能,并且所有功能都已正确连接,当我调用NSLOG时,我可以看到数据库中的数据,但当我尝试
countryDB=appDelegate.countries它崩溃,告诉我
由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[BookPhoto isEqualToString::]:发送到实例0x6d70440的无法识别的选择器'
BookPhoto是我最初用来解析XML的文件,所以不确定为什么在这里调用它,我的xml解析来自:countryDB是一个NSMutableArray,Countrys是我在委托中拥有的NSMutableArray(只是为了让你知道我不支持使用titleDB)Cheers是一个文件吗?还是一根绳子?isEqualToString:希望前面有一个字符串!还有,对不起,我漏了一步。在应用程序中:didFinishLaunchingWithOptions在appDelegate文件中,将以下语句放在开头以设置全局变量:appDelegate=self;
YourAppDelegate *appDelegate=(YourAppDelegate *)[[UIApplication sharedApplication] delegate];
// assuming you are using @property and @synthesize for your ivars
self.titleDB=appDelegate.titleDB;