views:

116

answers:

2

Can this be set more dynamically instead of hard coded?

self.title = @"My Title";
//           ^^^^^^^^^^^

I want the title to be the name of the row the user clicks on instead of Hard Coded Title name

In my viewController, I have State Names. I want the title to be the state name the user clicks on. Any suggestions?

Thanks in advance

A: 

Your question isn't that clear. In general you can set the title of the current view to be any NSString object, so

self.title = string;

However what I'm guessing your asking is whether you can set the tile of a view which is created and brought into view when you click on a UITableViewCell. If so the answer is yes you can. You can do this in the tableView:didSelectRowAtIndexPath: method of your view controller when you create the new view.

You need to pass either the indexPath variable or the text label from the cell cell.textLabel.text into a custom init method. So if you had a view controller called nextController for instance you'd do something like,

NextController *next = [[NextController alloc] initWithIndexPath: indexPath];
[appDelegate.navController pushViewController:next animated: YES];
[next release];

inside the tableView:didSelectRowAtIndexPath: method of your main controller. Then in your NextController class you'd override the init method along the these lines,

-(id)initWithIndexPath: (NSIndexPath *)indexPath {
    if ( self == [super init] ) {
        indexVariable = indexPath;
    }
    return self;
}

after declaring the indexVariable in you @interface. The all you need to do is set the title of your new view form the viewDidLoad: method of your new controller, either grabbing the cell's label using the NSIndexPath or directly if you passed the label instead.

Alasdair Allan
A: 

Alasdair's answer is correct, but may be more complex than what you need.

In your tableView:didSelectRowAtIndexPath: method, you probably have something that looks like (as in Alasdair's example):

ViewController *viewController = [[ViewController alloc] init]; //some flavor of "init"
[self.navController pushViewController:viewController animated: YES];
[viewController release];

All you really need to do is add:

[viewController setTitle:<<the string you want to set it to here>>];

after the first line above. Alternatively, you could pass the string in through a custom init, and set it from inside your pushed view controller. Either way.

But it's not really necessary to expose the entire table view to the underlying view controller.

mmc
Yup, that'd also work and is a lot simpler if setting the title is all you really need to do. I generally pass the NSIndexPath through so I can retrieve the original cell if I need it in the new view controller.
Alasdair Allan