【问题标题】:Programmatically Create Connect Button in Xcode5在 Xcode5 中以编程方式创建连接按钮
【发布时间】:2013-11-08 08:45:43
【问题描述】:

我正在尝试在 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);
}

【问题讨论】:

  • 您在使用导航控制器吗?您能展示一下您是如何创建“图表”的吗?
  • 嗨@sergio,我编辑了问题以显示如何绘制图表。谢谢,尼克

标签: ios button user-interface xcode5


【解决方案1】:

试试这行代码

    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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-14
    • 1970-01-01
    • 2014-10-16
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多