macOS - NSViewController
2021-09-15 本文已影响0人
LeungKinKeung
实例化
NSViewController-nib.png建议创建NSViewController子类时同时生成xib文件,否则在创建对象时需要自行调用-[NSViewController setView:] 后才能使用,假如有nib文件,直接[NSViewController new]即可创建对象
显示方式
1.通过新窗口显示(相当于storyboard里的Show):
@interface ViewController ()
@property (nonatomic, strong) NSWindowController *childWindowController;
@end
@implementation ViewController
- (IBAction)presentViewController:(NSButton *)sender {
CustomViewController *viewController = [CustomViewController new];
NSWindow *window = [NSWindow windowWithContentViewController:viewController];
NSWindowController *windowController = [[NSWindowController alloc] initWithWindow:window];
// 需要强引用此对象,否则会被自动释放
self.childWindowController = windowController;
// 相当于 [window makeKeyAndOrderFront:nil];
[windowController showWindow:nil];
}
- (void)viewDidLoad {
[super viewDidLoad];
// 需要监听窗口关闭通知,用于清理强引用NSWindowController对象
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowWillClose:) name:NSWindowWillCloseNotification object:nil];
}
- (void)windowWillClose:(NSNotification *)notification {
if (notification.object == self.childWindowController.window) {
self.childWindowController = nil;
}
}
NSViewController-Show.gif
2.通过Sheet弹窗显示(弹窗位置跟随父窗口,父窗口无法响应事件):
[self presentViewControllerAsSheet:viewController];
// 在父视图控制器里关闭弹窗
[self dismissViewController:viewController];
// 在子视图控制器里关闭自身
[self dismissController:nil];
macOS 10.15及之前版本效果(固定在顶部):
NSViewController-Sheet.gifmacOS 11及之后版本效果(居中):
NSViewController-macOS11 Sheet.gif3.通过Modal窗口显示(其他窗口无法响应事件):
[self presentViewControllerAsModalWindow:viewController];
// 在父视图控制器里关闭弹窗
[self dismissViewController:viewController];
// 在子视图控制器里关闭自身
[self dismissController:nil];
*效果和Show类似,区别在于这种情况下其他窗口无法响应事件,且UI线程被阻塞
NSViewController-Show.gif
4.通过Popover弹窗显示(点击弹窗以外的地方时消失)
- (IBAction)presentViewController:(NSButton *)sender {
CustomViewController *viewController = [CustomViewController new];
[self presentViewController:viewController
asPopoverRelativeToRect:sender.bounds
ofView:sender
preferredEdge:NSRectEdgeMinY
behavior:NSPopoverBehaviorTransient];
// NSPopoverBehaviorApplicationDefined:点击弹窗以外的地方不会消失
// NSPopoverBehaviorTransient/NSPopoverBehaviorSemitransient:点击弹窗以外的地方会消失
}
// 在父视图控制器里关闭弹窗
[self dismissViewController:viewController];
// 在子视图控制器里关闭自身
[self dismissController:nil];
NSViewController-Popover.gif
生命周期
-[NSViewController initWithCoder:]
-[NSViewController awakeFromNib]
-[NSViewController loadView]
-[NSViewController awakeFromNib]
-[NSViewController viewDidLoad]
-[NSViewController updateViewConstraints]
-[NSViewController viewWillLayout],-[NSViewController viewDidLayout]
-[NSViewController viewWillAppear]
-[NSViewController updateViewConstraints]
-[NSViewController viewWillLayout],-[NSViewController viewDidLayout]
-[NSViewController viewDidAppear]
-[NSViewController viewWillLayout],-[NSViewController viewDidLayout]
...
-[NSViewController viewWillDisappear]
-[NSViewController viewDidDisappear]
-[NSViewController dealloc]
方法
// 相当于调用了[[CustomViewController alloc] initWithNibName:nil bundle:[NSBundle mainBundle]]
+ (instancetype)new;
// 假如xib文件找不到或里面没有控件,view为nil且不执行-[NSViewController viewDidLoad],需要在-[NSViewController loadView]里设置view
// OSX 10.10 以后 “nibNameOrNil”如果为空,会尝试查找同类名的xib文件
- (instancetype)initWithNibName:(nullable NSNibName)nibNameOrNil bundle:(nullable NSBundle *)nibBundleOrNil NS_DESIGNATED_INITIALIZER;
// 通过xib、storyboard文件初始化的
- (nullable instancetype)initWithCoder:(NSCoder *)coder NS_DESIGNATED_INITIALIZER;
// 通过-[NSViewController initWithNibName:bundle:]创建的示例才有值
@property (nullable, copy, readonly) NSNibName nibName;
@property (nullable, strong, readonly) NSBundle *nibBundle;
// 可理解为默认的模型对象,比如-[NSCollectionView setContent:] 会通过此属性把数组里的对象依次传递给-[NSCollectionView itemPrototype] 里的NSCollectionViewItem实例
@property (nullable, strong) id representedObject;
// 标题,并不会显示出来,如果需要设置窗口标题,可使用self.view.window.title(self为NSViewController实例)
@property (nullable, copy) NSString *title;
// 假如未设置,会调用[self loadView]通过xib、storyboard文件生成view,假如不是通过xib、storyboard初始化的需要自己设置view属性,设置了之后会调用 -viewDidLoad 方法
@property (strong) IBOutlet NSView *view;
// 不建议直接调用此方法,可通过-[NSViewController view]间接调用
// 默认通过[self nibName]和[self nibBundle]生成view,假如没有xib文件,就需要自己设置view属性
// OSX 10.10之后[self nibName]假如为nil会尝试查找同类名的xib文件
- (void)loadView;
// 视图已加载,一般在此初始化属性,注意此时还没有窗口(self.view.window)
- (void)viewDidLoad API_AVAILABLE(macos(10.10));
// 视图已加载
@property (readonly, getter=isViewLoaded) BOOL viewLoaded API_AVAILABLE(macos(10.10));
// view添加到superview之前调用
- (void)viewWillAppear API_AVAILABLE(macos(10.10));
// view添加到superview之后调用,此时才有窗口(self.view.window)
- (void)viewWillAppear API_AVAILABLE(macos(10.10));
// 视图(self.view)被隐藏或移除、所在的窗口(self.view.window)被关闭或最小化时调用,注意窗口后置或被其他窗口覆盖时不会调用
- (void)viewWillDisappear API_AVAILABLE(macos(10.10));
- (void)viewDidDisappear API_AVAILABLE(macos(10.10));
// 预设的视图大小,有些控件会调用此方法设置view的frame
@property NSSize preferredContentSize API_AVAILABLE(macos(10.10));
// 更新视图约束
- (void)updateViewConstraints API_AVAILABLE(macos(10.10));
// 布局
- (void)viewWillLayout API_AVAILABLE(macos(10.10));
- (void)viewDidLayout API_AVAILABLE(macos(10.10));
...
// 父视图控制器
@property (nullable, readonly) NSViewController *parentViewController API_AVAILABLE(macos(10.10));
// 子视图控制器
@property (copy) NSArray<__kindof NSViewController *> *childViewControllers API_AVAILABLE(macos(10.10));
// 添加子视图控制器
- (void)addChildViewController:(NSViewController *)childViewController API_AVAILABLE(macos(10.10));
// 从父视图控制器移除自身
- (void)removeFromParentViewController API_AVAILABLE(macos(10.10));
// 插入子视图控制器
- (void)insertChildViewController:(NSViewController *)childViewController atIndex:(NSInteger)index API_AVAILABLE(macos(10.10));
// 移除子视图控制器
- (void)removeChildViewControllerAtIndex:(NSInteger)index API_AVAILABLE(macos(10.10));
// 子视图控制器preferredContentSize已更改
- (void)preferredContentSizeDidChangeForViewController:(NSViewController *)viewController API_AVAILABLE(macos(10.10));
- (void)viewWillTransitionToSize:(NSSize)newSize API_AVAILABLE(macos(10.10));
// 通过storyboard文件创建的才有
@property(nullable, readonly, strong) NSStoryboard *storyboard API_AVAILABLE(macos(10.10));