开发者

iPhone: NavigationController NavigationBar Back button text

开发者 https://www.devze.com 2022-12-16 18:26 出处:网络
When I 开发者_JAVA技巧push a new ViewController onto a navigation controller stack the \"back\" button is the Title of the previous controller.

When I 开发者_JAVA技巧push a new ViewController onto a navigation controller stack the "back" button is the Title of the previous controller.

How can I change the text in the back button to "Back" instead of the default name-of-last-controller?


This is the proper way to make a back button with something other than the previous' pages title

UIBarButtonItem *barButton = [[UIBarButtonItem alloc] init];
barButton.title = @"Your Title";

self.navigationItem.backBarButtonItem = barButton; 

Its to my understanding that:

self.navigationItem.backBarButtonItem.title = @"Your Title";

will make the Title of the navigation bar on the previous page this which is not what you want.


I know, the question is very old, but I found a nice solution.

UIBarButtonItem *barButton = [[UIBarButtonItem alloc] init];
barButton.title = @"back";
self.navigationController.navigationBar.topItem.backBarButtonItem = barButton;

Works from childView! Tested with iOS 7.


You need to create a custom button on the navigation controller. Put the following code in the viewDidLoad in your Root View Controller:

UIBarButtonItem * tempButtonItem = [[[ UIBarButtonItem alloc] init] autorelease];
tempButtonItem .title = @"Back";

self.navigationItem.backBarButtonItem = tempButtonItem ;

By setting the navigation bar button on the Root View Controller, the pushed view shows the appropriate back button.


You can change the title of the current view controller of the navigation controller before push the new view controller:

self.title = @"Custom Title";

[self pushViewController: newViewController ...];

and in the navigation controller's delegate class

-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {

if([viewController class] == [OldViewController class]) {

viewController.title = @"Your previous title";

}

}


You can actually set the title on the main view controller's navigationItem's title. Basically each UIViewController has a little stub UINavigationItem which contains metadata about how that view should be referenced inside a UINavigationController. By default, that metadata just falls back to the UIViewController itself.

Assuming 'self' is the UIViewController of the view that's visible inside the UINavigationController, set:

self.navigationItem.title = @"My Custom Title"


You can achieve this by setting the Back button title in the originating controller's "viewWillDisappear" function as follows:

override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        //Set Title for this view
        self.navigationItem.title = "My Title"

}

override func viewWillDisappear(animated: Bool) {
        super.viewWillAppear(animated)
        //Set Title for back button in next view 
        self.navigationItem.title = "Back"
}
0

精彩评论

暂无评论...
验证码 换一张
取 消