UIApplication共享应用程序错误:程序似乎访问了错误的文件
在我的 MainViewController 实现中,我需要访问两个不同类的变量。其中一个类是 AppDelegate,另一个类是 FlipsideViewController。 我访问这些的方式是通过这段代码:
-(void)someMethod
{
MyApplicationAppDelegate *appDelegate = (MyApplicationAppDelegate *)[[UIApplication sharedApplication] delegate];
FlipsideViewController *viewController = (FlipsideViewController *)[[UIApplication sharedApplication] delegate];
然后我有一个从应用程序委托访问的数组,以及一些从 FlipsideViewController 的 UISwitch 实例返回值的实例变量:
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] :由于未捕获的异常,无法识别的选择器发送到终止应用程序”
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];
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
}
}
I keep getting the error message "-[MyApplicationAppDelegate switch1]: unrecognized selector sent to instance. terminating app due to uncaught exception"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
[[UIApplication共享应用程序]委托];将始终返回
MyApplicationAppDelegate
类的(单例)实例,并且您不能简单地将其强制转换为FlipsideViewController*
。要访问 Flipsidecontroller 值(假设它存储在您的应用程序委托中),您可以定义一个属性并调用它:[[UIApplication sharedApplication] delegate]; will always return the (singleton) instance of
MyApplicationAppDelegate
class and you cannot simply cast it toFlipsideViewController*
. to access flipsidecontroller value (assuming it is stored in your appdelegate) you can define a property and call it: