[英]cocoa-Why there is an IBOutlet and a property that are of the same name?
[英]Cocoa-Why should I put my statusItem in ApplicationDelegate?
我是 OSX 應用程序開發的新手,我想制作一個狀態欄應用程序。
為了在狀態欄中顯示NSStatusItem
,我首先將statusBar
項放在ViewController
中。 它確實有效。
//ApplicationDelegate.h
#import <Cocoa/Cocoa.h>
@interface AppDelegate : NSObject <NSApplicationDelegate>
@property (strong, nonatomic) NSStatusItem *statusBar;
@end
.
//ApplicationDelegate.m
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
}
- (void)applicationWillTerminate:(NSNotification *)aNotification {
// Insert code here to tear down your application
}
-(void)awakeFromNib{
self.statusBar = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
self.statusBar.title = @"T";
self.statusBar.highlightMode = YES;
}
@end
(當我使用storyboard
而不是nib
時,我不確定是否應該使用awakeFromNib
方法。)
然而,當我閱讀一些演示代碼時,我發現我應該在ApplicationDelegate
類中這樣做。
為什么? 我認為這是我VIEW 的一部分,因為我遵循MVC規則。
NSStatusItem 與窗口或任何視圖無關。 即使沒有附加視圖,它也可以留在 NSStatusBar 中。
因此,在 ApplicationDelegate 類中創建和初始化 NSStatusItem 是一個很好的做法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.