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:0];
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];
105 if ([navbar_ respondsToSelector:@selector(setAccessoryView:animate:goingBack:)])
106 [navbar_ setAccessoryView:[page accessoryView] animate:animated goingBack:NO];
108 [navbar_ setAccessoryView:[page accessoryView] animate:animated removeOnPop:NO];
111 - (void) popPages:(unsigned)pages {
115 [[pages_ lastObject] setPageActive:NO];
118 for (unsigned i(0); i != pages; ++i)
119 [navbar_ popNavigationItem];
122 [self resetViewAnimated:YES toPage:[pages_ lastObject]];
125 - (void) setPrompt:(NSString *)prompt {
126 [navbar_ setPrompt:prompt];
129 - (void) resetViewAnimated:(BOOL)animated {
132 if ([pages_ count] > 1) {
133 [navbar_ disableAnimation];
134 while ([pages_ count] != (animated ? 2 : 1))
135 [navbar_ popNavigationItem];
136 [navbar_ enableAnimation];
138 [navbar_ popNavigationItem];
143 [self resetViewAnimated:animated toPage:[pages_ lastObject]];
146 - (void) resetViewAnimated:(BOOL)animated toPage:(RVPage *)page {
147 [page resetViewAnimated:animated];
148 [transition_ transition:(animated ? 2 : 0) toView:page];
149 [page setPageActive:YES];
150 [self reloadButtonsForPage:page];
152 if ([navbar_ respondsToSelector:@selector(setAccessoryView:animate:goingBack:)])
153 [navbar_ setAccessoryView:[page accessoryView] animate:animated goingBack:YES];
155 [navbar_ setAccessoryView:[page accessoryView] animate:animated removeOnPop:NO];
158 - (void) setTitle:(NSString *)title forPage:(RVPage *)page {
159 if ([pages_ count] == 0 || page != [pages_ lastObject])
161 UINavigationItem *navitem = [navbar_ topItem];
162 [navitem setTitle:title];
165 - (void) setBackButtonTitle:(NSString *)title forPage:(RVPage *)page {
166 if ([pages_ count] == 0 || page != [pages_ lastObject])
168 UINavigationItem *navitem = [navbar_ topItem];
169 [navitem setBackButtonTitle:title];
172 - (void) reloadButtonsForPage:(RVPage *)page {
173 if ([pages_ count] == 0 || page != [pages_ lastObject])
175 NSString *leftButtonTitle([pages_ count] == 1 ? [page leftButtonTitle] : nil);
176 [navbar_ showButtonsWithLeftTitle:leftButtonTitle rightTitle:[page rightButtonTitle]];
179 - (NSString *) getTitleForPage:(RVPage *)page {
183 - (void) reloadData {
184 for (int i(0), e([pages_ count]); i != e; ++i) {
185 RVPage *page([pages_ objectAtIndex:(e - i - 1)]);
190 - (CGRect) pageBounds {
191 return [transition_ bounds];