【发布时间】:2014-07-01 08:57:32
【问题描述】:
我的导航栏标题有问题。我有 2 个屏幕,当我单击屏幕 2 上的返回按钮时,它将返回屏幕 1,但屏幕 1 的标题消失了。这是我的 sn-p 代码
screen1 .m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = @"title 1";
....
屏幕 2 .m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = self.stringTitle;
// change the back button and add an event handler
self.navigationItem.hidesBackButton = YES;
//set custom image to button if needed
UIImage *backButtonImage = [UIImage imageNamed:@"btn_back.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:backButtonImage forState:UIControlStateNormal];
button.frame = CGRectMake(-12, 2, backButtonImage.size.width, backButtonImage.size.height);
[button addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
UIView *backButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, backButtonImage.size.width-15, backButtonImage.size.height)];
[backButtonView addSubview:button];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:backButtonView];
self.navigationItem.leftBarButtonItem = customBarItem;
self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};
....
}
- (void) backAction
{
[self.navigationController popViewControllerAnimated:YES];
}
我尝试了几种技巧来使屏幕 1 的标题出现以下技巧:
-
在
viewwillappear方法上设置标题- (void)viewWillAppear:(BOOL)animated{self.title = @"tes";} -
在
viewdidappear方法上设置标题- (void)viewDidAppear:(BOOL)animated{ self.navigationController.navigationBar.topItem.title = @"YourTitle"; self.title = @"tes"; }
但标题仍然消失。请帮忙
这是我移动到下一个屏幕的方式
- (IBAction)btGenerateTokenAction:(id)sender {
SofttokenResultController *controller = [[SofttokenResultController alloc] initWithNibName:@"SofttokenResultController" bundle:nil];
controller.navigationController.navigationBar.backItem.title = @"Kembali";
[ViewLoadingUtil animateToNextView:controller from:self :@""];
}
+ (void) animateToNextView:(UIViewController *)controller from:(UIViewController *)fromController :(NSString *)navTitle{
NSLog(@"animateToNextView called");
[fromController.navigationController pushViewController:controller animated:YES];
}
【问题讨论】:
-
你是如何从 screen1 移动到 screen2 的?
-
试试
self.navigationItem.title。 -
第一次可见(在进入 VC2 之前)。你添加后退按钮。如果你使用导航栏,默认后退按钮将是 der
-
您是否在导航控制器标题视图中添加了任何视图
-
@Suhail 是的,在移动到 screen2 之前它是可见的
标签: ios objective-c title back-button navigationbar