Scott
Scott

Reputation:

self.title = @"My Title";

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

Upvotes: 0

Views: 328

Answers (2)

mmc
mmc

Reputation: 17404

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.

Upvotes: 0

Alasdair Allan
Alasdair Allan

Reputation: 2144

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.

Upvotes: 1

Related Questions