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>
13 @implementation RVBook
16 [navbar_ setDelegate:nil];
20 [transition_ release];
24 - (void) navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button {
25 _assert([pages_ count] != 0);
26 RVPage *page = [pages_ lastObject];
28 case 0: [page _rightButtonClicked]; break;
29 case 1: [page _leftButtonClicked]; break;
33 - (void) navigationBar:(UINavigationBar *)navbar poppedItem:(UINavigationItem *)item {
34 _assert([pages_ count] != 0);
36 [[pages_ lastObject] setPageActive:NO];
37 [pages_ removeLastObject];
39 [self resetViewAnimated:YES toPage:[pages_ lastObject]];
42 - (id) initWithFrame:(CGRect)frame {
43 if ((self = [super initWithFrame:frame]) != nil) {
44 pages_ = [[NSMutableArray arrayWithCapacity:4] retain];
46 struct CGRect bounds = [self bounds];
47 CGSize navsize = [UINavigationBar defaultSizeWithPrompt];
48 CGRect navrect = {{0, 0}, navsize};
50 navbar_ = [[UINavigationBar alloc] initWithFrame:navrect];
51 [self addSubview:navbar_];
53 [navbar_ setBarStyle:1];
54 [navbar_ setDelegate:self];
56 [navbar_ setPrompt:@""];
58 transition_ = [[UITransitionView alloc] initWithFrame:CGRectMake(
59 bounds.origin.x, bounds.origin.y + navsize.height, bounds.size.width, bounds.size.height - navsize.height
62 [self addSubview:transition_];
66 - (void) setDelegate:(id)delegate {
70 - (void) setPage:(RVPage *)page {
71 if ([pages_ count] != 0)
72 [[pages_ lastObject] setPageActive:NO];
74 [navbar_ disableAnimation];
76 for (unsigned i(0), pages([pages_ count]); i != pages; ++i)
77 [navbar_ popNavigationItem];
81 [navbar_ enableAnimation];
84 - (void) pushPage:(RVPage *)page {
85 if ([pages_ count] != 0)
86 [[pages_ lastObject] setPageActive:NO];
88 NSString *title = [self getTitleForPage:page];
90 NSString *backButtonTitle = [page backButtonTitle];
91 if (backButtonTitle == nil)
92 backButtonTitle = title;
94 UINavigationItem *navitem = [[[UINavigationItem alloc] initWithTitle:title] autorelease];
95 [navitem setBackButtonTitle:backButtonTitle];
96 [navbar_ pushNavigationItem:navitem];
98 BOOL animated = [pages_ count] == 0 ? NO : YES;
99 [transition_ transition:(animated ? 1 : 0) toView:page];
100 [page setPageActive:YES];
102 [pages_ addObject:page];
103 [self reloadButtonsForPage:page];
106 [navbar_ setAccessoryView:[page accessoryView] animate:animated removeOnPop:NO];
108 [navbar_ setAccessoryView:[page accessoryView] animate:animated goingBack:NO];
112 - (void) popPages:(unsigned)pages {
116 [[pages_ lastObject] setPageActive:NO];
119 for (unsigned i(0); i != pages; ++i)
120 [navbar_ popNavigationItem];
123 [self resetViewAnimated:YES toPage:[pages_ lastObject]];
126 - (void) setPrompt:(NSString *)prompt {
127 [navbar_ setPrompt:prompt];
130 - (void) resetViewAnimated:(BOOL)animated {
133 if ([pages_ count] > 1) {
134 [navbar_ disableAnimation];
135 while ([pages_ count] != (animated ? 2 : 1))
136 [navbar_ popNavigationItem];
137 [navbar_ enableAnimation];
139 [navbar_ popNavigationItem];
144 [self resetViewAnimated:animated toPage:[pages_ lastObject]];
147 - (void) resetViewAnimated:(BOOL)animated toPage:(RVPage *)page {
148 [page resetViewAnimated:animated];
149 [transition_ transition:(animated ? 2 : 0) toView:page];
150 [page setPageActive:YES];
151 [self reloadButtonsForPage:page];
153 [navbar_ setAccessoryView:[page accessoryView] animate:animated removeOnPop:NO];
155 [navbar_ setAccessoryView:[page accessoryView] animate:animated goingBack:YES];
159 - (void) setTitle:(NSString *)title forPage:(RVPage *)page {
160 if ([pages_ count] == 0 || page != [pages_ lastObject])
162 UINavigationItem *navitem = [navbar_ topItem];
163 [navitem setTitle:title];
166 - (void) setBackButtonTitle:(NSString *)title forPage:(RVPage *)page {
167 if ([pages_ count] == 0 || page != [pages_ lastObject])
169 UINavigationItem *navitem = [navbar_ topItem];
170 [navitem setBackButtonTitle:title];
173 - (void) reloadButtonsForPage:(RVPage *)page {
174 if ([pages_ count] == 0 || page != [pages_ lastObject])
176 NSString *leftButtonTitle([pages_ count] == 1 ? [page leftButtonTitle] : nil);
177 [navbar_ showButtonsWithLeftTitle:leftButtonTitle rightTitle:[page rightButtonTitle]];
180 - (NSString *) getTitleForPage:(RVPage *)page {
184 - (void) reloadData {
185 for (int i(0), e([pages_ count]); i != e; ++i) {
186 RVPage *page([pages_ objectAtIndex:(e - i - 1)]);
191 - (CGRect) pageBounds {
192 return [transition_ bounds];