Simple Button Solution

Discussion in 'iOS Programming' started by hotkarl, Nov 19, 2010.

  1. hotkarl macrumors newbie

    hotkarl

    Joined:
    Nov 19, 2010
    Location:
    Northern Utah
    #1
    I have a whole bunch of buttons with the following action attached. I would like to include, in this action, an opacity change of the button. I thought maybe self.alpha or sender.alpha but it won't work. I'm just learning and would love some help.

    Code:
    -(IBAction) clicked:(id)sender {
    	NSString *titleForButton = [sender titleForState: UIControlStateNormal];
    	NSString *imagePath = [[NSString alloc] initWithFormat:@"%@.jpg", titleForButton];
    	NSString *musicPath = [[NSString alloc] initWithFormat:@"%@.mp3", titleForButton];
    	[COLOR="Red"]?.alpha = .2;[/COLOR]
    	tab.text = imagePath;
    	music.text = musicPath;
    }
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    What didn't work with sender.alpha? sender should a pointer to the object that sent the message. Did you get a compiler warning? Or did it not work at runtime?
     
  3. hotkarl thread starter macrumors newbie

    hotkarl

    Joined:
    Nov 19, 2010
    Location:
    Northern Utah
    #3
    I get attached failed message.
     

    Attached Files:

  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    You have not cast the id. Try

    Code:
    ((UIButton *) sender).alpha=0.2;
    
     
  5. hotkarl thread starter macrumors newbie

    hotkarl

    Joined:
    Nov 19, 2010
    Location:
    Northern Utah
    #5
    Thank you! I owe you a cheeseburger. Is there a particular article I should read to become familiar with that idea?

    Thanks again.
     
  6. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    It's basic C. You did learn C before Objective-C right? id is a shorthand for void *. Clearly that is untyped (effectively) so the compiler gives warnings about unrecognised messages.
     
  7. hotkarl thread starter macrumors newbie

    hotkarl

    Joined:
    Nov 19, 2010
    Location:
    Northern Utah
    #7
    No man. I've never done any programming. I am reading Programming in Objective-C 2.0 by Kochan and watching the iPhone tutorials at thenewboston.com. I'm brand new to all of this. Objective C is my first programming language.
     
  8. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #8
    There are two trains of thought on here (and a thread about it). I say you should learn C before Objective-C. Others say it's not required. Then something like this happens and I think I'm right.
     
  9. hotkarl thread starter macrumors newbie

    hotkarl

    Joined:
    Nov 19, 2010
    Location:
    Northern Utah
    #9
    Perhaps I will learn C as well. If it considered to be a prerequisite then I had probably ought to figure it out.

    Thank you once again.
     
  10. hotkarl thread starter macrumors newbie

    hotkarl

    Joined:
    Nov 19, 2010
    Location:
    Northern Utah
    #10
    What if I wanted to change the opacity of all the buttons that weren't the sender?
     
  11. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #11
    Code:
    [sender setAlpha:0.2];
    Should work with no typecast and is probably a little cleaner.
     
  12. seepel macrumors 6502

    seepel

    Joined:
    Dec 22, 2009
    #12
    Seconded!
     
  13. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #13
    You would have to have kept a reference to all those buttons somewhere you have access to.
     
  14. North Bronson macrumors 6502

    Joined:
    Oct 31, 2007
    Location:
    San José
    #14
    Big-ups to the OG Objective-C 1.0.
     
  15. hotkarl thread starter macrumors newbie

    hotkarl

    Joined:
    Nov 19, 2010
    Location:
    Northern Utah
    #15
    [sender setAlpha:0.2]; worked fine as well.

    Thanks!

    I am trying to figure out how to make the current button's alpha go back to normal when the next button is clicked by doing what robbieduncan said and somehow keeping a reference of the current button. If I can't get it on my own I may be back here to get some more advice from all you super gurus.

    Thanks again.
     

Share This Page