0

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.

Moxy
  • 4,162
  • 2
  • 30
  • 49
Pedroinpeace
  • 1,419
  • 1
  • 14
  • 20

1 Answers1

0

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...

HackyStack
  • 4,887
  • 3
  • 22
  • 28