UIApplication共享应用程序错误:程序似乎访问了错误的文件

发布于 2024-08-18 18:21:57 字数 859 浏览 2 评论 0原文

在我的 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 技术交流群。

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

发布评论

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

评论(1

扬花落满肩 2024-08-25 18:21:57

[[UIApplication共享应用程序]委托];将始终返回 MyApplicationAppDelegate 类的(单例)实例,并且您不能简单地将其强制转换为 FlipsideViewController*。要访问 Flipsidecontroller 值(假设它存储在您的应用程序委托中),您可以定义一个属性并调用它:

-(void)somemethod{
     MyApplicationAppDelegate *appDelegate = (MyApplicationAppDelegate *)[[UIApplication sharedApplication] delegate];
     FlipsideViewController *viewController = appDelegate.flipsideController;
}

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