开发者

UIBarButtonItem referencing the custom view to later editing

开发者 https://www.devze.com 2023-04-07 02:08 出处:网络
I hope this is a simple question, I have a UIBarButtonItem which I initialized using a UILabel as a custom view, the button is living inside toolbar.

I hope this is a simple question, I have a UIBarButtonItem which I initialized using a UILabel as a custom view, the button is living inside toolbar. What I want to do is being able to change the text from the label that is inside the UIBarButtonItem, here is my code:

    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.tex开发者_如何学Got = [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 have no idea how to gain reference to the inner view of the button again, any clues? I was thinking on doing something like this: (but it is not working).

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


The label is the customView of bar button,

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


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

that should do it but I haven't tested it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号