0

我有成功创建出现在导航栏上的自定义 UIBarButtonItem 的代码。我在其他视图控制器中使用该代码,它运行良好,但在 UITableViewController 中,按钮显示为常规 BackButtonItem。有人可以帮我吗?这是我在所有 viewController 的 viewDidLoad 方法中使用的代码

@implementation P2OListViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {

    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];

    self.navigationItem.leftBarButtonItem = nil;
    self.navigationItem.hidesBackButton = YES;

    UIImage *backButtonImage = [UIImage imageNamed:@"button-back@2x.png"];
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setImage:backButtonImage forState:UIControlStateNormal];
    backButton.frame = CGRectMake(0, 0, 60, 35);

    [backButton addTarget:self
                   action:@selector(goToMenu:)
         forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *back = [[UIBarButtonItem alloc]initWithCustomView:backButton];
    self.navigationItem.backBarButtonItem = back;

- (void)viewWillAppear:(BOOL)animated {

}

- (void)goToMenu:(id)sender {
[self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@结尾

4

1 回答 1

1

通过将其 hide 属性设置为 yes 来隐藏默认 backBarButtonItem

self.navigationItem.hidesBackButton = YES;

在你看来会出现

- (void)viewWillAppear:(BOOL)animated 
{
self.navigationItem.leftBarButtonItem = nil;
    self.navigationItem.hidesBackButton = YES;

    UIImage *backButtonImage = [UIImage imageNamed:@"button-back@2x.png"];
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setImage:backButtonImage forState:UIControlStateNormal];
    backButton.frame = CGRectMake(0, 0, 60, 35);

    [backButton addTarget:self
                   action:@selector(goToMenu:)
         forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *back = [[UIBarButtonItem alloc]initWithCustomView:backButton];
    self.navigationItem.backBarButtonItem = back;

}

如果上述方法不起作用,试试这个并看看会发生什么:

if (!self.navigationItem.backBarButtonItem.hidden) {
      self.navigationItem.backBarButtonItem.hidden = YES;
   }
   else { 
      NSLog(@"back button already hidden");
   }
于 2013-01-27T05:15:18.213 回答