Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSPageController tutorial for Cocoa on OS X

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 documentation isn't helping me.

Forgot to mention: I'm working on Mac project (NOT iOS)

like image 525
Mamphir Avatar asked Feb 10 '13 19:02

Mamphir


1 Answers

Simple NSPageController book mode tutorial

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.


Add some images to Images.xcassets

I used three images in this sample project:

  • first.png
  • second.png
  • third.png

MyAppDelegate.h

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 <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

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;
}

And it's done


If your _pageController is null/nil

Connect your Page Controller to _pageController by pressing ctrl and dragging with mouse to _pageController in MyAppDelegate.h file.

like image 84
juniperi Avatar answered Nov 03 '22 09:11

juniperi