Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

modal UIViewController title not showing

emansfieldemansfield Posts: 21Registered Users
edited February 2010 in iOS SDK Development
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.
Post edited by emansfield on

Replies

  • jsdjsd Posts: 900Registered Users
    edited May 2009
    emansfield wrote: »
    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.

    try
    self.navigationItem.title=@"My Title";
    
  • emansfieldemansfield Posts: 21Registered Users
    edited May 2009
    jsd wrote: »
    try
    self.navigationItem.title=@"My Title";
    

    Nope, I tried that one too and nothing shows.
  • jsdjsd Posts: 900Registered Users
    edited May 2009
    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.
  • emansfieldemansfield Posts: 21Registered Users
    edited May 2009
    jsd wrote: »
    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.
  • emansfieldemansfield Posts: 21Registered Users
    edited May 2009
    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.
  • emansfieldemansfield Posts: 21Registered Users
    edited May 2009
    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.:confused:
  • jsdjsd Posts: 900Registered Users
    edited May 2009
    I got it to work. In the first view controller, do this to launch the modal nav:
    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:
    - (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.
  • emansfieldemansfield Posts: 21Registered Users
    edited May 2009
    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. :)
  • emansfieldemansfield Posts: 21Registered Users
    edited May 2009
    jsd wrote: »
    I got it to work. In the first view controller, do this to launch the modal nav:
    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:
    - (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.
  • emansfieldemansfield Posts: 21Registered Users
    edited May 2009
    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.
  • jsdjsd Posts: 900Registered Users
    edited May 2009
    emansfield wrote: »
    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.
  • emansfieldemansfield Posts: 21Registered Users
    edited May 2009
    jsd wrote: »
    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.
  • siamaksiamak Posts: 39Registered Users
    edited May 2009
    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.
  • mebarronmebarron Posts: 47Registered Users
    edited February 2010
    emansfield wrote: »
    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.

    ---
    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;
    }
Sign In or Register to comment.