3 #import <UIKit/UINavigationBar.h>
4 #import <UIKit/UINavigationItem.h>
6 #import <UIKit/UITransitionView.h>
8 #import <UIKit/UIView-Geometry.h>
9 #import <UIKit/UIView-Hierarchy.h>
11 #import <Foundation/Foundation.h>
12 #import <CoreGraphics/CGGeometry.h>
21 @implementation RVBook
24 [navbar_ setDelegate:nil];
28 [transition_ release];
32 - (UINavigationBar *) navigationBar {
36 - (void) navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button {
37 _assert([pages_ count] != 0);
38 RVPage *page = [pages_ lastObject];
40 case 0: [page _rightButtonClicked]; break;
41 case 1: [page _leftButtonClicked]; break;
45 - (void) navigationBar:(UINavigationBar *)navbar poppedItem:(UINavigationItem *)item {
46 _assert([pages_ count] != 0);
48 [[pages_ lastObject] setPageActive:NO];
49 [pages_ removeLastObject];
51 [self resetViewAnimated:YES toPage:[pages_ lastObject]];
54 - (id) initWithFrame:(CGRect)frame {
55 if ((self = [super initWithFrame:frame]) != nil) {
56 pages_ = [[NSMutableArray arrayWithCapacity:4] retain];
58 struct CGRect bounds = [self bounds];
59 CGSize navsize = [UINavigationBar defaultSize];
60 CGRect navrect = {{0, 0}, navsize};
62 navbar_ = [[UINavigationBar alloc] initWithFrame:navrect];
63 [self addSubview:navbar_];
65 [navbar_ setBarStyle:0];
66 [navbar_ setDelegate:self];
68 transition_ = [[UITransitionView alloc] initWithFrame:CGRectMake(
69 bounds.origin.x, bounds.origin.y + navsize.height, bounds.size.width, bounds.size.height - navsize.height
72 [self addSubview:transition_];
76 - (void) setDelegate:(id)delegate {
80 - (void) setPage:(RVPage *)page {
81 if ([pages_ count] != 0)
82 [[pages_ lastObject] setPageActive:NO];
84 [navbar_ disableAnimation];
86 for (unsigned i(0), pages([pages_ count]); i != pages; ++i)
87 [navbar_ popNavigationItem];
91 [navbar_ enableAnimation];
94 - (void) pushPage:(RVPage *)page {
95 if ([pages_ count] != 0)
96 [[pages_ lastObject] setPageActive:NO];
98 NSString *title = [self getTitleForPage:page];
100 NSString *backButtonTitle = [page backButtonTitle];
101 if (backButtonTitle == nil)
102 backButtonTitle = title;
104 UINavigationItem *navitem = [[[UINavigationItem alloc] initWithTitle:title] autorelease];
105 [navitem setBackButtonTitle:backButtonTitle];
106 [navbar_ pushNavigationItem:navitem];
108 BOOL animated = [pages_ count] == 0 ? NO : YES;
109 [page setFrame:[transition_ bounds]];
110 [transition_ transition:(animated ? 1 : 0) toView:page];
111 [page setPageActive:YES];
113 [pages_ addObject:page];
114 [self reloadButtonsForPage:page];
116 [navbar_ setAccessoryView:[page accessoryView] animate:animated removeOnPop:NO];
119 - (void) popPages:(unsigned)pages {
123 [[pages_ lastObject] setPageActive:NO];
126 for (unsigned i(0); i != pages; ++i)
127 [navbar_ popNavigationItem];
130 [self resetViewAnimated:YES toPage:[pages_ lastObject]];
133 - (void) resetViewAnimated:(BOOL)animated {
136 if ([pages_ count] > 1) {
137 [navbar_ disableAnimation];
138 while ([pages_ count] != (animated ? 2 : 1))
139 [navbar_ popNavigationItem];
140 [navbar_ enableAnimation];
142 [navbar_ popNavigationItem];
147 [self resetViewAnimated:animated toPage:[pages_ lastObject]];
150 - (void) resetViewAnimated:(BOOL)animated toPage:(RVPage *)page {
151 [page resetViewAnimated:animated];
152 [page setFrame:[transition_ bounds]];
153 [transition_ transition:(animated ? 2 : 0) toView:page];
154 [page setPageActive:YES];
155 [self reloadButtonsForPage:page];
157 [navbar_ setAccessoryView:[page accessoryView] animate:animated removeOnPop:NO];
160 - (void) setBackButtonTitle:(NSString *)title forPage:(RVPage *)page {
161 if ([pages_ count] == 0 || page != [pages_ lastObject])
163 UINavigationItem *navitem = [navbar_ topItem];
164 [navitem setBackButtonTitle:title];
167 - (void) reloadTitleForPage:(RVPage *)page {
168 if ([pages_ count] == 0 || page != [pages_ lastObject])
170 UINavigationItem *navitem = [navbar_ topItem];
171 NSString *title = [self getTitleForPage:page];
172 [navitem setTitle:title];
175 - (void) reloadButtonsForPage:(RVPage *)page {
176 if ([pages_ count] == 0 || page != [pages_ lastObject])
178 NSString *leftButtonTitle([page leftButtonTitle]);
179 UINavigationButtonStyle leftButtonStyle = [page leftButtonStyle];
180 UINavigationButtonStyle rightButtonStyle = [page rightButtonStyle];
181 //[navbar_ showButtonsWithLeftTitle:leftButtonTitle rightTitle:[page rightButtonTitle] leftBack:(leftButtonTitle == nil)];
184 showLeftButton:leftButtonTitle
185 withStyle:leftButtonStyle
186 rightButton:[page rightButtonTitle]
187 withStyle:rightButtonStyle
191 - (NSString *) getTitleForPage:(RVPage *)page {
195 - (void) reloadData {
196 for (int i(0), e([pages_ count]); i != e; ++i) {
197 RVPage *page([pages_ objectAtIndex:(e - i - 1)]);
202 - (CGRect) pageBounds {
203 return [transition_ bounds];