我最近一直在提問有關UIToolbars的問題,但是現在我發現我需要以編程方式向它添加項目,我已經看到其他人的方法如何去做,但是當我嘗試做同樣的事情時,沒有任何事情出現。指出這個問題是我需要幫助的。這裏是我的連接在IB:以編程方式向UIToolbar添加項目不工作
這裏是相關的代碼:
頭文件
#import <UIKit/UIKit.h>
@interface ParkingRootViewController : UIViewController {
UINavigationController *navigationController;
UIToolbar *toolbar;
UIBarButtonItem *lastUpdateLabel;
}
@property(nonatomic, retain) IBOutlet UINavigationController *navigationController;
@property (nonatomic, retain) IBOutlet UIToolbar *toolbar;
@property (nonatomic, retain) IBOutlet UIBarButtonItem *lastUpdateLabel;
- (IBAction)selectHome:(id)sender;
@end
實現文件:
- (void)viewDidLoad {
[super viewDidLoad];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 150.0f, 20.0f)];
label.text = @"last updated...";
label.textColor = [UIColor colorWithWhite:1.0 alpha:1.0];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = UITextAlignmentCenter;
label.font = [UIFont boldSystemFontOfSize:13.0];
label.userInteractionEnabled = NO;
lastUpdateLabel = [[UIBarButtonItem alloc] initWithCustomView:label];
[label release];
[toolbar setItems:[NSArray arrayWithObject:lastUpdateLabel]];
[self.view addSubview:self.navigationController.view];
//[self.view addSubview:toolbar];
//[self.navigationController.view addSubview:toolbar];
[self.navigationController.view setFrame:self.view.frame];
}
任何幫助不勝感激!
編輯:
我刪除無論我在筆尖,這將導致工具欄顯示/修改,我我在viewDidLoad中的代碼更新到以下幾點:
self.navigationController.toolbarHidden = NO;
//creating label in tool bar
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 150.0f, 20.0f)];
label.text = @"last updated...";
label.textColor = [UIColor colorWithWhite:1.0 alpha:1.0];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = UITextAlignmentCenter;
//label.highlightedTextColor = [UIColor colorWithWhite:0.5 alpha:1.0];
//label.highlighted = YES;
label.font = [UIFont systemFontOfSize:13.0];
label.userInteractionEnabled = NO;
UIBarButtonItem *lastUpdateLabel = [[UIBarButtonItem alloc] initWithCustomView:label];
//[lastUpdateLabel initWithCustomView:label];
//[label release];
//[toolbar setItems:[NSArray arrayWithObject:lastUpdateLabel]];
[self setToolbarItems:[NSArray arrayWithObject:lastUpdateLabel]];
我結束了顯示一個空白的工具欄。我啓動了調試器,這就是我所看到的: 啊哈! lastUpdateLabel的視圖的_text字段超出了範圍!但爲什麼?我該如何補救?提前致謝!
編輯2:
我已經能夠添加標籤和用下面的代碼的NSActivityIndicator:
@synthesize refreshDataButton;
//...
self.navigationController.toolbarHidden = NO;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20.0f, 0.0f, 80.0f, 40.0f)];
label.text = @"last updated...";
label.textColor = [UIColor colorWithWhite:1.0 alpha:1.0];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = UITextAlignmentCenter;
label.font = [UIFont systemFontOfSize:13.0];
label.userInteractionEnabled = NO;
[self.toolbar addSubview:label];
// create activity indicator
// dist frm lft, dist frm top
CGRect frame = CGRectMake( 90.0, 11.0, 25.0, 25.0);
UIActivityIndicatorView *loading = [[UIActivityIndicatorView alloc] initWithFrame:frame];
loading.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
[loading sizeToFit];
loading.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleBottomMargin);
[loading startAnimating];
[self.toolbar addSubview:loading];
但是當我嘗試添加的UIBarButtonItem我有沒有運氣(不顯示在工具欄上):
self.refreshDataButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:100 target:self action:@selector(refreshDataButtonTapped)];
[self setToolbarItems:[NSArray arrayWithObject:refreshDataButton]];
這裏是頭文件:
#import <UIKit/UIKit.h>
//#import <CoreData/CoreData.h>
@interface ParkingRootViewController : UIViewController {
UINavigationController *navigationController;
UIToolbar *toolbar;
UIBarButtonItem *refreshDataButton;
//UIActivityIndicatorView *loading;
}
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
@property (nonatomic, retain) IBOutlet UIToolbar *toolbar;
@property (nonatomic, retain) UIBarButtonItem *refreshDataButton;
//@property (nonatomic, retain) IBOutlet UIActivityIndicatorView *loading;
@property (nonatomic, readonly) NSString *applicationDocumentsDirectory;
-(IBAction)selectHome:(id)sender;
-(void)testCoreData;
-(void)refreshDataButtonTapped;
@end
有沒有想法?謝謝!
難道你的建議,沒有運氣。我只看到一個純灰色的工具欄。拋出這行代碼:toolbar.barStyle = UIBarStyleBlackOpaque;實際上得到的工具欄變成黑色,所以連接不會混亂或任何東西......感謝您的迴應。 – Stunner 2010-12-18 04:34:22