I write Objective-C code, and I often come across a situation where I need to use a class variable to store a value for one-time use. After that, I no longer need. For me, storing this value in a class variable is like the smell of code. Indeed, the value should be passed as a parameter to the methods that I use.
I usually come across this when I use delegates. As an example, I have a user interface with several buttons that are used to load and display the UIActionSheet when they are used. This action sheet contains a date picker that sets a value for UILabel when the action sheet is rejected.
- (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 actionSheetButtonAtIndex callback does not allow activeField to be activeField , so I need 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 I need, but again this seems like a lot of effort than it costs.
So my question is the best way to do what I'm trying to do?
I don't necessarily want to change the date / action selector interface I created (although if there is a better template for setting multiple dates in a UIView, keeping the DatePicker aside, Iām all ears.)
objective-c iphone delegates uiactionsheet
Gavin miller
source share