Q:

Modifying UIButton's alpha property from another class

I'm trying to change the alpha of an UIButton from another class. The function that is called in set the alpha property of my UIButton is actually called because I've put a NSLog there and I can see how it works. I'd be thankful if you could give me any suggestion.
Here's my current code.
ViewController.h
- (void) setAlphaToButton;

@property (strong, nonatomic) IBOutlet UIButton *myButton;

ViewController.m
@synthesize myButton;

- (void) setAlphaToButton {
    myButton.alpha = 0.5;
    NSLog(@"Alpha set");
}

ImageViewSubclass.m
- (void) tapDetected:(UITapGestureRecognizer *)tapRecognizer {
    ViewController *VC = [[ViewController alloc] init];
    [VC setAlphaToButton];
}

And when the image view is pressed, in my console I get: Alpha set. And the button doesn't change.

A:

In your code, an instance of ViewController is alloced and inited, and the method setAlphaToButton is called on it. Then the view controller is released because you have no object retaining it. That's why you don't see any effect; the ViewController instance you call the method on never appears on screen.
It's not clear how your code is supposed to work; do you have an instance of ViewController in existence when tapDetected is called? If this is the case, and this is the ViewController whose button you want to alter the alpha of, then you need to have a reference to that instance of ViewController and call setAlphaToButton on it.

