I need to use NSPageController
in some project but I have no idea how it works, does anybody have time for some simple tutorial to show me because the documentatio
Create new Cocoa Application-project. Open MainMenu.xib
in interface builder and add Image Well and Label objects to your application window. Also add Page Controller object.
Set Page Controller's view to point Image Well.
Images.xcassets
I used three images in this sample project:
Add referencing outlets for Page Controller, Label and Image Well. Set MyAppDelegate
as NSPageControllerDelegate
and add also NSArray
for images. After this your MyAppDelegate.h
file should look like this:
@interface MyAppDelegate : NSObject
@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
Some initialization:
- (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];
}
Page Controller delegate methods:
- (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;
}
Connect your Page Controller to _pageController by pressing ctrl
and dragging with mouse to _pageController in MyAppDelegate.h
file.