user2498776
user2498776

Reputation: 35

set backbutton's name and title of View Controller

I’m new in iOS and as a part of learning I'm trying to modify the code from Bart Jacobs tutorial .

I've got the hierachy of 3 viewcontrollers. The first one contains the names of anime (UITableViewController), the second - names of persons from selected anime (UITableViewController) and the the third - the scrollable picture of selected person (UIViewController .

The title of the 1st controller is set with:

self.title = @"Anime"

When I select the name of anime in 1st view controller it pushes the 2nd. Back button in the 2nd view controller has the name "Anime" (that’s what I need), but has no title of 2nd view controller.

When I choose the person in 2nd view controller it pushes the 3rd and here is the new problem: back button in the 2nd view controller has the name "Back" and there is no title of 3rd view controller.

What I want is: 1) the back botton in current view controller (2nd and 3rd) has the same name as the title of previous view controller 2) the title of current view controller has the name of the selected row from the previous view controller.

Any help much appreciated

Thanks

Upvotes: 0

Views: 1062

Answers (2)

HRM
HRM

Reputation: 2127

You are doing self.title = @"Anime" in your first view controller. So when pushing next view controller from this, the back button of next controller will be given 'Anime' by default. But you missed out setting title for next view controllers, thats why you are getting a blank title and hence the next view controller's back button will have "Back" only.

So, what you need is to set the title before pushing the next view controller, then the title and back button will display as you expected. This is how you need to set it before pushing:

In your tableView:didSelectRowAtIndexPath

ViewController *viewController2 = [[[ViewController alloc] init] autorelease];
viewController2.title = @"Selected anime name"; //Here you need to take the selected row's name
[self.navigationController pushViewController:viewController2  animated:YES];

Hope it helps.

Upvotes: 1

Lord Zsolt
Lord Zsolt

Reputation: 6557

In tableView:DidSelectRowAtIndexPath: before pushing your next view to the navigation controller do the following:

 nextViewController.title = @"Name of the Anime";

I assume the names are in an NSArray, so you can set the title based on indexPath.row:

NSString *title = [animeNames objectAtIndex:indexPath.row];
nextViewController.title = title;

Upvotes: 0

Related Questions