我正在尝试从另一个类更改 UIButton
的 alpha
.在设置我的 UIButton 的 alpha
属性中调用的函数实际上是被调用的,因为我已经在其中放置了一个 NSLog
并且我可以看到它是如何工作的.如果您能给我任何建议,我将不胜感激.
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.
这是我当前的代码.
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];
}
当按下图像视图时,在我的控制台中我得到:Alpha set
.而且按钮没有变化.
And when the image view is pressed, in my console I get: Alpha set
. And the button doesn't change.
在您的代码中,分配并初始化了一个 ViewController 的实例,并在其上调用了 setAlphaToButton 方法.然后释放视图控制器,因为您没有保留它的对象.这就是为什么您看不到任何效果的原因;您调用该方法的 ViewController 实例永远不会出现在屏幕上.
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.
不清楚您的代码应该如何工作;调用 tapDetected 时是否存在 ViewController 的实例?如果是这种情况,并且这是您要更改其 alpha 的按钮的 ViewController,那么您需要引用 ViewController 的 那个 实例并在其上调用 setAlphaToButton.
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.
这篇关于从另一个类修改 UIButton 的 alpha 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!