我想在可可MAC應用程序開發中交換視圖,如UINavigationController用於可可觸摸中的推送/流行視圖。 它是推送和流行視圖的應用程序的要求,不知何故,我可以設法做到這一點,但現在我處於項目的中間,我想要像viewWillAppear在可可中。NSViewController viewWillAppear cocoa
0
A
回答
1
這種方法在Mac上不可用。與iOS UI相比,OSX UI非常不同。 使用 - (void)loadView進行初始UI更新(或者 - (void)awakeFromNib(如果您的UI是在Interface Builder中設計的並從xib文件加載的話),然後以編程方式隨時隨地創建特定的按需UI更新請求調用showWindow,makeKeyAndOrderFront,獲取通知,從代表獲得回調等
-1
我已經使用NSViewController來改變視圖,如按需設置窗口上的viewController按鈕,但我發現一旦(id)初始化被調用一個特定的viewController,它永遠不會再調用同一個viewController.For由於這個原因我的UI(視圖/ MVC)不更新。我強制再次調用(id)init,但視圖不刷新。
0
我使用了NSViewController的特殊視圖,該視圖調用了視圖控制器。 您可以擴展代碼以使其具有像viewWillAppear這樣的方法。
- (void)viewWillMoveToWindow:(NSWindow *)newWindow {
[super viewWillMoveToWindow:newWindow];
if (self.installed && !newWindow) {
[self viewWillUninstall];
NSViewController<SMControllerViewDelegate> *viewController = self.viewController;
if (viewController) {
@autoreleasepool {
[viewController viewWillUninstall];
}
}
}
}
- (void)viewDidMoveToWindow {
[super viewDidMoveToWindow];
if (!self.installed && self.window) {
[self viewDidInstall];
NSViewController<SMControllerViewDelegate> *viewController = self.viewController;
if (viewController) {
@autoreleasepool {
[viewController viewDidInstall];
}
}
}
}
1
我想出了somethiing這樣的:
部首:
@protocol GWViewExtensions <NSObject>
- (void) viewWasAddedToSuperview;
@end
@interface GWView : NSView
//set to the view controller managing this view to get NSResponder udpates,
//and GWViewExtension protocol method calls.
@property (weak,nonatomic) IBOutlet NSViewController * viewController;
@end
實現:
#import "GWView.h"
@implementation GWView
- (void) awakeFromNib {
[self setNeedsDisplay:TRUE];
}
- (void) addSubview:(NSView *)aView {
[super addSubview:aView];
if([aView isMemberOfClass:[GWView class]]) {
GWView * gview = (GWView *)aView;
if(gview.viewController && [gview.viewController conformsToProtocol:@protocol(GWViewExtensions)]) {
if([gview.viewController respondsToSelector:@selector(viewWasAddedToSuperview)]) {
[gview.viewController performSelectorOnMainThread:@selector(viewWasAddedToSuperview) withObject:nil waitUntilDone:FALSE];
}
}
}
}
- (void) setViewController:(NSViewController *) newController {
if(_viewController) {
NSResponder *controllerNextResponder = [_viewController nextResponder];
[super setNextResponder:controllerNextResponder];
[_viewController setNextResponder:nil];
}
_viewController = newController;
if(newController) {
NSResponder *ownNextResponder = [self nextResponder];
[super setNextResponder:_viewController];
[_viewController setNextResponder:ownNextResponder];
}
}
- (void)setNextResponder:(NSResponder *)newNextResponder {
if(self.viewController) {
[self.viewController setNextResponder:newNextResponder];
return;
}
[super setNextResponder:newNextResponder];
}
- (void) setBackgroundColor:(NSColor *)backgroundColor {
_backgroundColor = backgroundColor;
[self setNeedsDisplay:TRUE];
}
- (void) drawRect:(NSRect) dirtyRect {
[super drawRect:dirtyRect];
if(self.backgroundColor) {
NSGraphicsContext * theContext = [NSGraphicsContext currentContext];
[theContext saveGraphicsState];
[[NSGraphicsContext currentContext] setPatternPhase:NSMakePoint(0,[self frame].size.height)];
[self.backgroundColor set];
NSRectFill([self bounds]);
[theContext restoreGraphicsState];
}
}
- (void) dealloc {
self.backgroundColor = nil;
}
@end
用途:
#import <Cocoa/Cocoa.h>
#import "GWView.h"
@interface GWDribbbleShotDetailColumn : NSViewController <GWViewExtensions>
@end
#import "GWDribbbleShotDetailColumn.h"
@interface GWDribbbleShotDetailColumn()
@end
@implementation GWDribbbleShotDetailColumn
- (void) viewWasAddedToSuperview {
NSRect svb = self.view.superview.bounds;
NSRect sf = self.view.frame;
sf.size.height = NSHeight(svb);
sf.size.width = NSWidth(svb);
self.view.frame = sf;
}
@end
2
僅供參考,iOS視圖生命週期方法(viewDidLoad,viewWillAppear,viewDidAppear等)自OSX 10.10優勝美地(可能起)開始提供。
並注意:如果您打算在早於10.10 OSX版本上運行應用程序,請不要實施此類方法,因爲它們不會被早期版本識別(因此內部代碼將不會執行)
相關問題
- 1. Cocoa:用nib加載NSViewController
- 2. Cocoa - 以編程方式呈現NSViewController
- 3. UIViewController vs NSViewController
- 4. NSViewController中的NSScrollView
- 5. NSView和NSViewController之間的區別
- 6. NSBundle加載NSViewController
- 7. NSViewController未發佈
- 8. NSViewController和綁定
- 9. NSViewController中的firstResponder
- 10. IBOutlets在NSViewController
- 11. setAction:在NSViewController中
- 12. NSViewController中的奇怪行爲
- 13. UITableViewController viewWillAppear
- 14. 如何在Xcode中創建一個NSViewController?
- 15. NSViewController未被創建
- 16. keyup keydown方法NSViewController
- 17. 管理FirstResponder在NSViewController
- 18. 綁定NSViewController的representObject
- 19. 子類NSViewController或NSView
- 20. Xcode爲NSViewController生成多個@interface聲明?
- 21. 如何從另一個NSViewController中包含的NSViewController訪問NSView
- 22. UIButton setBackgroundImage and viewWillAppear
- 23. viewWillAppear閃爍
- 24. selectRowAtIndexPath失敗viewWillAppear
- 25. viewWillAppear中不叫
- 26. viewWillAppear中不叫
- 27. viewWillAppear不叫
- 28. viewWillAppear和UIApplicationDidBecomeActiveNotification
- 29. viewWillAppear中iPad上
- 30. Swift viewWillAppear redirect