2

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.

IOS_DEV
  • 949
  • 3
  • 12
  • 33
  • And your question is? The 50% transparency does not work? Does it appear fully or not at all? Did you check upon the alpha value in the debugger or with the aid of nslog? Did you try alternative values such as 0 or 0.3f? – Hermann Klecker Feb 23 '13 at 19:38
  • The button appears with the alpha set to 1.0, so it doesn't change its state at all. – IOS_DEV Feb 23 '13 at 19:41

3 Answers3

2

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.

Darren
  • 10,091
  • 18
  • 65
  • 108
  • I don't really understand what you're trying to tell me. If you could post a bit of code, I think I would understand it much better. Thanks anyway. – IOS_DEV Feb 23 '13 at 19:46
  • I could do that, but I would need to understand a little bit more about how your code is set up for it to be useful to you. Is the ImageViewSubclass view a subview that has been added to a ViewController's view? And show your code for how you add the UITapGestureRecognizer to the ImageViewSubclass, and where you are adding it. – Darren Feb 23 '13 at 19:52
  • Yes, I add the `ImageViewSubclass` into the `ViewController`. I actually have many `ImageViewSubclassess`and when I detect that more than one is pressed, my idea is to set an alpha to an `UIButton` which is also inside the `ViewController`. The problem is that the code to handle if there's more than one selected is inside the `ImageViewSubclass`. I hope you understand it better now. – IOS_DEV Feb 23 '13 at 20:02
  • 1
    I would suggest that you should move the code detecting if more than one is pressed to the ViewController. – Darren Feb 23 '13 at 20:49
  • In any case, the tapDetected function will need to call a ViewController function, don't you think? I don't see the advantadges of puting the detecting code in the ViewController. – IOS_DEV Feb 23 '13 at 21:57
  • If you call it in the ViewController you just need to use: [self setAlphaToButton]; – Darren Feb 23 '13 at 22:04
0

Your view is not loaded at the moment you trying to set alpha! You need to call this method after your viewDidLoad fired. You can force it by calling view, but it's kind of hackand not recommended!

MyViewController *vc = [MyViewController new];
vc.view; // this string will force view loading 
[vc setAlphaToButton];
iiFreeman
  • 5,165
  • 2
  • 29
  • 42
  • Ok then try to NSLog(@"self.button = %@", self.button); inside yours setAlphaToButton method. Is it == nil? Better to set IBOutlets additional setting in viewDidLoad method. – iiFreeman Feb 23 '13 at 22:13
  • You have to realize that you creating a new instance of your view controller and setting alpha, you shouldn't expect that some existind view in your views hierarchi will be changed. Also make sure that tap detected method is actually fired. – iiFreeman Feb 23 '13 at 22:18
0

Add a property of uiviewcontroller class in imageviewsubclass as

ImageViewSubclass.h
@propery (nonatomic, retain) uiviewController *parent;
ImageViewSubclass.m
@synthesize parent;

And initialize it with "self" in view controller class when initalize object of imageviewsubclass and add on the view like

ImageViewsubclass *oneObj = [ImageViewsubClass alloc] init];
oneOBj.parent = self;

do the same for all objects of ImageviewsubClass objects.

and in

- (void) tapDetected:(UITapGestureRecognizer *)tapRecognizer {
    [parent setAlphaToButton];
}
andreamazz
  • 4,256
  • 1
  • 20
  • 36
robin.cssoft
  • 136
  • 6
  • This might work, but it is a violation of MVC to have a view controller as a property of a view. It would be better to use delegation and make the view controller a delegate of the view. The code would be similar. – Darren Feb 25 '13 at 17:25
  • @Darren Here we only have refrence to viewcontroller which is responsible against any action perform on view nothing else – robin.cssoft Feb 26 '13 at 10:41
  • Yes, I understand. And it functionally is the same things as having a delegate. But the reason that it's discouraged is that someone working with your code will see that they can set a view controller property on an ImageViewSubclass object, but from looking at the interface they have no way of knowing that the code will crash if the view controller doesn't implement setAlphaToButton. – Darren Feb 26 '13 at 12:55