我需要在某些项目中使用NSPageController
,但我不知道它是如何工作的,是否有人有时间通过一些简单的教程向我展示,因为文档没有帮助我。
忘了提及:我正在研究Mac project
(不是 iOS
)
最佳答案
NSPageController简单书本模式教程
创建新的 cocoa 应用程序 -project。在界面构建器中打开MainMenu.xib
,然后将 Image Well 和标签对象添加到您的应用程序窗口。还添加页面 Controller 对象。
设置页面 Controller 的 View 以指向图像井。
将一些图像添加到Images.xcassets
我在此示例项目中使用了三张图片:
MyAppDelegate.h
添加页面 Controller ,标签和图像引用的引用导出。将
MyAppDelegate
设置为NSPageControllerDelegate
并为图像添加NSArray
。之后,您的MyAppDelegate.h
文件应如下所示:@interface MyAppDelegate : NSObject <NSApplicationDelegate, NSPageControllerDelegate>
@property (assign) IBOutlet NSWindow *window;
@property (unsafe_unretained) IBOutlet NSPageController *pageController;
@property (weak) IBOutlet NSImageView *imageView;
@property (weak) IBOutlet NSTextField *infoLabel;
@property (nonatomic) NSArray *imageArray;
@end
MyAppDelegate.m
一些初始化:
- (void)awakeFromNib {
_imageArray = @[ [NSImage imageNamed:@"first"],
[NSImage imageNamed:@"second"],
[NSImage imageNamed:@"third"]];
/* Set delegate for NSPageControl */
[_pageController setDelegate:self];
/* Set arranged objects for NSPageControl */
[_pageController setArrangedObjects:_imageArray];
/* Set transition style, in this example we use book style */
[_pageController setTransitionStyle:NSPageControllerTransitionStyleStackBook];
/* Set info label's text */
NSString *info = [NSString stringWithFormat:@"Image %ld/%ld", ([_pageController selectedIndex]+1), [_imageArray count]];
[_infoLabel setStringValue:info];
}
页面 Controller 委托(delegate)方法:- (void)pageController:(NSPageController *)pageController didTransitionToObject:(id)object {
/* When image is changed, update info label's text */
NSString *info = [NSString stringWithFormat:@"Image %ld/%ld", ([_pageController selectedIndex]+1), [_imageArray count]];
[_infoLabel setStringValue:info];
}
- (NSString *)pageController:(NSPageController *)pageController identifierForObject:(id)object {
/* Returns object's array index as identiefier */
NSString *identifier = [[NSNumber numberWithInteger:[_imageArray indexOfObject:object]] stringValue];
return identifier;
}
- (NSViewController *)pageController:(NSPageController *)pageController viewControllerForIdentifier:(NSString *)identifier {
/* Create new view controller and image view */
NSViewController *vController = [NSViewController new];
NSImageView *iView = [[NSImageView alloc] initWithFrame:[_imageView frame]];
/* Get image from image array using identiefier and set image to view */
[iView setImage:(NSImage *)[_imageArray objectAtIndex:[identifier integerValue]]];
/* Set image view's frame style to none */
[iView setImageFrameStyle:NSImageFrameNone];
/* Add image view to view controller and return view controller */
[vController setView:iView];
return vController;
}
完成了
如果您的_pageController为null/nil
通过按
ctrl
并用鼠标拖动到MyAppDelegate.h
文件中的_pageController,将您的Page Controller连接到_pageController。关于objective-c - 适用于OS X的Cocoa的NSPageController教程,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14801513/