pass different types eg UIView, UIBUtton to a method
hi,
i have a method where i perform common tasks such as changing hidden, alpha, tag etc, but the only way i can see how to do it, is to have one of these methods for each type of object i want to pass to it, so one method to pass a UIView to it, another one for UIButton, another one for UIImageView, etc etc. it becomes a heap of code that is completely repeated, other than the type in the method name.
I tried to put the type in the method name as id, but then i get an error in xcode saying "Property 'alpha' not found on object of type 'id'"
is there a way to just have the one lot of code i can pass anything to, eg UIView, UIBUtton, UIImageView, etc
thanks.
Code:
-(void)applyAlphaHiddenIDToView:(UIView*)currentView
{
if ([currentConfigItem valueForKey:kalpha] != nil)
currentView.alpha = [[currentConfigItem valueForKey:kalpha] floatValue];
if ([[currentConfigItem valueForKey:khidden] isEqualToString:@"YES"])
currentView.hidden = YES;
if ([currentConfigItem valueForKey:kID] != nil)
currentView.tag = [[currentConfigItem valueForKey:kID] integerValue];
}
Code:
-(void)applyAlphaHiddenDisabledIDToButton:(UIButton*)currentButton
{
if ([currentConfigItem valueForKey:kalpha] != nil)
currentButton.alpha = [[currentConfigItem valueForKey:kalpha] floatValue];
if ([[currentConfigItem valueForKey:khidden] isEqualToString:@"YES"])
currentButton.hidden = YES;
if ([[currentConfigItem valueForKey:kenabled] isEqualToString:@"NO"])
currentButton.enabled = NO;
if ([currentConfigItem valueForKey:kID] != nil)
currentButton.tag = [[currentConfigItem valueForKey:kID] integerValue];
}
Actually, UIButton, UIImageView etc are all subclasses of UIView, so your method can be declared as having a UIView type parameter and you can pass it the others. All the properties you mentioned (tag, alpha, hidden) are all inherited from UIView.
i have a method where i perform common tasks such as changing hidden, alpha, tag etc, but the only way i can see how to do it, is to have one of these methods for each type of object i want to pass to it, so one method to pass a UIView to it, another one for UIButton, another one for UIImageView, etc etc. it becomes a heap of code that is completely repeated, other than the type in the method name.
I tried to put the type in the method name as id, but then i get an error in xcode saying "Property 'alpha' not found on object of type 'id'"
is there a way to just have the one lot of code i can pass anything to, eg UIView, UIBUtton, UIImageView, etc
thanks.
Code:
-(void)applyAlphaHiddenIDToView:(UIView*)currentView
{
if ([currentConfigItem valueForKey:kalpha] != nil)
currentView.alpha = [[currentConfigItem valueForKey:kalpha] floatValue];
if ([[currentConfigItem valueForKey:khidden] isEqualToString:@"YES"])
currentView.hidden = YES;
if ([currentConfigItem valueForKey:kID] != nil)
currentView.tag = [[currentConfigItem valueForKey:kID] integerValue];
}
Code:
-(void)applyAlphaHiddenDisabledIDToButton:(UIButton*)currentButton
{
if ([currentConfigItem valueForKey:kalpha] != nil)
currentButton.alpha = [[currentConfigItem valueForKey:kalpha] floatValue];
if ([[currentConfigItem valueForKey:khidden] isEqualToString:@"YES"])
currentButton.hidden = YES;
if ([[currentConfigItem valueForKey:kenabled] isEqualToString:@"NO"])
currentButton.enabled = NO;
if ([currentConfigItem valueForKey:kID] != nil)
currentButton.tag = [[currentConfigItem valueForKey:kID] integerValue];
}
As baja_yu says, the properties you mention are all properties from UIView, so you're good.
If you want to set properties that are not inherited from UIView, cast your pointer to a different type.
If, for example, you want to set the image and title for a button's normal state, or the value for a slider, you could use code like this:
Code:
-(void)applyAlphaHiddenIDEnabledTitleImageToView:(UIView*)currentView
{
if ([currentConfigItem valueForKey:kalpha] != nil)
currentView.alpha = [[currentConfigItem valueForKey:kalpha] floatValue];
if ([[currentConfigItem valueForKey:khidden] isEqualToString:@"YES"])
currentView.hidden = YES;
if ([currentConfigItem valueForKey:kID] != nil)
currentView.tag = [[currentConfigItem valueForKey:kID] integerValue];
if ([[currentConfigItem valueForKey:kenabled] isEqualToString:@"NO"])
viewAsButton.enabled = NO;
//Now look for the tags that apply changes to a button.
if ([currentView isKindOfClass: [UIButton class])
{
//Create a pointer to the view, type-cast as a button
UIButton* viewAsButton = (UIButton*) currentView;
NSString* buttonTitle = [currentConfigItem valueForKey:kTitle];
if (buttonTitle != nil)
[viewAsButton setTitle buttonTitle forState: UIControlStateNormal];
UIImage* buttonImage = [currentConfigItem valueForKey:kImage];
if (buttonImage != nil)
[viewAsButton setImage buttonImage forState: UIControlStateNormal];
}
else if ([currentView isKindOfClass: [UISlider class])
{
//Now handle the settings for sliders
NSNumber* sliderValue = [currentConfigItem valueForKey: kValue];
if (sliderValue != nil)
//You can also cast the view to a new class in the assignment,
//but it's harder to read.
((UISlider*)currentView).value = [sliderValue floatValue];
}
}
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.