从另一个类修改 UIButton 的 alpha 属性

Modifying UIButton#39;s alpha property from another class(从另一个类修改 UIButton 的 alpha 属性)
本文介绍了从另一个类修改 UIButton 的 alpha 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从另一个类更改 UIButtonalpha.在设置我的 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 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Get an event when UIBarButtonItem menu is displayed(显示UIBarButtonItem菜单时获取事件)