UIBarButtonItem引用自定义视图以便以后编辑

时间:2011-09-23 04:59:13

标签: iphone uiview uibarbuttonitem objective-c++

我希望这是一个简单的问题,我有一个UIBarButtonItem,我使用UILabel作为自定义视图进行初始化,按钮位于工具栏内。 我想要做的是能够从UIBarButtonItem内的标签更改文本,这是我的代码:

    NSDate *lastUpdateDate = [AWSyncEntity getLastUpdatedDateByEntityName:@"Patient" inManagedObjectContext:self.managedObjectContext]; 

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];//liberar
    [dateFormat setDateFormat:@"MM/dd/yyyy hh:mm a"];

    UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 180, 44.01)]; //liberar
    myLabel.font = [UIFont boldSystemFontOfSize:10];
    myLabel.textColor = [UIColor whiteColor];
    myLabel.backgroundColor = [UIColor clearColor];
    myLabel.text = [NSString stringWithFormat:@"Actualizado: %@", [dateFormat stringFromDate:lastUpdateDate]];

    UIBarButtonItem *btn2 = [[UIBarButtonItem alloc] initWithCustomView:myLabel]; //liberar    
    UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; //liberar        
    self.toolbarItems = [NSArray arrayWithObjects:flexibleSpace,btn2, flexibleSpace, nil];
    [self.navigationController setToolbarHidden:NO animated:YES];

    UIBarButtonItem *mybtn = (UIBarButtonItem *)[self.toolbarItems objectAtIndex:2];    
    //I was thinking this would be possible...
    //UILabel *mylbl = (UILabel *) [mybtn view];

    [flexibleSpace release];
    [btn2 release];
    [myLabel release];
    [dateFormat release];

我不知道如何再次获得对按钮内部视图的引用,任何线索? 我正在考虑做这样的事情:(但它不起作用)。

    //I was thinking this would be possible...
    //UILabel *mylbl = (UILabel *) [mybtn view];

2 个答案:

答案 0 :(得分:0)

标签是条形按钮的 customView

UILabel *mylbl = (UILabel *)[mybtn customView];

答案 1 :(得分:0)

UILabel * myLbl =(UILabel *)[mybtn customView];

应该这样做,但我还没有测试过。

相关问题