I have a UINavigationController which allows you to go from 1 screen to the other. There are only 2 screens at the moment.
On the second screen, there is a toolbar with 2 buttons put on the right hand side.
The problem I'm having is when I go back to the previous screen one of two things happens.
1. The app does not go back to the previous screen and it will simply dump to the iphone home screen (quits the app altogether).
2. I know that it is a memory issue and have pinpointed it to UIBarButton allocation. Now, if I remove the release the screen movement is fine; but it causes a memory leak.
I've tried adding autorelease to my UIBarButton call but this causes the app to dump out.
I've tried creating a temp pointer to the UIBarButton then releasing once it has been used; but this causes the same problem.
If I remove all references to releasing UIBarButton all navigation is fine, but it causes a memory leak problem.
I seem to be going around in circles.
Code:
// This is on SecondViewController;
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
// Toolbar
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 100.0, 44.01)];
// Create array for buttons
NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:2];
// Create buttons
UIBarButtonItem *btnAdd = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self action:@selector(btnAdd:)];
btnAdd.style = UIBarButtonItemStyleBordered;
UIBarButtonItem *btnEdit = self.editButtonItem;
// Add to buttons array
[buttons addObject:btnAdd];
[buttons addObject:btnEdit];
// Set the buttons to toolbar
[toolbar setItems:buttons];
UIBarButtonItem *tmp = [[[UIBarButtonItem alloc] initWithCustomView:toolbar] autorelease];
// Add toolbar to the right bar
self.navigationItem.rightBarButtonItem = tmp;
// Memory cleanup
[buttons release];
[btnAdd release];
[btnEdit release];
[toolbar release];
//[tmp release];
}
Has anyone come across this problem before, and if so; how did you resolve it?
Thanks