2012-08-16 81 views
0

我想在可可MAC應用程序開發中交換視圖,如UINavigationController用於可可觸摸中的推送/流行視圖。 它是推送和流行視圖的應用程序的要求,不知何故,我可以設法做到這一點,但現在我處於項目的中間,我想要像viewWillAppear在可可中。NSViewController viewWillAppear cocoa

回答

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版本上運行應用程序,請不要實施此類方法,因爲它們不會被早期版本識別(因此內部代碼將不會執行)