I'm writing some Objective-C code and I've frequently hit the situation where I have to use a class variable to store a value for one time use. After consuming it I no longer need it. To me, storing this value in a class variable seems like code smell. Really the value should be passed in as a parameter to the methods I'm using.
I run across this usually when I'm consuming delegates. As an example, I have a UI with multiple buttons used to load and display a UIActionSheet
when they're tapped. This action sheet contains a date picker which sets a value for a UILabel
when the action sheet is dismissed.
- (IBAction)setPurchaseDateTapped {
self.activeField = purchaseDate;
[self loadDatePickerActionSheet:@"Edit Purchase Date"];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
self.activeField.text = value_from_UIActionSheet;
}
As you can see here, the actionSheet clickedButtonAtIndex callback doesn't allow me to pass the activeField
so I have to use a class variable. It seems more correct to write this:
- (void)actionSheet:(UIActionSheet *)actionSheet parameterValue:(id)parameter {
parameter.text = value_from_UIActionSheet;
}
I believe(?) that I can subclass the UIActionSheet and UIActionSheet delegate and add the signatures that I require, but again this seems like more effort than it's worth.
So my question is what is the best way to do what I'm trying to do?
I don't necessarily want to change the date picker/action sheet interface that I've created (although if there's a better pattern for setting multiple dates on a UIView while keeping the DatePicker out of the way, I'm all ears.)