简体   繁体   English

使导航栏的左栏按钮标题与上一个视图的标题相同

[英]Make left bar button's title of navigation item the same as previous view's title

In my 1st VC(postVC) , I have below code to set navigation bar title varied to username: 在我的第一个VC(postVC)中 ,我有以下代码来设置导航栏标题,该标题随用户名而异:

// Set nav bar items.
[self.navigationController setNavigationBarHidden:NO animated:NO];
NSString *title = [NSString stringWithFormat:@"@%@'s Posts",[userPassed username]];
self.navigationItem.title = title;

However, there is a little problem here that when I call 2nd VC(detailVC) by this method (push VC), the left bar button item's title in the 2nd VC is shown "back" but I expect its title to follow the title in previous VC( 1st VC ). 但是,有一点问题就在这里,当我把第二个VC(detailVC)通过这种方法(推VC),左边栏按钮项目在第二届VC名字为“回”,但我希望它的标题遵循称号先前的VC( 1st VC )。

- (void) didSelectItemFromCollectionView:(NSNotification *)notification
{
    NSDictionary *cellData = [notification object];
    if (cellData)
    {
        if (!self.detailViewController)
        {
            self.detailViewController = [[PAWUserPostsDetailViewController alloc] initWithNibName:@"PAWUserPostsDetailViewController" bundle:nil];
        }
    self.detailViewController.detailItem = cellData;
    [self.navigationController pushViewController:self.detailViewController animated:YES];
    }
}

If I change 如果我改变

 self.navigationItem.title = @"test";

it works as expected. 它按预期工作。 In 2nd VC , left bar button item's title is shown "test" as I want. 第二个VC中 ,根据需要,左栏按钮项的标题显示为“测试”。 So, to be clear, my question is how to make left bar button item's title follow the title of previous VC which is varied by username. 因此,要明确地说,我的问题是如何使左栏按钮项的标题跟上前VC的标题,该标题因用户名而异。

This is a workout, although probably not the most efficient: 这是一种锻炼,虽然可能不是最有效的:

Create a property in your header file of PAWUserPostsDetailViewController 在PAWUserPostsDetailViewController的头文件中创建一个属性

@property (strong, nonatomic) NSString *backButtonTitle; 

Then go back into your view controller where you select the row and put this in didSelectItem method: 然后回到您的视图控制器,在其中选择行并将其放入didSelectItem方法中:

NSString *backTitle = [NSString stringWithFormat:@"@%@'s Posts",[userPassed username]];

PAWUserPostsDetailViewController *detailVC = [[PAWUserPostsDetailViewController alloc] initWithNibName:@"PAWUserPostsDetailViewController" bundle:nil];;
detailVC.backButtonTitle = backTitle;

Then go back into your PAWUserPostsDetailViewController.m and set it as you normally would: 然后回到您的PAWUserPostsDetailViewController.m并按照通常的方式进行设置:

self.navigationItem.title = backButtonTitle;

In postVC.h file 在postVC.h文件中

@property (strong, nonatomic)NSString*title;

In postVC.m file 在postVC.m文件中

title = [NSString stringWithFormat:@"@%@'s Posts",[userPassed username]];
self.navigationItem.title = title;
self.detailViewController.newtitle=self.title; 
[self.navigationController pushViewController:self.detailViewController animated:YES];

In detailVC.h file 详细的VC.h文件

  @property (strong, nonatomic) NSString*newtitle;

In detailVC.m file 详细的VC.m文件

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:newtitle style:UIBarButtonItemStylePlain target:self action:@selector(back:)];

First try this: 首先尝试:

NSString *title = [NSString stringWithFormat:@"@%@'s Posts",[userPassed username]]; 

// If it`s to long try this:
    if ([title length] > 7)
{
    title = [title substringWithRange:NSMakeRange(0, 6)];
}
self.title = title;

There isn't any reason to the first option doesn't work, but here a another option: 没有任何理由使第一个选项不起作用,但是这里有另一个选项:

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] init];

    if ([title length] > 7)
{
    title = [title substringWithRange:NSMakeRange(0, 6)];
}
backButton.title = title;
self.navigationItem.backBarButtonItem = backButton;

Becarfuly if title is to long, the name of the button will be back, 如果标题太长,则按钮名称将返回,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM