UIApplication sharedAppication错误:程序似乎正在访问错误的文件 [英] UIApplication sharedAppication error: program seems to be accessing wrong file
问题描述
在MainViewController实现中,我需要访问两个不同类的变量.其中一个类是AppDelegate,另一个是FlipsideViewController. 我访问这些代码的方式是通过以下代码:
in my MainViewController implementation, I need to access variables from two different classes. one of the classes is the AppDelegate and the other is the FlipsideViewController. the way I accessed these was through this code:
-(void)someMethod
{
MyApplicationAppDelegate *appDelegate = (MyApplicationAppDelegate *)[[UIApplication sharedApplication] delegate];
FlipsideViewController *viewController = (FlipsideViewController *)[[UIApplication sharedApplication] delegate];
然后我有一个从我的应用程序委托访问的数组,还有一些实例变量,它们从flipsideViewController的UISwitch实例返回值:
then I have an array I access from my application delegate, and some instance variables that return values from an instance of UISwitch from the flipsideViewController:
NSMutableArray* array = [[NSMutableArray alloc] initWithArray:(NSMutableArray *)appdelegate.originalArray];
for (id element in array)
{
if ([[element attribute] isEqualToString:@"someAttribute"] && [viewController.switch1 isOn] == YES)
{
//preform function
}
}
我一直收到错误消息-[[MyApplicationAppDelegate switch1]:无法识别的选择器发送到实例.由于未捕获的异常而终止了应用程序".
I keep getting the error message "-[MyApplicationAppDelegate switch1]: unrecognized selector sent to instance. terminating app due to uncaught exception"
推荐答案
[[UIApplication sharedApplication]委托];将始终返回MyApplicationAppDelegate
类的(单个)实例,并且您不能简单地将其强制转换为FlipsideViewController*
.要访问flipsidecontroller值(假设它存储在您的appdelegate中),您可以定义一个属性并调用它:
[[UIApplication sharedApplication] delegate]; will always return the (singleton) instance of MyApplicationAppDelegate
class and you cannot simply cast it to FlipsideViewController*
. to access flipsidecontroller value (assuming it is stored in your appdelegate) you can define a property and call it:
-(void)somemethod{
MyApplicationAppDelegate *appDelegate = (MyApplicationAppDelegate *)[[UIApplication sharedApplication] delegate];
FlipsideViewController *viewController = appDelegate.flipsideController;
}
这篇关于UIApplication sharedAppication错误:程序似乎正在访问错误的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!