在OS X上的Cocoa的NSPageController教程 [英] NSPageController tutorial for Cocoa on OS X

查看:1337
本文介绍了在OS X上的Cocoa的NSPageController教程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在一些项目中使用 NSPageController ,但我不知道它是如何工作的,有没有时间为一些简单的教程显示我,因为文档不是

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.

忘记提及:我正在处理 Mac项目 > iOS )

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

推荐答案

Simple NSPageController book mode tutorial



创建新的 Cocoa Application 项目。在界面构建器中打开 MainMenu.xib ,并将 Image Well 标签对象添加到应用程序窗口。还要添加页面控制器对象。

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.

设置页面控制器视图以指向 Image Well

示例项目:


  • first.png

  • second.png

  • third.png

页面控制器标签图像井添加引用出口。将 MyAppDelegate 设置为 NSPageControllerDelegate ,并为图像添加 NSArray 。之后,您的 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



一些初始化:


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

页面控制器委托方法:

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






h1>



And it's done

通过按 ctrl 将页面控制器连接到_pageController,然后使用鼠标拖动到< c> MyAppDelegate.h 档案。

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

这篇关于在OS X上的Cocoa的NSPageController教程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆