从另一个类修改UIButton的alpha属性 [英] Modifying UIButton's alpha property from another class

查看:114
本文介绍了从另一个类修改UIButton的alpha属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从另一个类更改 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实例?如果是这种情况,并且这是ViewController,你想要改变其按钮的alpha,那么你需要引用那个 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属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆