Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jmerkow

macrumors newbie
Original poster
Jan 20, 2011
17
0
I am trying to add a 'reset' button to a nav-bar added by the UINavigationController. The button is connected to a function that updates the view. For some reason, it is not updating the view. It works fine when it is connected to a different button (not on the nav-bar).

AppDelegate:

Code:
- (BOOL)application:(UIApplication *)application 
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

	gameViewController = [[GameViewController alloc] init];
	
	UINavigationController *navController = [[UINavigationController alloc]
 initWithRootViewController:gameViewController];
	
	[window setRootViewController:navController];
	[navController release];
	
	[window makeKeyAndVisible];
    
    return YES;
}

In the View:
Code:
- (void)viewDidLoad {
    
	NSLog(@"ViewDidLoad");
	
	UIBarButtonItem *resetGameButtonItem = [[[UIBarButtonItem alloc]
initWithTitle:@"Reset Game"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(resetGameButtonPressed:)] autorelease];

self.navigationItem.leftBarButtonItem = resetGameButtonItem;
Button Function:
Code:
- (IBAction)resetGameButtonPressed:(id) sender{
	[gs resetGame];
	[self updateViewWithGameState];
}

Nested Function:
Code:
- (void)updateViewWithGameState{
	progressView.progress = 1.0/[gs maxScore]*[gs currentScore];
	scoreText.text = [NSString stringWithFormat:@"%d",
[gs currentScore]];

	[rollButton setImage:[UIImage 
imageNamed:[diceImageNames objectAtIndex:([gs lastRoll]-1)]]
forState:UIControlStateNormal];

	[tableView reloadData];	
}


EDIT:

I found the solution by playing around. To those interested, I took the navbar item addtions out of viewdidload and put them into init
Code:
- (id) init;{
	[super init];
	
	UIBarButtonItem *resetGameButtonItem = [[[UIBarButtonItem alloc] 
											 initWithTitle:@"Reset Game"
											 style:UIBarButtonItemStyleBordered
											 target:self
											 action:@selector(resetGameButtonPressed:)] autorelease];
	
	self.navigationItem.leftBarButtonItem = resetGameButtonItem;
	return self;
}
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.