0
UIMapViewController *mapViewContr = [[[UIMapViewController alloc] initwithClientKey:clientKey] autorelease];
UINavigationController *nc = [[[UINavigationController alloc] initWithRootViewController:mapViewContr] autorelease];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 44, 44);
[button setImage:[UIImage imageNamed:@"info2.png"] forState:UIControlStateNormal];
UIBarButtonItem *lbutton = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
nc.navigationItem.leftBarButtonItem = lbutton;

上面的代码不起作用,leftBarButtonItem 不显示。但是当我在函数中设置 leftBarButtonItem 时: UIMapViewController.vieDidLoad , leftBarButtonItem 显示。为什么?流动的代码在 UIMapViewController.vieDidLoad

    UIButton *_button = [UIButton buttonWithType:UIButtonTypeCustom];
    _button.frame = CGRectMake(0, 0, 44, 44);
    //    [button setBackgroundImage:[UIImage imageNamed:@"info2.png"] forState:UIControlStateNormal];
    [_button setImage:[UIImage imageNamed:@"info2.png"] forState:UIControlStateNormal];
    UIBarButtonItem *lbutton = [[[UIBarButtonItem alloc] initWithCustomView:_button] autorelease];
    self.navigationItem.leftBarButtonItem = lbutton;
4

1 回答 1

0

在您调用的第一个代码块中:

nc.navigationItem.leftBarButtonItem = lbutton;

这应该是:

mapViewContr.navigationItem.leftBarButtonItem = lbutton;

您总是希望引用navigationItem视图控制器,而不是导航控制器。

于 2013-08-02T03:13:34.657 回答