2010-12-18 68 views
5

我最近一直在提问有关UIToolbars的问题,但是现在我发现我需要以编程方式向它添加项目,我已经看到其他人的方法如何去做,但是当我尝试做同样的事情时,没有任何事情出现。指出这个问题是我需要帮助的。这里是我的连接在IB:以编程方式向UIToolbar添加项目不工作

alt text

这里是相关的代码:

头文件

#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]]; 

我结束了显示一个空白的工具栏。我启动了调试器,这就是我所看到的: enter image description here 啊哈! lastUpdateLabel的视图的_text字段超出了范围!但为什么?我该如何补救?提前致谢!

编辑2:

我已经能够添加标签和用下面的代码的NSActivityIndi​​cator:

@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 

有没有想法?谢谢!

回答

0

代码发布工作正常,我认为它必须是如何连接XIB。我会重新建立你在IB的连接(即打破所有连接并重新制作它们),保存你的XIB并重试。

+0

难道你的建议,没有运气。我只看到一个纯灰色的工具栏。抛出这行代码:toolbar.barStyle = UIBarStyleBlackOpaque;实际上得到的工具栏变成黑色,所以连接不会混乱或任何东西......感谢您的回应。 – Stunner 2010-12-18 04:34:22

12

该代码应该工作...有几个建议,我可以做。相反的:

[toolbar setItems:[NSArray arrayWithObject:lastUpdateLabel]]; 

试试这个:

[toolbar setItems:[NSArray arrayWithObject:lastUpdateLabel] animated:YES]; 

此外,由于您使用一个UINavigationController,NavControllers带着自己的工具栏,如果你喜欢,你可以使用。默认情况下,它是隐藏的,您可以通过这样做使人们看到:

self.navigationController.toolbarHidden = NO; 

,你可以通过这样设置它的工具栏项目:

[self setToolbarItems:[NSArray arrayWithObject:lastUpdateLabel]]; 

希望这TID位可以帮助你。祝你好运!

+0

谢谢,我发现了self.navigationController.toolbarHidden = NO;对我有用/为我工作。不过,我仍然遇到一个空白的工具栏。请参阅上面对我的问题所作的编辑。 – Stunner 2011-02-05 03:30:54

+2

它似乎你正在使用两个不同的工具栏。如果你想使用导航工具栏,你必须使用“self.navigationController.toolbar”来提及它。如果您正在使用自己的工具栏,那么您可以提及使用“self.toolbar”,如果它已被设置为属性。您的子视图似乎已添加到您自己的工具栏中,但正在为导航工具栏设置项目。那就是我会开始寻找问题的地方。 – Bittu 2011-05-24 20:18:35

0

按钮需要被viewController加上推送。该navController持有酒吧,但栏上的项目是由正在显示的viewController控制(添加)。这样,每种不同类型的vc都有自己的酒吧。

因此,采取barbuttonitem代码,并将其放在vc的init部分,并享受。

0

//试试这个。

- (void)viewDidAppear:(BOOL)animated 
{ 
    [toolbar setItems:[NSArray arrayWithObject:lastUpdateLabel]]; 
} 
0

您可以添加标签工具栏直接如果u有框架实例化它...例如

UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(7,7,200,30)]; 

    [lbl setBackgroundColor:[UIColor clearColor]]; 

    [lbl setText:@"Test lbl"]; 

    [_toolBar addSubview:lbl]; 
相关问题