how to change toolbar item made in storyboard to show dynamic date

448 views Asked by At

I had a similar question yesterday and I got part of my answer from there. But the thread got closed so this is kinda my follow-up question thread :)

original Q: How can I have a toolbar button/item change its date with [NSDate date]?

original A:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MM/dd/yyyy"];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
yourToolBarItem = [NSString stringWithFormat:@"%@", [formatter stringFromDate:[NSDat date]]];

Followup Q: I have the toolbar made directly from navigationcontroller in storyboard . How can I reach its items programaticly and change item/object at index 0 to the current date.

I can't get my head around this. I have tried loads of types with [self navigationController.ToolbarItems] replacObjectAtIndexbut a big no for me so far. Very helpful for any directive and help.

1

There are 1 answers

0
HackyStack On

The easiest thing is to create a property for your button: @property IBOutlet UIBarButtonItem *myBarButton;

Then initialize it in viewWillAppear: myBarButton = [[UIBarButtonItem alloc] initWithTitle:(your date string) style: UIBarButtonItemStylePlain target:self action:(some action)]; // Of course you'll need to add it to the navigation bar here too...

Then when it's time to update the date on the button: [myBarButton setTitle:(your date string)];

Hope that helps...