2013-10-29 22 views
0

我試圖在Xcode 5編程方式添加一個連接按鈕,我的項目中創建連接按鈕,類似的概念顯示出在這個環節上所示的例子之一:編程在Xcode5

enter image description here

的我使用的代碼如下所示:

- (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; 

} 

但是,按鈕沒有出現,當我啓動應用程序...它可能是因爲我在創建視圖以某種方式阻止該按鈕的圖形?

enter image description here

編輯:我使用相同的代碼,如蘋果公司的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); 
} 
+0

你使用的是導航控制器嗎?你能告訴你如何創建你的「圖表」嗎? – sergio

+0

嗨@sergio,我編輯了這個問題,以顯示如何繪製圖表。 謝謝, 尼克 – narner

回答

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; 
0

確保self.navigationItem不是零!

+0

嗨KudoCC,剛剛檢查,它沒有設置爲零,所以它一定是別的..但是,感謝! – narner