Advertise Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

sdkIQ for iPhone
($4.99)

Your First iPhone App
($1.99)

iPhone Code Generator
($9.99)

Dual Matches
($0.99)

Calcuccino Programmers' Calculator
($2.99)

SDKtoday
(free)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 05-19-2009, 12:34 PM   #1 (permalink)
New Member
 
Join Date: May 2009
Location: Fredericton, New Brunswick, Canada
Posts: 21
Default modal UIViewController title not showing

Hi,

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:

- (void) viewWillAppear:(BOOL)animated {
self.title = (category ? category.name : @"Add New Category");
}

I have also tried:

- (void) viewWillAppear:(BOOL)animated {
self.navigationController.title = (category ? category.name : @"Add New Category");
}

neither of the above display any title.

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.
emansfield is offline   Reply With Quote
Old 05-19-2009, 12:45 PM   #2 (permalink)
jsd
at this moment
 
Join Date: Mar 2009
Location: San Francisco, CA
Posts: 871
Default

Quote:
Originally Posted by emansfield View Post
Hi,

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:

- (void) viewWillAppearBOOL)animated {
self.title = (category ? category.name : @"Add New Category");
}

I have also tried:

- (void) viewWillAppearBOOL)animated {
self.navigationController.title = (category ? category.name : @"Add New Category");
}

neither of the above display any title.

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.
try
Code:
self.navigationItem.title=@"My Title";
jsd is offline   Reply With Quote
Old 05-19-2009, 12:50 PM   #3 (permalink)
New Member
 
Join Date: May 2009
Location: Fredericton, New Brunswick, Canada
Posts: 21
Default

Quote:
Originally Posted by jsd View Post
try
Code:
self.navigationItem.title=@"My Title";
Nope, I tried that one too and nothing shows.
emansfield is offline   Reply With Quote
Old 05-19-2009, 12:52 PM   #4 (permalink)
jsd
at this moment
 
Join Date: Mar 2009
Location: San Francisco, CA
Posts: 871
Default

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.
jsd is offline   Reply With Quote
Old 05-19-2009, 01:37 PM   #5 (permalink)
New Member
 
Join Date: May 2009
Location: Fredericton, New Brunswick, Canada
Posts: 21
Default

Quote:
Originally Posted by jsd View Post
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.
emansfield is offline   Reply With Quote
Old 05-21-2009, 08:42 AM   #6 (permalink)
New Member
 
Join Date: May 2009
Location: Fredericton, New Brunswick, Canada
Posts: 21
Default

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.
emansfield is offline   Reply With Quote
Old 05-22-2009, 08:03 AM   #7 (permalink)
New Member
 
Join Date: May 2009
Location: Fredericton, New Brunswick, Canada
Posts: 21
Default

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.
emansfield is offline   Reply With Quote
Old 05-22-2009, 11:37 AM   #8 (permalink)
jsd
at this moment
 
Join Date: Mar 2009
Location: San Francisco, CA
Posts: 871
Default

I got it to work. In the first view controller, do this to launch the modal nav:

Code:
ModalViewController *modal = [[ModalViewController alloc] init];	
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:modal];
[self presentModalViewController:nav animated:YES];
[modal release];
then, in the one that is loaded, do this:

Code:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
		self.title=@"hi mom";
        // Custom initialization
    }
    return self;
}
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.
jsd is offline   Reply With Quote
Old 05-22-2009, 11:42 AM   #9 (permalink)
New Member
 
Join Date: May 2009
Location: Fredericton, New Brunswick, Canada
Posts: 21
Default Success - finally!

Update!

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.
emansfield is offline   Reply With Quote
Old 05-22-2009, 11:46 AM   #10 (permalink)
New Member
 
Join Date: May 2009
Location: Fredericton, New Brunswick, Canada
Posts: 21
Default

Quote:
Originally Posted by jsd View Post
I got it to work. In the first view controller, do this to launch the modal nav:

Code:
ModalViewController *modal = [[ModalViewController alloc] init];	
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:modal];
[self presentModalViewController:nav animated:YES];
[modal release];
then, in the one that is loaded, do this:

Code:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
		self.title=@"hi mom";
        // Custom initialization
    }
    return self;
}
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.
emansfield is offline   Reply With Quote
Old 05-22-2009, 11:54 AM   #11 (permalink)
New Member
 
Join Date: May 2009
Location: Fredericton, New Brunswick, Canada
Posts: 21
Default

Another update...

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.
emansfield is offline   Reply With Quote
Old 05-22-2009, 12:00 PM   #12 (permalink)
jsd
at this moment
 
Join Date: Mar 2009
Location: San Francisco, CA
Posts: 871
Default

Quote:
Originally Posted by emansfield View Post
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.
jsd is offline   Reply With Quote
Old 05-22-2009, 12:21 PM   #13 (permalink)
New Member
 
Join Date: May 2009
Location: Fredericton, New Brunswick, Canada
Posts: 21
Default

Quote:
Originally Posted by jsd View Post
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.
emansfield is offline   Reply With Quote
Old 05-29-2009, 05:09 PM   #14 (permalink)
Member
 
Join Date: Apr 2009
Location: London, UK
Posts: 39
Default

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.
siamak is offline   Reply With Quote
Old 02-22-2010, 05:37 PM   #15 (permalink)
Registered Member
 
mebarron's Avatar
 
Join Date: Apr 2009
Location: Michigan
Posts: 41
Default

Quote:
Originally Posted by emansfield View Post
Hi,

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:

- (void) viewWillAppearBOOL)animated {
self.title = (category ? category.name : @"Add New Category");
}

I have also tried:

- (void) viewWillAppearBOOL)animated {
self.navigationController.title = (category ? category.name : @"Add New Category");
}

neither of the above display any title.

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:

AddPhotoViewController* addPhotoViewController; // Title: ADD PHOTO
AddPhotoViewController* editPhotoViewController; // Title: EDIT PHOTO

This seems "heavy handed" to me, but works.

- (AddPhotoViewController *)addPhotoViewController{
// only Title varies between ePVC and aPVC
if(addPhotoViewController == nil){
addPhotoViewController = [[AddPhotoViewController alloc] initWithNibName:@"AddPhotoView" bundle:nil];
}
return addPhotoViewController;
}

- (AddPhotoViewController *)editPhotoViewController{
// only Title varies between ePVC and aPVC
if(editPhotoViewController == nil){
editPhotoViewController = [[AddPhotoViewController alloc] initWithNibName:@"EditPhotoView" bundle:nil];
}
return editPhotoViewController;
}
mebarron is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


» Advertisements
» Stats
Members: 41,860
Threads: 49,768
Posts: 213,054
Top Poster: BrianSlick (3,138)
Welcome to our newest member, gustavo7sexton
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 06:58 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0