RightBarButtonItem不会出现在导航栏中

时间:2013-01-26 23:03:32

标签: ios xcode uinavigationbar uibarbuttonitem uinavigationitem

这让我感到害怕!我只是尝试以编程方式在我的导航栏中添加一个右侧“闪存”按钮,无论出于何种原因,我都无法显示它。我的代码如下。谁能帮我?提前致谢。

 //Note: flashButton is declared as a UIButton property in .h, and synthesized in .m        
    UIView *flashContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 36, 36)];

    [flashButton addTarget:self action:@selector(toggleTorch) forControlEvents:UIControlEventTouchUpInside];
    [flashButton setBackgroundImage: [UIImage imageNamed:@"FlashIconInactive_small.png"] forState:UIControlStateNormal];
    [flashButton setBackgroundImage: [UIImage imageNamed:@"FlashIconActive_small.png"] forState:UIControlStateHighlighted];

    [flashContainer addSubview:flashButton];
    [flashButton sizeToFit];

    UIBarButtonItem *flashButtonItem = [[UIBarButtonItem alloc] initWithCustomView:flashContainer];

    scannerVC.delegate = self;
    scannerVC.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelTapped)];
    [[scannerVC navigationItem] setRightBarButtonItem:flashButtonItem];

1 个答案:

答案 0 :(得分:1)

flashButton是否通过IBOutlet连接到xib或storyboard中的按钮?如果没有,您需要使用+[UIButton buttonWithType:]创建UIButton实例。

另一个尝试的测试是在第一行设置断点(addTarget:one)并在调试器中输入po flashButton。什么输出?如果它为零,那么您的按钮不存在,需要创建。