我是Objective-c的新手。当我正在阅读其他人编写的一些源代码时,我遇到了一个问题。
我发现有
IBOutlet NSPopover *popover;
以及
@property NSPopover *popover;
PopoverViewController.h
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
#import "TimerPopoverViewController.h"
@class TimerLogic;
@class TimerInfo;
@interface TimerPopoverDelegate : NSObject <NSPopoverDelegate> {
@private
IBOutlet NSPopover *popover;
IBOutlet NSWindow *detachWindow;
IBOutlet TimerPopoverViewController *viewController;
}
@property NSPopover *popover;
- (void)showPopover:(id)sender timerInfo:(TimerInfo *)timerInfo;
@end
我认为它们是不同的变量。但是,我无法弄清楚他们做了什么?
就我而言,IBOutlet
是显示一个弹出窗口。
但是@property
做了什么?
答案 0 :(得分:2)
这可能是非常旧的代码,也可能是以非常古老的(现在气馁的)风格编写的。这里的IBOutlet
声明了一个实例变量(ivar)。 @property
声明了一个由实例变量支持的属性。在现代的ObjC中,你应该这样实现它:
<强> PopoverViewController.h 强>
#import <Cocoa/Cocoa.h>
@class TimerInfo;
// Things declared here are public
@interface TimerPopoverDelegate : NSObject <NSPopoverDelegate>
// You could leave this here if it is required by other parts of the program,
// but other parts of the program really shouldn't require it. See below.
// @property (nonatomic, readonly, weak) NSPopover *popover;
- (void)showPopover:(id)sender timerInfo:(TimerInfo *)timerInfo;
@end
<强> PopoverViewController.m 强>
// Generally avoid importing local headers into the .h unless you have to.
#import "TimerPopoverViewController.h"
// Things declared here are private. This is much better than the old @private.
@interface TimerPopoverDelegate ()
@property (nonatomic, readwrite, weak) IBOutlet NSPopover *popover;
@property (nonatomic, readwrite, weak) IBOutlet NSWindow *detachWindow;
@property (nonatomic, readwrite, weak) IBOutlet TimerPopoverViewController *viewController;
@end
(目前popover
是公开的,但您应该避免以这种方式暴露IBOutlet
。外部对象不应直接触及视图控制器的出口。)