TestNavBar.app : Select all

/* TestNavBar.h */ #import <CoreFoundation/CoreFoundation.h> #import <UIKit/UIKit.h> #import <UIKit/UINavigationBar.h> #import <UIKit/UINavigationItem.h> @interface MainView : UIView { UINavigationBar *navBar; /* Our navigation bar */ UINavigationItem *navItem; /* Navigation bar title */ BOOL isMuted; /* Is mute turned on? */ } - (id)initWithFrame:(CGRect)frame; - (void)dealloc; - (UINavigationBar *)createNavBar:(CGRect)rect; - (void)setNavBar; - (void)navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button; @end @interface TestNavBar : UIApplication { UIWindow *window; MainView *mainView; } - (void)applicationDidFinishLaunching:(NSNotification *)aNotification; @end /* TestNavBar.m */ #import "TestNavBar.h" @implementation TestNavBar - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { window = [ [ UIWindow alloc ] initWithContentRect: [ UIHardware fullScreenApplicationContentRect ] ]; CGRect rect = [ UIHardware fullScreenApplicationContentRect ]; rect.origin.x = rect.origin.y = 0.0f; mainView = [ [ MainView alloc ] initWithFrame: rect ]; [ window setContentView: mainView ]; [ window orderFront: self ]; [ window makeKey: self ]; [ window _setHidden: NO ]; } @end @implementation MainView - (id)initWithFrame:(CGRect)rect { if ((self == [ super initWithFrame: rect ]) != nil) { isMuted = NO; navBar = [ self createNavBar: rect ]; /* Update the navBar for the first time */ [ self setNavBar ]; [ self addSubview: navBar ]; } return self; } - (void)dealloc { [ navBar release ]; [ navItem release ]; [ self dealloc ]; [ super dealloc ]; } - (UINavigationBar *)createNavBar:(CGRect)rect { UINavigationBar *newNav = [ [UINavigationBar alloc] initWithFrame: CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, 48.0) ]; [ newNav setDelegate: self ]; [ newNav enableAnimation ]; /* Add our title */ navItem = [ [UINavigationItem alloc] initWithTitle:@"My Example" ]; [ newNav pushNavigationItem: navItem ]; return newNav; } - (void)setNavBar { if (isMuted == YES) { [ navItem setTitle: @"Spouse (Muted)" ]; [ navBar showLeftButton:nil withStyle: 0 rightButton:@"Mute" withStyle: 1 ]; } else { [ navItem setTitle: @"Spouse" ]; [ navBar showLeftButton:nil withStyle: 0 rightButton:@"Mute" withStyle: 0 ]; } } - (void)navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button { if (button == 0) /* Right button */ { if (isMuted == YES) /* Toggle isMuted */ isMuted = NO; else isMuted = YES; [ self setNavBar ]; /* Update navbar buttons */ } } @end