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

时间:2016-12-19 15:18:32

标签: ios objective-c macos class

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

@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

然后我有我的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];
}
...

我基本上是尝试将右键单击的功能添加到NSView,以便在右键单击它时会运行一个位于AppDelegate类中的方法,但它不起作用。

我尝试过添加:

@property (nonatomic) AppDelegate* thisapp;

MyNotificationView s @interface。然后在AppDelegate createLabel方法中添加:

view.thisapp = self;

然后在MyNotificationView markAsRead方法中执行类似的操作:

[_ thisapp markAsRead:_theid];

但这会引发错误:

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

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

1 个答案:

答案 0 :(得分:1)

您需要在AppDelegate.h文件中声明您的方法,然后您可以在markread方法中使用[[UIApplication sharedApplication] delegate];来获取可用于调用markAdRead方法的应用代理的实例上。