NSPathControl with popups for each component of the path? - objective-c

From Apple's sample code and reading the docs I can see no way configuring the NSPathControl to behave similarly to e.g. the 'jump bar' in the Xcode Editor window:
I.e. have it represent a path (or other kind of hierarchy) and make each component of the path a clickable popup to navigate the hierarchy..?
Anybody having luck faking such behaviour using a NSPathControlDelegate listening to clicks and showing a menu in a temporary window?
Seems like a common design where one would even expect some OSS implementation - but no such luck yet googling for it..

I made a subclass of NSPathControl so that I can use mouseDown: to popup the context menus of the component cells at the right position. I added also a delegate to the menu to create deeper menus on demand.
- (void)mouseDown:(NSEvent *)event {
NSPoint point = [self convertPoint:[event locationInWindow] fromView:nil];
NSPathCell *cell = self.cell;
NSPathComponentCell *componentCell = [cell pathComponentCellAtPoint:point
withFrame:self.bounds
inView:self];
NSRect componentRect = [cell rectOfPathComponentCell:componentCell
withFrame:self.bounds
inView:self];
NSMenu *menu = [componentCell menuForEvent:event
inRect:componentRect
ofView:self];
if (menu.numberOfItems > 0) {
NSUInteger selectedMenuItemIndex = 0;
for (NSUInteger menuItemIndex = 0; menuItemIndex < menu.numberOfItems; menuItemIndex++) {
if ([[menu itemAtIndex:menuItemIndex] state] == NSOnState) {
selectedMenuItemIndex = menuItemIndex;
break;
}
}
NSMenuItem *selectedMenuItem = [menu itemAtIndex:selectedMenuItemIndex];
[menu popUpMenuPositioningItem:selectedMenuItem
atLocation:NSMakePoint(NSMinX(componentRect) - 17, NSMinY(componentRect) + 2)
inView:self];
}
}
- (NSMenu *)menuForEvent:(NSEvent *)event {
if (event.type != NSLeftMouseDown) {
return nil;
}
return [super menuForEvent:event];
}

I extended Stephan's answer slightly to accommodate for lazily loading the menu items. I created a small protocol to call for the menu rather than having to build the menu's ahead of time for each cell:
NSPathControlExtended.h
#protocol NSPathControlExtendedDelegate <NSPathControlDelegate>
#required
- (NSMenu *)pathControl:(NSPathControl *)pathControl menuForCell:(NSPathComponentCell *)cell;
#end
#interface NSPathControlExtended : NSPathControl
#property (weak) id <NSPathControlExtendedDelegate> delegate;
#end
NSPathControlExtended.m
#import "NSPathControlExtended.h"
#implementation NSPathControlExtended
#synthesize delegate;
- (instancetype)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
// Drawing code here.
}
- (void)mouseDown:(NSEvent *)event {
NSPoint point = [self convertPoint:[event locationInWindow] fromView:nil];
NSPathCell *cell = self.cell;
NSPathComponentCell *componentCell = [cell pathComponentCellAtPoint:point
withFrame:self.bounds
inView:self];
NSRect componentRect = [cell rectOfPathComponentCell:componentCell
withFrame:self.bounds
inView:self];
NSMenu *menu = [delegate pathControl:self menuForCell:componentCell];
if (menu.numberOfItems > 0) {
NSUInteger selectedMenuItemIndex = 0;
for (NSUInteger menuItemIndex = 0; menuItemIndex < menu.numberOfItems; menuItemIndex++) {
if ([[menu itemAtIndex:menuItemIndex] state] == NSOnState) {
selectedMenuItemIndex = menuItemIndex;
break;
}
}
NSMenuItem *selectedMenuItem = [menu itemAtIndex:selectedMenuItemIndex];
[menu popUpMenuPositioningItem:selectedMenuItem
atLocation:NSMakePoint(NSMinX(componentRect) - 17, NSMinY(componentRect) + 2)
inView:self];
}
}
- (NSMenu *)menuForEvent:(NSEvent *)event {
if (event.type != NSLeftMouseDown) {
return nil;
}
return [super menuForEvent:event];
}
#end

Related

The right 1/3 of a button created using a SKSpriteNode is not detecting touches

In my game, I am creating numerous buttons. I am finding that many of the buttons are not detecting when a user touches down/up on the RIGHT side of the button. In other words, the button responds correctly when touched on the LEFT side of the button, but not the RIGHT side. In short, if you sliced the button into three parts, the right 1/3 of the button does not respond while the other 2/3 of the button work perfectly. Below is the code that I have used to implement my buttons. Can anyone tell me what I might be doing wrong? I did notice when I first started using Sprite Kit that my buttons would not respond at all when the SKSpriteNode::zPosition property was NOT set to 1 or greater...once I set this property my buttons started working. This zPosition problem occurred whether or not the button sprite node was the only sprite node in the scene or not which makes me think that the problem is not an overlap issue either. Got any ideas?
I have extended the SKSpriteNode class to have an additional method GESpriteNode::setGestureHandler:withKey:, which have the following signatures:
- (void) setGestureHandler: (void (^)(SKNode *node, GEGestureRecognizer *recognizer))handler withKey: (NSString *)key;
- (void) removeGestureHandlerWithKey: (NSString *)key;
And, implemented like so (where _event is an NSMutableDictionary):
- (void) setGestureHandler: (void (^)(SKNode *node, GEGestureRecognizer *recognizer))handler withKey: (NSString *)key {
if (key != nil) {
[_events setObject: [handler copy] forKey: key];
}
}
- (void) removeGestureHandlerWithKey: (NSString *)key {
if (key != nil) {
[_events removeObjectForKey: key];
}
}
- (void) touchesBegan: (NSSet *)touches withEvent: (UIEvent *)event {
if (![self isHidden]) {
void (^handler)(SKNode *, GEGestureRecognizer *);
handler = [_events objectForKey: GETouchDownGestureHandlerKey];
if (handler != nil) {
GEGestureRecognizer *recognizer = [[GEGestureRecognizer alloc] initWithScene: [self scene] withTouches: touches];
if (recognizer != nil) {
handler(self, recognizer);
}
}
}
}
- (void) touchesEnded: (NSSet *)touches withEvent: (UIEvent *)event {
if (![self isHidden]) {
void (^handler)(SKNode *, GEGestureRecognizer *);
handler = [_events objectForKey: GETouchUpGestureHandlerKey];
if (handler != nil) {
GEGestureRecognizer *recognizer = [[GEGestureRecognizer alloc] initWithScene: [self scene] withTouches: touches];
if (recognizer != nil) {
handler(self, recognizer);
}
}
}
}
They are used as follows:
GESpriteNode *spriteNode = [GESpriteNode spriteNodeWithImageNamed: NODE_BUTTON_1];
[spriteNode setAlpha: ALPHA_OPAQUE];
[spriteNode setGestureHandler: ^(SKNode *node, GEGestureRecognizer *recognizer) {
[node setScale: SCALE_95];
} withKey: GETouchDownGestureHandlerKey];
[spriteNode setGestureHandler: ^(SKNode *node, GEGestureRecognizer *recognizer) {
[node setScale: SCALE_100];
} withKey: GETouchUpGestureHandlerKey];
[spriteNode setHidden: NO];
[spriteNode setName: NODE_BUTTON_1];
[spriteNode setPosition: CGPointMake(384.0f, 387.0f)];
[spriteNode setScale: SCALE_100];
[spriteNode setUserInteractionEnabled: YES];
[spriteNode setZPosition: 1];
[self addChild: spriteNode];
My GEGestureRecognizer.h class is defined as so:
#import <SpriteKit/SpriteKit.h>
extern NSString * const GETouchDownGestureHandlerKey;
extern NSString * const GETouchUpGestureHandlerKey;
#interface GEGestureRecognizer : NSObject
// public properties
#property (nonatomic, strong) SKScene *scene;
// public methods
- (instancetype) initWithScene: (SKScene *)scene withTouches: (NSSet *)touches;
- (CGPoint) locationInScene: (SKScene *)scene;
#end
And, its GEGestureRecognizer.m is defined as:
#import "GEGestureRecognizer.h"
#pragma mark -
#pragma mark Constants
NSString * const GETouchDownGestureHandlerKey = #"GETouchDownGestureHandlerKey";
NSString * const GETouchUpGestureHandlerKey = #"GETouchUpGestureHandlerKey";
#interface GEGestureRecognizer () {
#private
SKScene *_scene;
NSSet *_touches;
}
#end
#implementation GEGestureRecognizer
#synthesize scene = _scene;
- (instancetype) initWithScene: (SKScene *)scene withTouches: (NSSet *)touches {
if ((self = [super init])) {
_scene = scene;
_touches = touches;
}
return self;
}
- (CGPoint) locationInScene: (SKScene *)scene {
CGFloat x = 0;
CGFloat y = 0;
CGFloat k = 0;
for (UITouch *touch in _touches) {
const CGPoint point = [touch locationInNode: scene];
x += point.x;
y += point.y;
k++;
}
if (k > 0) {
return CGPointMake(x/k, y/k);
}
return CGPointZero;
}
#end
What exactly is this code doing? Why do you increase x and y values? Why do you need k value? I think you are offsetting the location of touch, try logging all touch locations and drawing them on screen.
There are native methods to detect touch in nodes and translate coordinates to scene locations.
Recreating the GestureRecognizer on each event seems rather inefficient, the locationInScene does not make much sense either when multiple touches occur (no idea what you're trying to achieve there)
That said, if your buttons only started to work when you change the zPosition, I assume you do have an overlap issue.
Since there are other nodes involved (whether visible or not) at (default) zPosition 0, you'd have to show more code to get help.

Having Draggable NSTabViewItem

is it possible to provide draggable NSTabViewItem,
Basically what i want, if i down L Button on the label of NSTabViewITem and Move, i should allow to drag the TabView item,
I want to do it for Moving of NSTabView Item and have one more feature, if user drag a Label of NSTabView Item and move it to a perticular region, then i should allow to remove that NSTabView Item,
I could able to find only one way of having PSMTab bar, but i have other features also on NSTabView Item that i will be missing if i go with that approach.
Thanks for looking into it,
Somehow i could able to do it.... posting some important piece of code...
1 -- Have to have Custom TabView class for Handling mouse events.
// Interface posted below,
#import <Cocoa/Cocoa.h>
typedef enum __itemDragState{
itemNotDragging = 0,
itemDragStatNormal = 0,
itemDragging = 1,
itemDropped = 2
} ItemDragStat;
#protocol CustomTabViewDelegate <NSObject>
#required
-(bool)allowDrag;
-(bool)allowDrop;
-(void)dragEnter;
-(void)acceptDrop;
-(void)draggingCancelled;
-(void)itemDropped:(id)draggedTabViewItem;
-(void)itemDroppedCompleted:(id)droppedTabViewItem;
#end
#interface CustomTab : NSTabView{
ItemDragStat eItemDragStat;
id draggedItem;
}
#property(assign)id draggedItem;
#end
Now some of the important implementation
#import "CustomTab.h"
#include "Log.h"
#implementation CustomTab
#synthesize draggedItem;
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
}
return self;
}
# if 0
// don't delete it, might need later on
- (void)drawRect:(NSRect)dirtyRect
{
// Drawing code here.
}
# endif
- (void)mouseUp:(NSEvent *)theEvent{
log(" Mouse up ");
NSPoint location = [self convertPoint: [theEvent locationInWindow]
fromView: nil];
NSTabViewItem *anItem = [self tabViewItemAtPoint: location];
if ( anItem == nil ) {
// if its mouse up else where, reject dragging regardless
eItemDragStat = itemDragStatNormal;
log("Item will not be dropped");
return;
}
if ( ![anItem isEqual:[self selectedTabViewItem]]){
log("Mouse up is in nonselected item");
if ( eItemDragStat == itemDragging){
log("Item will be dropped into this ");
id droppedTabViewItem = anItem;
if ( droppedTabViewItem && [droppedTabViewItem respondsToSelector:#selector(itemDropped:)]){
id selectedTabViewItem = [self selectedTabViewItem];
[droppedTabViewItem performSelector:#selector(itemDropped:) withObject:selectedTabViewItem];
}
}
}
eItemDragStat = itemDragStatNormal;
// return;
// [super mouseUp:theEvent];
}
- (void)mouseDown:(NSEvent *)theEvent{
NSPoint location = [self convertPoint: [theEvent locationInWindow]
fromView: nil];
draggedItem = [self tabViewItemAtPoint:location];
NSTabViewItem *anItem = [self tabViewItemAtPoint: location];
if (anItem != nil && ![anItem isEqual: [self selectedTabViewItem]])
{
[self selectTabViewItem: anItem];
}
}
- (void)mouseDragged:(NSEvent *)theEvent{
NSPoint location = [self convertPoint: [theEvent locationInWindow]
fromView: nil];
id tabViewItemId = [self tabViewItemAtPoint:location];
NSTabViewItem *anItem = [self tabViewItemAtPoint: location];
if (anItem){
if (![anItem isEqual:draggedItem]){
if (tabViewItemId && [tabViewItemId respondsToSelector:#selector(allowDrag)]){
eItemDragStat = itemDragging;
}else{
// drag will be cancelled now.
// tell client item to stop dragging
if (eItemDragStat == itemDragging){
if ( draggedItem && [ draggedItem respondsToSelector:#selector(draggingCancelled)]){
[draggedItem performSelector:#selector(draggingCancelled)];
draggedItem = nil;
}
}
eItemDragStat = itemNotDragging;
// if we have +cursor then it should be reset
}
}else{
log(" Mouse dragged");
}
}else{
// dragging went elsewhere, lets close this dragging operation
if ( draggedItem && [ draggedItem respondsToSelector:#selector(draggingCancelled)]){
[draggedItem performSelector:#selector(draggingCancelled)];
draggedItem = nil;
}
// here reset the mouse pointer
eItemDragStat = itemNotDragging;
}
}
#end
It needs some more fine tuning and its going on....

Mac app - drag and drop files into a menubar application

My inspiration for this project is the Droplr and CloudApp mac menubar applications. I have been attempting to implement the code explained here.
However, when I implement the code, the menubar images disappear. Here is my code to create the status item:
- (id)init {
self = [super init];
if (self != nil) {
// Install status item into the menu bar
NSStatusItem *statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:STATUS_ITEM_VIEW_WIDTH];
_statusItemView = [[StatusItemView alloc] initWithStatusItem:statusItem];
_statusItemView.image = [NSImage imageNamed:#"Status"];
_statusItemView.alternateImage = [NSImage imageNamed:#"StatusHighlighted"];
_statusItemView.action = #selector(togglePanel:);
StatusItemView* dragView = [[StatusItemView alloc] initWithFrame:NSMakeRect(0, 0, 24, 24)];
[statusItem setView:dragView];
[dragView release];
}
return self;
}
This is my view file:
#import "StatusItemView.h"
#implementation StatusItemView
#synthesize statusItem = _statusItem;
#synthesize image = _image;
#synthesize alternateImage = _alternateImage;
#synthesize isHighlighted = _isHighlighted;
#synthesize action = _action;
#synthesize target = _target;
#pragma mark -
- (id)initWithStatusItem:(NSStatusItem *)statusItem {
CGFloat itemWidth = [statusItem length];
CGFloat itemHeight = [[NSStatusBar systemStatusBar] thickness];
NSRect itemRect = NSMakeRect(0.0, 0.0, itemWidth, itemHeight);
self = [super initWithFrame:itemRect];
if (self != nil) {
_statusItem = statusItem;
_statusItem.view = self;
}
return self;
}
#pragma mark -
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
//register for drags
[self registerForDraggedTypes:[NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
}
return self;
}
//we want to copy the files
- (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender {
return NSDragOperationCopy;
}
perform the drag and log the files that are dropped
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender {
NSPasteboard *pboard;
NSDragOperation sourceDragMask;
sourceDragMask = [sender draggingSourceOperationMask];
pboard = [sender draggingPasteboard];
if( [[pboard types] containsObject:NSFilenamesPboardType] ) {
NSArray *files = [pboard propertyListForType:NSFilenamesPboardType];
NSLog(#"Files: %#",files);
}
return YES;
}
#pragma mark -
- (void)drawRect:(NSRect)dirtyRect {
[self.statusItem drawStatusBarBackgroundInRect:dirtyRect withHighlight:self.isHighlighted];
NSImage *icon = self.isHighlighted ? self.alternateImage : self.image;
NSSize iconSize = [icon size];
NSRect bounds = self.bounds;
CGFloat iconX = roundf((NSWidth(bounds) - iconSize.width) / 2);
CGFloat iconY = roundf((NSHeight(bounds) - iconSize.height) / 2);
NSPoint iconPoint = NSMakePoint(iconX, iconY);
[icon compositeToPoint:iconPoint operation:NSCompositeSourceOver];
}
#pragma mark -
#pragma mark Mouse tracking
- (void)mouseDown:(NSEvent *)theEvent {
[NSApp sendAction:self.action to:self.target from:self];
}
#pragma mark -
#pragma mark Accessors
- (void)setHighlighted:(BOOL)newFlag {
if (_isHighlighted == newFlag) return;
_isHighlighted = newFlag;
[self setNeedsDisplay:YES];
}
#pragma mark -
- (void)setImage:(NSImage *)newImage {
if (_image != newImage) {
_image = newImage;
[self setNeedsDisplay:YES];
}
}
- (void)setAlternateImage:(NSImage *)newImage {
if (_alternateImage != newImage) {
_alternateImage = newImage;
if (self.isHighlighted) {
[self setNeedsDisplay:YES];
}
}
}
#pragma mark -
- (NSRect)globalRect {
NSRect frame = [self frame];
frame.origin = [self.window convertBaseToScreen:frame.origin];
return frame;
}
#end
Thanks everyone!
I know this is an old question but perhaps this might help:
Try setting the NSStatusItem *statusItem as a #propery of the class. If you have ARC, the menubar might be getting destroyed right after the init function finishes.

Array of buttons returning null

I am trying to add the buttons I create to an array and then remove them buttons from the array. My array keeps returning null so I get the feeling my buttons are not even being added to my array?
I am a beginner. I am using Xcode 4.3. Here is my code:
//
// MainViewController.h
// Test-Wards
//
// Created by Dayle Pearson on 5/12/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "FlipsideViewController.h"
#interface MainViewController : UIViewController <FlipsideViewControllerDelegate>
{
/*This stuff creates a timer */
IBOutlet UILabel *opponentsBlue;
NSTimer *timer;
int redBlue;
/*Stuff for making a label creator */
CGPoint startPoint;
int xStuff, yStuff;
/*array for storing wards*/
NSMutableArray *wardArray;
}
#property CGPoint startPoint;
- (IBAction)startRedBlue:(id)sender;
- (IBAction)removeWard:(id)
sender;
- (void)countdown;
#end
//
// MainViewController.m
// Test-Wards
//
// Created by Dayle Pearson on 5/12/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "MainViewController.h"
#interface MainViewController ()
#end
#implementation MainViewController
#synthesize startPoint;
- (void)countdown
{
if (redBlue < 2) {
[timer invalidate];
timer = nil;
}
redBlue -= 1;
opponentsBlue.text = [NSString stringWithFormat:#"%i", redBlue];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *theTouch = [touches anyObject];
startPoint = [theTouch locationInView:self.view];
}
- (IBAction)startRedBlue:(id)sender
{
UIButton *wardButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
wardButton.frame = CGRectMake((startPoint.x - 5), (startPoint.y - 5), 10, 10);
[wardButton setTitle:#"180" forState:UIControlStateNormal];
//add targets and actions
/*[wardButton addTarget:self action:#selector() forControlEvents:<#(UIControlEvents)#>*/
//add to a view
[self.view addSubview:wardButton];
[self->wardArray addObject: wardButton];
NSLog(#"This elemnt = %#", wardArray);
}
- (IBAction)removeWard:(id)sender
{
[self->wardArray removeLastObject];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#pragma mark - Flipside View
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller
{
[self dismissModalViewControllerAnimated:YES];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"showAlternate"]) {
[[segue destinationViewController] setDelegate:self];
}
}
#end
You forgot to initialize your wardArray. You should add
wardArray = [NSMutableArray array];
to your designated initializer.
In Objective-C sending messages to nil objects is legal - these messages are simply ignored. That's why you do not see the items that you added.
I also noticed that you add buttons to the view, but you never remove them. To remove the buttons from the screen, change the code as follows:
- (IBAction)removeWard:(id)sender
{
[[self->wardArray lastObject] removeFromSuperview];
[self->wardArray removeLastObject];
}
You have to initialize your array before you can add to it
wardArray = [NSMutableArray array]; // quick and easy
wardArray = [[NSMutableArray alloc] init]; // oldschool
I recommend doing it like so in your method. This will only initialize it once if it doesn't exist so theres no chance of it never being ready to have objects :)
- (IBAction)startRedBlue:(id)sender
{
....
// If wardArray doesn't exist we create it. Otherwise we add our ward to it.
if (!wardArray) {
wardArray = [NSMutableArray array];
} else {
[self->wardArray addObject: wardButton];
}
}
- (IBAction)removeWard:(id)sender
{
UIButton *ward = (UIButton *)sender;
[ward removeFromSuperview]; // Takes it off the screen.
[self->wardArray removeObject:ward]; //Takes it out of the array
}

How do I use UIPageViewController to load separate XIBs?

I'm delving into the new world of UIPageViewControllers and there are a lot of tutorials out there, however all of them seem to create one view, and then just use new instances of it with different content.
I'd really like to be able to create multiple XIBs and then just chain them together with the UIPageViewController but it's too new and I can't get my head around the way it works.
Well, here's a long answer that you should be able to copy and paste. (This code was adapted from Erica Sadun (https://github.com/erica/iOS-5-Cookbook))
First, create a new class of type UIPageViewController. Call it BookController. Now paste the following code in your .h file.
// Used for storing the most recent book page used
#define DEFAULTS_BOOKPAGE #"BookControllerMostRecentPage"
#protocol BookControllerDelegate <NSObject>
- (id) viewControllerForPage: (int) pageNumber;
#optional
- (void) bookControllerDidTurnToPage: (NSNumber *) pageNumber;
#end
#interface BookController : UIPageViewController <UIPageViewControllerDelegate, UIPageViewControllerDataSource>
+ (id) bookWithDelegate: (id) theDelegate;
+ (id) rotatableViewController;
- (void) moveToPage: (uint) requestedPage;
- (int) currentPage;
#property (assign) id <BookControllerDelegate> bookDelegate;
#property (nonatomic, assign) uint pageNumber;
and in your .m file:
#define IS_IPHONE ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
#define SAFE_ADD(_Array_, _Object_) {if (_Object_ && [_Array_ isKindOfClass:[NSMutableArray class]]) [pageControllers addObject:_Object_];}
#define SAFE_PERFORM_WITH_ARG(THE_OBJECT, THE_SELECTOR, THE_ARG) (([THE_OBJECT respondsToSelector:THE_SELECTOR]) ? [THE_OBJECT performSelector:THE_SELECTOR withObject:THE_ARG] : nil)
#pragma Utility Class - VC that Rotates
#interface RotatableVC : UIViewController
#end
#implementation RotatableVC
- (void) loadView
{
[super loadView];
self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
self.view.backgroundColor = [UIColor whiteColor];
}
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return YES;
}
#end
#pragma Book Controller
#implementation BookController
#synthesize bookDelegate, pageNumber;
#pragma mark Debug / Utility
- (int) currentPage
{
int pageCheck = ((UIViewController *)[self.viewControllers objectAtIndex:0]).view.tag;
return pageCheck;
}
#pragma mark Page Handling
// Update if you'd rather use some other decision style
- (BOOL) useSideBySide: (UIInterfaceOrientation) orientation
{
BOOL isLandscape = UIInterfaceOrientationIsLandscape(orientation);
return isLandscape;
}
// Store the new page and update the delegate
- (void) updatePageTo: (uint) newPageNumber
{
pageNumber = newPageNumber;
[[NSUserDefaults standardUserDefaults] setInteger:pageNumber forKey:DEFAULTS_BOOKPAGE];
[[NSUserDefaults standardUserDefaults] synchronize];
SAFE_PERFORM_WITH_ARG(bookDelegate, #selector(bookControllerDidTurnToPage:), [NSNumber numberWithInt:pageNumber]);
}
// Request controller from delegate
- (UIViewController *) controllerAtPage: (int) aPageNumber
{
if (bookDelegate &&
[bookDelegate respondsToSelector:#selector(viewControllerForPage:)])
{
UIViewController *controller = [bookDelegate viewControllerForPage:aPageNumber];
controller.view.tag = aPageNumber;
return controller;
}
return nil;
}
// Update interface to the given page
- (void) fetchControllersForPage: (uint) requestedPage orientation: (UIInterfaceOrientation) orientation
{
BOOL sideBySide = [self useSideBySide:orientation];
int numberOfPagesNeeded = sideBySide ? 2 : 1;
int currentCount = self.viewControllers.count;
uint leftPage = requestedPage;
if (sideBySide && (leftPage % 2)) leftPage--;
// Only check against current page when count is appropriate
if (currentCount && (currentCount == numberOfPagesNeeded))
{
if (pageNumber == requestedPage) return;
if (pageNumber == leftPage) return;
}
// Decide the prevailing direction by checking the new page against the old
UIPageViewControllerNavigationDirection direction = (requestedPage > pageNumber) ? UIPageViewControllerNavigationDirectionForward : UIPageViewControllerNavigationDirectionReverse;
[self updatePageTo:requestedPage];
// Update the controllers
NSMutableArray *pageControllers = [NSMutableArray array];
SAFE_ADD(pageControllers, [self controllerAtPage:leftPage]);
if (sideBySide)
SAFE_ADD(pageControllers, [self controllerAtPage:leftPage + 1]);
[self setViewControllers:pageControllers direction: direction animated:YES completion:nil];
}
// Entry point for external move request
- (void) moveToPage: (uint) requestedPage
{
[self fetchControllersForPage:requestedPage orientation:(UIInterfaceOrientation)[UIDevice currentDevice].orientation];
}
#pragma mark Data Source
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
[self updatePageTo:pageNumber + 1];
return [self controllerAtPage:(viewController.view.tag + 1)];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
[self updatePageTo:pageNumber - 1];
return [self controllerAtPage:(viewController.view.tag - 1)];
}
#pragma mark Delegate
- (UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation
{
NSUInteger indexOfCurrentViewController = 0;
if (self.viewControllers.count)
indexOfCurrentViewController = ((UIViewController *)[self.viewControllers objectAtIndex:0]).view.tag;
[self fetchControllersForPage:indexOfCurrentViewController orientation:orientation];
BOOL sideBySide = [self useSideBySide:orientation];
self.doubleSided = sideBySide;
UIPageViewControllerSpineLocation spineLocation = sideBySide ? UIPageViewControllerSpineLocationMid : UIPageViewControllerSpineLocationMin;
return spineLocation;
}
-(void)dealloc{
self.bookDelegate = nil;
}
#pragma mark Class utility routines
// Return a UIViewController that knows how to rotate
+ (id) rotatableViewController
{
UIViewController *vc = [[RotatableVC alloc] init];
return vc;
}
// Return a new book
+ (id) bookWithDelegate: (id) theDelegate
{
BookController *bc = [[BookController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
bc.dataSource = bc;
bc.delegate = bc;
bc.bookDelegate = theDelegate;
return bc;
}
This Class can now be used to control any book you create in any project, and for multiple books in a single project. For each book, create a delegate UIPageViewController with the #interface:
#interface NameOfBookController : UIPageViewController <BookControllerDelegate>
In the .m file of this delegate, include:
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
[super loadView];
CGRect appRect = [[UIScreen mainScreen] applicationFrame];
self.view = [[UIView alloc] initWithFrame: appRect];
self.view.backgroundColor = [UIColor whiteColor];
self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
// Establish the page view controller
bookController = [BookController bookWithDelegate:self];
bookController.view.frame = (CGRect){.size = appRect.size};
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
// Add the child controller, and set it to the first page
[self.view addSubview:bookController.view];
[self addChildViewController:bookController];
[bookController didMoveToParentViewController:self];
}
Then add:
- (id) viewControllerForPage: (int) pageNumber
{
// Establish a new controller
UIViewController *controller;
switch (pageNumber) {
case 0:
view1 = [[FirstViewController alloc] init];
view1.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
controller = view1;
//rinse and repeat with each new view controller
break;
case 1:
//etc.
break;
default:
return nil;
break;
}
return controller;
}
For the record, this code is not memory-safe. If using ARC, add in your #autoreleasePool{}; if not, don't forget your retain/release cycle.
I hope this helps!
This article shows how to create an app using UIPageViewController with custom viewcontrollers for each page: http://www.informit.com/articles/article.aspx?p=1760500&seqNum=6