调用另一个类时,'AppDelegate'没有可见的@interface

时间:2023-01-16 16:43:14

I have an implementation of a custom class like this:

我有一个像这样的自定义类的实现:

@interface MyNotificationView: NSView
@property (nonatomic) int theid;
@end

@implementation MyNotificationView
- (void) rightMouseDown:(NSEvent *)event {
    [self markread];
}
- (void)markread {
    //call markAsRead in AppDelegate with _theid as param
}
@end

And then I have my AppDelegate:

然后我有我的AppDelegate:

...

@implementation AppDelegate
...
-(void)createLabel{
    MyNotificationView *view = [[MyNotificationView alloc] initWithFrame:CGRectMake(0,0,100,100)];
    view.theid = notification_id;
}
-(void)markAsRead:(int)index{
    NSMutableArray *arrayofdics = [[[NSUserDefaults standardUserDefaults] objectForKey:@"arrayofdics"] mutableCopy];
    NSMutableDictionary *dic = [[arrayofdics objectAtIndex:index] mutableCopy];
    [dic setObject:[NSNumber numberWithBool:1] forKey:@"read"];
    [arrayofdics replaceObjectAtIndex:index withObject:dic];
    [[NSUserDefaults standardUserDefaults] setObject:arrayofdics forKey:@"arrayofdics"];
    [self createBodyWindow];
    unreadNotifications--;
    [self setNotificationMenuBar];
}
...

I am basically trying to add the functionality of a right click to a NSView so that when it is right clicked it runs a method that is within the AppDelegate class but it it is not working.

我基本上是尝试将右键单击的功能添加到NSView中,这样当它被右键单击时,它会运行一个AppDelegate类中的方法,但它无法正常工作。


I have tried adding:

我试过添加:

@property (nonatomic) AppDelegate* thisapp;

to MyNotificationViews @interface. And then in the AppDelegates createLabel method adding:

到MyNotificationViews @interface。然后在AppDelegates中创建createLabel方法:

view.thisapp = self;

and then in MyNotificationViews markAsRead method doing something like:

然后在MyNotificationViews中使用markAsRead方法执行以下操作:

[_thisapp markAsRead:_theid];

[_thisapp markAsRead:_theid];

but this throws the error:

但这会引发错误:

No visible @interface for 'AppDelegate' declares the selector 'markAsRead:'

How should I properly implement this.

我该如何正确实现这一点。

1 个解决方案

#1


1  

You need to declare your method i the AppDelegate.h file and then you can use [[UIApplication sharedApplication] delegate]; in your markread method to get a instance to your app delegate that you can use to call your markAdRead method on.

您需要在AppDelegate.h文件中声明您的方法,然后您可以使用[[UIApplication sharedApplication] delegate];在您的markread方法中,为您的app委托获取一个实例,您可以使用该实例调用markAdRead方法。

#1


1  

You need to declare your method i the AppDelegate.h file and then you can use [[UIApplication sharedApplication] delegate]; in your markread method to get a instance to your app delegate that you can use to call your markAdRead method on.

您需要在AppDelegate.h文件中声明您的方法,然后您可以使用[[UIApplication sharedApplication] delegate];在您的markread方法中,为您的app委托获取一个实例,您可以使用该实例调用markAdRead方法。