NSViewController viewWillAppear cocoa

时间:2012-08-16 13:31:57

标签: macos cocoa nsviewcontroller

我想在可可MAC应用程序开发中交换视图,比如UINavigationController,用于cocoa touch中的推送/弹出视图。 这是应用程序对推送和弹出视图的要求,不知怎的,我可以设法实现这一点,但现在我在项目的中间,我想要像可可中的viewWillAppear。

5 个答案:

答案 0 :(得分:2)

仅供参考,iOS OS生命周期方法(viewDidLoad,viewWillAppear,viewDidAppear等)自OSX 10.10 Yosemite(可能还有以后)开始提供。

并提醒注意:如果您计划在10.10 OSX版本之前运行应用程序,请不要实现此类方法,因为早期版本无法识别这些方法(因此不会执行其中的代码)

答案 1 :(得分:1)

这种方法在Mac上不可用。与iOS UI相比,OSX UI非常不同。 使用 - (void)loadView进行初始UI更新(或 - (void)awakeFromNib如果您的UI是在Interface Builder中设计并从xib文件加载),之后以编程方式进行特定的按需UI更新请求,无论何时调用showWindow,makeKeyAndOrderFront,获取通知,从代理获取回调等。

答案 2 :(得分:1)

我想出了这样的事情:

HEADER:

@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

答案 3 :(得分: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];
            }
        }
    }
}

答案 4 :(得分:-1)

我已经使用NSViewController来改变视图,就像在按钮点击窗口上设置viewController一样根据需要,但我发现一旦(id)init被调用特定的viewController,它再也不会再调用同一个viewController。出于这个原因,我的UI(View / MVC)没有更新。我再次强行调用(id)init,但视图没有刷新。