0

我正在尝试在 Xcode 5 中以编程方式将连接按钮添加到我的项目中,类似于此链接中示例的概念中显示的按钮:

在此处输入图像描述

我正在使用的代码如下所示:

- (void) setupConnectButton
{
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:self.peripheral.isConnected ? NSLocalizedString(@"Disconnect", nil) : NSLocalizedString(@"Connect", nil) style:UIBarButtonItemStyleBordered  target:self action:@selector(connectAction:)];
    self.navigationItem.leftBarButtonItem = item;
    self.navigationItem.leftBarButtonItem.enabled = (self.peripheral != nil);

    self.beatsPerMinute.hidden = !self.peripheral.isConnected;
    self.legendLabel.hidden = !self.peripheral.isConnected;

}

但是,当我启动应用程序时,按钮没有出现......是否可能是因为我创建的图表以某种方式阻止了视图中的按钮?

在此处输入图像描述

编辑:我正在使用与 Apple AcceleratorGraph 项目中相同的代码创建图表:https ://developer.apple.com/library/IOS/samplecode/AccelerometerGraph/Introduction/Intro.html

绘制图形的代码如下所示:

// The graph view itself exists only to draw the background and gridlines. All other content is drawn either into
// the GraphTextView or into a layer managed by a GraphViewSegment.
- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    // Fill in the background
    CGContextSetFillColorWithColor(context, graphBackgroundColor());
    CGContextFillRect(context, self.bounds);

    CGFloat width = self.bounds.size.width;
    CGContextTranslateCTM(context, 0.0, 56.0);

    // Draw the grid lines
    DrawGridlines(context, 0.0, width);
}
4

1 回答 1

0

试试这行代码

    UIBarButtonItem *firstButton = [[UIBarButtonItem alloc] initWithTitle:@"First" style:UIBarButtonItemStyleBordered target:self action:@selector(firstButtonAction:)];
    UIBarButtonItem *secondButton = [[UIBarButtonItem alloc] initWithTitle:@"Second" style:UIBarButtonItemStyleBordered target:self action:@selector(secondButtonAction:)];

    UIToolbarTransparent *toolbar = [UIToolbarTransparent new];

    [toolbar setFrame:CGRectMake(0,0, 140,44)];
    [toolbar setItems:[NSArray arrayWithObjects:firstButton, secondButton, nil]];

    UIBarButtonItem *customBarButton = [[UIBarButtonItem alloc] initWithCustomView:toolbar];
    self.navigationItem.leftBarButtonItem = customBarButton;
于 2013-10-29T09:24:59.277 回答