导航栏后退按钮文本不会更改

时间:2014-12-03 14:14:29

标签: ios objective-c xcode uinavigationbar

我正在尝试更改导航栏后退按钮文本的文本。我已经尝试过这里提供的解决方案,但我无法获得我想要的文本。在appdelegate中,我设置了以下功能:

 [[UINavigationBar appearance] setBarTintColor:[self colorWithHexString:@"e74c3c"]];
 [[UINavigationBar appearance] setBarTintColor:[self colorWithHexString:@"e74c3c"]];   

[[UINavigationBar appearance] setTitleTextAttributes:
 [NSDictionary dictionaryWithObjectsAndKeys:
  [UIColor whiteColor], UITextAttributeTextColor,
  [UIFont fontWithName:@"Futura-CondensedMedium" size:19.0], UITextAttributeFont,nil]];

NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowOffset = CGSizeMake(0.0, 1.0);
shadow.shadowColor = [UIColor whiteColor];

[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil]
 setTitleTextAttributes:
 @{NSForegroundColorAttributeName:[UIColor whiteColor],
   NSShadowAttributeName:shadow,
   NSFontAttributeName:[UIFont fontWithName:@"Futura-CondensedMedium" size:19.0]
   }
 forState:UIControlStateNormal];

[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];

在每个viewcontroller的viewdidload我设置:

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"SomeText" style:UIBarButtonItemStylePlain target:nil action:nil];

UIBarButtonItem *newBackButton = 
        [[UIBarButtonItem alloc] initWithTitle:@"NewTitle" 
                                         style:UIBarButtonItemStyleBordered 
                                        target:nil 
                                        action:nil];
[[self navigationItem] setBackBarButtonItem:newBackButton];

有什么建议吗?

3 个答案:

答案 0 :(得分:4)

如果我没有误解你,你可以这样做来设置后退按钮的标题。假设有三个视图控制器:ABC。现在,当您从A导航到B时,您希望后退按钮的标题为“MyViewA”,然后您需要将此代码放在ViewController A的“viewWillAppear:”中(当您在视图控制器B上时,这将显示后退按钮的标题为“MyViewA”。

 //viewWillAppear of ViewController A
 -(void)viewWillAppear:(BOOL)animated{

       //now set title of back button
       self.navigationItem.backBarButtonItem =
       [[UIBarButtonItem alloc] initWithTitle:@"YourTitle"
                                  style:UIBarButtonItemStyleBordered
                                 target:nil
                                 action:nil];
 }

同样,如果您要从B导航到C并想要为ViewController C设置后退按钮标题,则需要将上述代码放在“viewWillAppear:”委托中ViewController B。简而言之,您需要在当前视图控制器上设置下一个视图控制器的标题。

答案 1 :(得分:1)

您无法更改后退按钮中文本的值。您必须将按钮设置为左侧或右侧barButtonItem(如果您希望将其作为后退按钮,可能会保留),如下所示:

[self.navigationItem setLeftBarButtonItem:newBackButton];

然后,您可以在启动按钮时将IBAction附加到按钮:

UIBarButtonItem *newBackButton = 
    [[UIBarButtonItem alloc] initWithTitle:@"NewTitle" 
                                     style:UIBarButtonItemStyleBordered 
                                    target:nil 
                                    action:yourBackFunction];

至少那将是一种解决方法,我之前也使用过它......

答案 2 :(得分:1)

后退按钮属于上一个视图控制器,而不是当前在屏幕上显示的控制器。 要修改后退按钮,您应该在推送之前更新它,在启动segue的视图控制器上:


protected void submitButton_Click(object sender, EventArgs e)
        {
                System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
                mail.To.Add("admin@gmail.com");
                mail.From = new MailAddress(EmailTextBox.Text, "Head", System.Text.Encoding.UTF8);
                mail.Subject = "Query";
                mail.SubjectEncoding = System.Text.Encoding.UTF8;
                mail.Body = "Contact Details" + "<br/> <b>Name:</b>" + Request["app-name"] + " <br/> <b>Email - Address :</b>" + EmailTextBox.Text + "<br/> <b>Description :</b>" + Request["AppointmentMessage"] + "<br/> <b>Contact Number :</b>" + Request["app-num"];
                mail.BodyEncoding = System.Text.Encoding.UTF8;
                mail.IsBodyHtml = true;
                mail.Priority = MailPriority.High;
                SmtpClient client = new SmtpClient();
                client.Credentials = new System.Net.NetworkCredential("admin@gmail.com", "admin");
                client.Port = 587;
                client.Host = "smtp.gmail.com";
                client.EnableSsl = true;
                try
                {
                    client.Send(mail);
                    Page.RegisterStartupScript("UserMsg", "<script>alert('Successfully Send...')</script>");
                }
                catch (Exception ex)
                {
                    Exception ex2 = ex;
                    string errorMessage = string.Empty;
                    while (ex2 != null)
                    {
                        errorMessage += ex2.ToString();
                        ex2 = ex2.InnerException;
                    }
                    Page.RegisterStartupScript("UserMsg", "<script>alert('Sending Failed...');if(alert){ window.location='SendMail.aspx';}</script>");
                }
            }
        }