I have a UIViewController object which is being presented as a modal view, but I am unable to set the view title. I want the title to vary depending on the context. I have tried the following:
If I set a constant title in the NIB file entry for the view controller in Interface Builder then it displays the title, but I need to be able to set it programmatically.
I can set the title for a view using the first example above on a view controller that is pushed onto the navigation controller's stack and that displays correctly, but it won't do it when presenting the view as a modal view.
I have a UIViewController object which is being presented as a modal view, but I am unable to set the view title. I want the title to vary depending on the context. I have tried the following:
If I set a constant title in the NIB file entry for the view controller in Interface Builder then it displays the title, but I need to be able to set it programmatically.
I can set the title for a view using the first example above on a view controller that is pushed onto the navigation controller's stack and that displays correctly, but it won't do it when presenting the view as a modal view.
Are you using a navigation controller? Is the set title stuff you're doing in the navigation controller or the controller that is being pushed? It has to be in the latter.
Are you using a navigation controller? Is the set title stuff you're doing in the navigation controller or the controller that is being pushed? It has to be in the latter.
I'm using a nav controller for the main views, but using the 'presentModalViewController:animated:' method to display the view in question. The modal view has a navigation bar with 'cancel' and 'save' buttons. I'm trying to set the title in the modal view that is about at appear after the 'presentModalViewController:animated:' is called. The 'viewWillAppear' method is being called and the title value is being set in the view (or the navigationItem or the navigationController, whichever version I'm trying.) I can see it in the debugger. But, nothing gets displayed on the actual view. The view itself displays fine, with the buttons, etc., but no title.
So, after much experimentation I've come to the conclusion that there is no way to programmatically set the view title for a modal view with a navigation bar. At least, it doesn't work for me in the iPhone simulator. I've tried every permutation I can think of and nothing works. I've even tried different iPhone SDK versions and it doesn't work in any of them. I presume that this is a bug, or that there must be some really arcane way of setting the title.
Has no-one else run into this problem? I would love it if someone could prove me wrong on this one.
I tried to attach a very simple demo app that demonstrates the problem I'm having, but it exceeds the max. file size allowed.
So, I thought I had discovered the cause of my problem - I was using an old beta version of the 3.0 SDK by mistake. However, after finally getting everything back to the 2.2.1 SDK (and that's another story - see the 3.0 forum) I figured everything would now work correctly. Wrong! This problem still persists in 2.2.1.
Has nobody else run into this problem? I find it hard to believe that I'm the only one trying to programmatically set the title of a modal view.
works for me. the modal view controller slides in with a nav bar containing "hi mom" as the title. let me know if you want me to send you the full xcode project.
I finally figured out how to set the title in a modal view with navigation bar. It seems pretty kludgy and arcane to me, but it works:
1. Define an IBOutlet for a UINavigationItem in your view controller code.
2. In IB connect the title item from the navigation bar (not the bar itself, but the embedded title) to the outlet defined in #1.
3. In the viewDidAppear method set the title property of the UINavigationItem.
Note! It has to be done in the viewDidAppear method, not the viewWillAppear method, as the navigation bar is obviously not initialized until after the viewWillAppear method has been called. If you put it in the viewWillAppear method then it will show on the second and subsequent appearance of the view, but not on the first.
A similar procedure is necessary to programmatically set the UIBarButtonItem objects in the navigation bar, except that the outlet needs to reference the entire navigation bar, not the bar's title.
This seems like a bug in the UI SDK code. It does not work by just setting the title of the navigationItem property of the view, even if you set it in the viewDidAppear method.
works for me. the modal view controller slides in with a nav bar containing "hi mom" as the title. let me know if you want me to send you the full xcode project.
Thanks jsd. That probably works, but it doesn't solve my problem as it will set the title when the view is loaded. I want to set the title when the view appears as I use the same view for adding new items and for editing existing ones. The solution described above works for me though, kludgy though it may be.
Actually setting the outlet to reference the entire navigation bar, and then setting its title in the viewDidAppear method also works, and that then lets you use the same outlet to set the button items as well as the title.
Thanks jsd. That probably works, but it doesn't solve my problem as it will set the title when the view is loaded. I want to set the title when the view appears as I use the same view for adding new items and for editing existing ones. The solution described above works for me though, kludgy though it may be.
you could define a custom titleview in the initWithNibName and just store a reference to it in your class - then access the label in the view later on and do whatever you want with it.
you could define a custom titleview in the initWithNibName and just store a reference to it in your class - then access the label in the view later on and do whatever you want with it.
Well, that is essentially what I am doing except I get IB to establish the reference to the nav bar rather than overloading the initWithNibName method and I don't need a custom titleView. Your solution may get around the problem of having to set the title after the view appears though. Thanks.
I had a similar problem earlier today. In the viewDidLoad method of my modal view's controller, I was trying to set the title of the navigation bar as well as add left and right buttons to the navigation bar. I was accessing the navigation bar using "self.navigationItem".
And as in your app, I was not getting the desired effect.
But I went back to IB and by right clicking on the File Owner (set in IB to my modal view's controller), I noticed that its "navigationItem" property was not connected to my modal view's navigation bar. Once I established this connection, everything was fine.
I have a UIViewController object which is being presented as a modal view, but I am unable to set the view title. I want the title to vary depending on the context. I have tried the following:
If I set a constant title in the NIB file entry for the view controller in Interface Builder then it displays the title, but I need to be able to set it programmatically.
I can set the title for a view using the first example above on a view controller that is pushed onto the navigation controller's stack and that displays correctly, but it won't do it when presenting the view as a modal view.
Can anyone tell me what I need to do, please.
---
The discussion on this thread shows me there are other, simpler
approaches to the problem of setTitle for a NavBar. Still I send on
these thoughts.
I was unable to dynamically change the title of a ModalView.
It would take a bit of work to rework the NIBs, and switch to pushing a Modeless one.
(Plus I believe you should/can not push a modeless one from a modal one)
I made a copy of the NIB file for the ModalView I had, AddPhotoView.XIB
and changed only the Title in the navigationItem, giving two
versions of a view (AddPhotoView.XIB and EditPhotoView.XIB),
both of which using the same controller Class.
This allowed me to dynamically decide which Title to display
, using virtually identical NIBs.
There are to instance variables for the same Class: