NSPageController tutorial for Cocoa on OS X

后端 未结 1 2033
故里飘歌
故里飘歌 2021-02-06 00:27

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

1条回答
  •  野性不改
    2021-02-06 01:00

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

    0 讨论(0)
提交回复
热议问题