2012-04-10 112 views
1

在我的項目(「Xcode 4.3.2 - 單一視圖應用程序」)中,我手動添加了一個帶有按鈕項目的工具欄,我需要以編程方式更新按鈕標籤。 我該怎麼做?你可以幫我嗎?xCode - 工具欄按鈕項目標籤更改

ViewController.h

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController <UIWebViewDelegate>{ 
    IBOutlet UIWebView *webDisplay; 
    IBOutlet UIToolbar *menuBar; //my toolBar 
} 
@property(nonatomic,retain) UIWebView *webDisplay; 
@property(nonatomic,retain) UIToolbar *menuBar; 

- (IBAction) changeLabel:(id)sender; 

@end 

ViewController.m

- (IBAction) changeLabel:(id)sender{ 

    //code for updating label 

} 
+1

可能重複http://stackoverflow.com/questions/1225457/change-the-text-of-a-uilabel-uibarbuttonitem -on-a-toolbar-programmatically) – Gray 2012-04-11 13:05:42

回答

3
@interface ViewController : UIViewController <UIWebViewDelegate>{ 
    IBOutlet UIWebView *webDisplay; 
    IBOutlet UIToolbar *menuBar; //my toolBar 
    IBOutlet UIBarButtonItem *barButton; 
} 
... 

- (IBAction) changeLabel:(id)sender{ 

barButton.title = @"test"; 

} 

一定要設置BarButtonItem爲引用奧特萊斯 「barButton」

+0

非常感謝!現在它的工作正常... – shebi 2012-04-10 12:30:13

0
#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController <UIWebViewDelegate>{ 

IBOutlet UIWebView *webDisplay; 
IBOutlet UIToolbar *menuBar; //my toolBar 

    UIButton* btnBack; 
} 

@property(nonatomic,retain) UIWebView *webDisplay; 

@property(nonatomic,retain) UIToolbar *menuBar; 

- (IBAction) changeLabel:(id)sender; 

@end 

ViewController.m

btnBack = [UIButton buttonWithType:UIButtonTypeCustom]; 
[btnBack setFrame:CGRectMake(0, 0, 49, 30)]; 
[btnBack setBackgroundImage:[UIImage imageNamed:@"backbtn.png"] forState:UIControlStateNormal]; 
[btnBack.titleLabel setFont:[UIFont boldSystemFontOfSize:12]]; 
UIBarButtonItem *abackButton = [[UIBarButtonItem alloc] initWithCustomView:btnBack] 



- (IBAction) changeLabel:(id)sender{ 

//code for updating label 
[btnBack setTitle:NSLocalizedString(@"Detail_Back", nil) forState:UIControlStateNormal]; 

} 
的[工具欄上的更改一個UILabel(的UIBarButtonItem)的文本編程](
相關問題