-1

So, I have one ViewController that has a TableView populate with an Array (_vinhoArray) and another TableViewController that opens when a user taps a row in this first ViewController.

What I want is set the title of second View ( TableViewController) for the name of row selected.

In this first one I have this code.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

    {
        NSString *selectedRow = [_vinhoArray objectAtIndex:indexPath.row];
        // SubTable is the name of Second View
        SubTable *subTable = [[SubTable alloc] init];
        subTable.titleView = selectedRow;
    }

In the second view a have a property set

@property (nonatomic, strong) NSString *titleView;

In the -(void)viewDidLoad I have this code

self.title = titleView;

But nothing shows in the title of the Second View (TableViewController)

Thats it! Please Help

Nayan
  • 3,014
  • 2
  • 17
  • 33

4 Answers4

0

What is probably happening is that the view is loading before you set the titleView property. As you are setting the title statically in viewDidLoad, no changes are being shown. You can confirm this by putting in a couple of breakpoints and seeing the order of the calls being made.

One solution to this is to use a more dynamic method of setting the table. Create a custom accessor in your second view controller:

- (void)setTitleView:(NSString *)title {
    _titleView = title; // This sets the backing iVar
    self.title = titleView;
}

Now, whenever you set the property, then the change is applied, even if it is after the view has loaded.

You don't need to declare the method or the iVar, these are set up by autosynthesis; you are merely overriding the default implementation of the property setter.

Abizern
  • 146,289
  • 39
  • 203
  • 257
  • Hi man I try here and seems that i get the value for the `_titleView` because i put a `NSLog(@"%@", _titleView);`and works fine. But a still don't get the title for the Navigation Controller. I create a @property in my second view controller `@property (nonatomic, strong) NSString *titleView;` Dont synthesize and put the code you told me. (By the way, in the third line self.title = titleView; is not self.title = _titleView; ? If a put just titleView, without the underscore.. i get an error. – Diego Rodrigues Jan 17 '14 at 16:36
0

Are you certain that setting .title property is what you want? If you're in a UINavigationController you may actually want

self.navigationItem.title 

instead. Setting the title is independent of source of the title, so I'd work on getting that second View Controller to display a static title you choose, and only then turn to the task of passing a title dynamically from the first VC. Your issue is likely in setting the title string, not in how you pass that string around from VC 1 to VC 2.

As obvious as it seems, the .title property of a UIViewController doesn't always actually cause a title string to be displayed… the rules for when that property is actually used get tricky.

Bill Patterson
  • 2,495
  • 1
  • 19
  • 20
  • Calling `self.title = @"Some Title";` where `self` is the view controller is fine. It's the same result as calling `self.navigationItem.title = @"Some Title";` – rmaddy Jan 17 '14 at 03:38
  • Ah, ok. I knew that NavigationItems by default pull their value from the ViewController's .title property, but I wasn't aware that the relationship was "active" in the sense that changes to title would automatically cause updates to NavItem display after initial read. Thank you. – Bill Patterson Jan 17 '14 at 05:21
0

I am assuming that "titleView" is nothing but of type NSString.

In your SecondViewController.h, synthesize your titleView property.

And NSLog it in your SecondViewController before assigning it to self.title just to check that it's getting some value we tried to assign.

Hope this helps!

Nayan
  • 3,014
  • 2
  • 17
  • 33
  • Hi man, i try here and seems that you right. If a put the `NSLog` BEFORE the `self.title` i get a `null value` but i put AFTER, i get the right value. How i can fix that? – Diego Rodrigues Jan 17 '14 at 16:45
  • u mean u r getting null when u NSLog of self.title before assigning it, then it's right! How self.title will get any value before assigning it? – Nayan Jan 20 '14 at 04:57
  • Yeah, but how i can assign this value before, so i can use it? – Diego Rodrigues Jan 20 '14 at 14:33
0

If you simply want to set the title on the navigation bar of the view controller you are presenting, there's no need to create a property for it. You probably want to do something like this:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

    {
        NSString *titleString = [_vinhoArray objectAtIndex:indexPath.row];

        // SubTable is the name of Second View
        SubTable *subTable = [[SubTable alloc] init];
        subTable.title = titleString;
        [self.navigationController pushViewController:subTable animated:YES];
    } 

Above seems to be the easiest and most straight-forward way to implement this. On a side note, it's pretty unconventional to name a variable type like an NSString with some other class type, like titleView or titleLabel. Something similar to "titleString", like I used, would be more appropriate and less confusing for anyone reading your code.

Mike
  • 9,765
  • 5
  • 34
  • 59
  • Hello Mike, your code works with a change that i made here. MY first VC was connect by a `segue`to my second VC. If i put your code the title is fine but my navigation bar is corrupted. So I delete that `segue`and your code works fine :) But now i'm getting i problem with my second VC. I get an error when the TableView populates. Terminating app due to uncaught exception `'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier TableCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard` – Diego Rodrigues Jan 17 '14 at 17:16
  • Whatever you're using in cellForRowAtIndexPath for a cell identifier is not properly set up. Are you using a nib file for the tableViewCells? – Mike Jan 17 '14 at 19:26
  • No, the TableViewCell is in the Storyboard file in the TableViewController. But before I don't get any errors, the two view controllers are connected by a segue. – Diego Rodrigues Jan 17 '14 at 23:26
  • http://stackoverflow.com/questions/12737860/assertion-failure-in-dequeuereusablecellwithidentifierforindexpath – Mike Jan 18 '14 at 01:48