Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 11-21-2011, 07:25 PM   #1 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 135
thephotographer is on a distinguished road
Default 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];
}
thephotographer is offline   Reply With Quote
Old 11-21-2011, 07:31 PM   #2 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

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.
baja_yu is offline   Reply With Quote
Old 11-21-2011, 08:37 PM   #3 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by thephotographer View Post
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];
}

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];
    }
}
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


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.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is online now   Reply With Quote
Old 11-21-2011, 08:40 PM   #4 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 135
thephotographer is on a distinguished road
Default

Ah perfect! thanks Duncan, thats exactly what im after! you always seem to have the answers
thephotographer is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 396
16 members and 380 guests
7twenty7, chiataytuday, Clouds, dedeys78, Duncan C, e2applets, EvilElf, iekei, ipodphone, jeroenkeij, leostc, mbadegree, Murphy, QuantumDoja, sacha1996, Sami Gh
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,125
Posts: 402,910
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 06:34 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0