In my iOS application, I want to provide a settings-view. "presentModalViewController" works very well:
ViewSettings *controller = [[ViewSettings alloc] initWithNibName:@"ViewSettings" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller];
[self.navigationController presentModalViewController:navController animated:YES];
[controller release];
[navController release];
Unfortunately, I have to change my running code and create the ViewSettings including the UINavigationController in Interface Builder. Why? Long story, I'll explain it in the end of this thread...
I try to drag and drop an UINavigationController in my ViewSettings and create an IBOutlet to access it in my class. I give this controller to "presentModalViewController" but the application crashed...
What I'm doing wrong?
[EDIT]
Error Message: GDB: Program received signal: "SIGABRT".
The error happens in the last line of this code:
ViewSettings *viewSettings = [[ViewSettings alloc] initWithNibName:@"ViewSettings" bundle:nil];
UINavigationController *navController = viewSettings.navigationController;
UINavigationBar *navBar = navController.navigationBar;
OwnNavigationBar *ownNavBar = (OwnNavigationBar *)navBar;
[ownNavBar drawHeaderImage:YES];
[self.navigationController presentModalViewController:navController animated:YES];
Detailed Error: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target .'
[/EDIT]
[EDIT2]
Thanks for your help!
Yes, navigationController is nil...
I think I add the UINavigationController in a wrong way... I put it in this window, because it was not possible to put it directly in my view:
How do I add the UINavigationController correct?
[/EDIT2]
PS: Why do I have to use IB? (you can skip this...)
I need an background image in my UINavigationBar. My first try was:
UIImage *image = [UIImage imageNamed: @"header.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.navigationController.navigationBar addSubview:imageView];
[self.navigationController.navigationBar sendSubviewToBack:imageView];
But in some issues, the title oder a UIBarButton is not visible! I tried a lot, e.g. sets the "tag" of the view and sendSubviewToBack in each view, but no success. This is a very annoying bug!
My second try was to create a category and overwrite the drawRect-method:
@implementation UINavigationBar(MyNavigationBar)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: @"header.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end
But now, all of my UINavigationBars have an background image and I can't deactivate it. The problem is, that "ViewSettings" needs the background image, but the following pushed views do not.
Unfortunately, it isn't possible to set a property in a category or call [super drawRect:rect] to avoid painting the image.
My last try is to write an own UINavigationBar
@interface OwnNavigationBar : UINavigationBar {
BOOL _drawHeaderImage;
}
Now I can control the drawRect-method!! GREAT!!
- (void)drawRect:(CGRect)rect {
if (_drawHeaderImage) {
UIImage *image = [UIImage imageNamed: @"header.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
else {
[super drawRect: rect];
}
}
But I celebrate to early... :-( It isn't possible to set an own UINavigationBar in the UINavigationController!!! "navigationBar" in UINavigationController is a read-only property! AAAAHHHHHHHHHH!
I have one last chance: in Interface Builder it is possible to give an UINavigationController an own UINavigationBar!! YES! I GOT IT!! :-)
I configured it in my MainWindow_iPhone.xib and it works great! Now, I have to implement this for my ViewSettings, because this (modal) view needs a new UINavigationController.
PS: Maybe, someone can send this thread to Apple, this all are very annoying circumstances and bugs :-(