在对象c中更改alpha并启用ClassA的UIButton [英] Change alpha and enabled UIButton of ClassA from ClassB in object c

查看:123
本文介绍了在对象c中更改alpha并启用ClassA的UIButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ClassB中更改UIButton的参数,但是UIButton在ClassA中声明。
我如何从ClassB看到?



只需:

  _closeButton.alpha = 1; 
_closeButton.enabled = TRUE;






感谢您的回覆!



ok我已经尝试过了:
in ClassB.h


ClassA * _closeButtonController;


inBB didLoad:


_closeButtonController = [[ClassA alloc] initWithNibName:@ClassAbundle:nil];
_closeButtonController._closeButton.enabled = TRUE;
_closeButtonController._closeButton.alpha = 1;


我没有错误,但不工作!






@Warkst in ClassA.h我已经delcared @property(nonatomic,retain)IBOutlet UIButton * _closeButton;和ClassA.m @synthesize _closeButton;使用下划线无处不在!



我已经解决了这个代码:
在ClassA.m

   - (void)viewDidLoad 
{
[super viewDidLoad];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showAndEnableCloseButton)name:@EnableCloseButtonobject:nil];
}

....

   - (void)showAndEnableCloseButton 
{
_closeButton.alpha = 1;
_closeButton.enabled = YES;
}

在ClassB.m
当TouchUpInsideUIButton时,关闭查看ClassB并返回ClassA视图,调用表示NSNotificationCenter ClassA的_closeButton中的更改:

   - (IBAction)close: (id)sender 
{
[[NSNotificationCenter defaultCenter] postNotificationName:@EnableCloseButtonobject:self];
[self.view removeFromSuperview];
}

我希望我对某人有帮助,
谢谢大家为您的反应!
Good Day!

解决方案

ClassA将是ClassB的委托。当ClassB需要更改ClassA中的UIButton时,它会直接从ClassA(它的委托)调用一个方法。 Inside ClassB:

  [self.delegate changeButtonFor:self]; 

然后,ClassA中的changeButton方法将直接对UIButton执行所需的操作。



您需要在ClassB中创建一个委托属性,并将其设置为ClassA:

  self.delegate = _closeButtonController; 

这是你需要放在你的ClassB头文件中:

  @class ClassB; 

@protocol ButtonDelegate
- (void)changeButtonFor:(ClassB *)aClass;
@end

@interface ClassB:...
{
id< ButtonDelegate>代表;
}

@property(assign)id< ButtonDelegate>代表;

不要忘记在实现文件中合成它:

  @synthesize delegate; 

现在,在ClassA的头文件中,声明它采用ButtonDelegate协议:

  #importClassB.h

@interface ClassA:...< ButtonDelegate>然后在实现中实现changeButtonFor方法来做你想要的与butted:



  @implementation ClassA 

- (void)changeButtonFor:(ClassB *)aClass
{
button1.alpha = 0.5;
...
}


I change a UIButton's parameter in ClassB, but UIButton is declared in the ClassA. How do I do see from ClassB?

Simply:

_closeButton.alpha = 1;
_closeButton.enabled = TRUE;


Thanks for reply!!

ok I have tried this: in ClassB.h

ClassA *_closeButtonController;

in ClassB.m didLoad:

_closeButtonController = [[ClassA alloc] initWithNibName:@"ClassA" bundle:nil]; _closeButtonController._closeButton.enabled = TRUE; _closeButtonController._closeButton.alpha = 1;

I haven't error but dont' work!


@Warkst in ClassA.h I have delcared @property (nonatomic, retain) IBOutlet UIButton *_closeButton; and in ClassA.m @synthesize _closeButton; Use the underscore everywhere !

I have resolved with this code: in ClassA.m

    - (void)viewDidLoad
{
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showAndEnableCloseButton) name:@"EnableCloseButton" object:nil];
}

....

- (void)showAndEnableCloseButton
{
    _closeButton.alpha = 1;
    _closeButton.enabled = YES;
}

In ClassB.m When "TouchUpInside" UIButton, close the View of ClassB and return on view of ClassA and call means NSNotificationCenter the changes in _closeButton of ClassA:

- (IBAction)close:(id)sender
{
    [[NSNotificationCenter defaultCenter]postNotificationName:@"EnableCloseButton" object:self];
    [self.view removeFromSuperview];
}

I hope I was of help to someone, Thank you all for your responses! Good Day!

解决方案

You should do this using delegation. ClassA would be the delegate of ClassB. When ClassB needs to change the UIButton in ClassA, it calls a method from ClassA (its delegate) that does this directly. Inside ClassB:

[self.delegate changeButtonFor:self];

The changeButton method in ClassA would then do what you want, directly to the UIButton.

You need to create a delegate property in ClassB, and set it to ClassA before:

self.delegate = _closeButtonController;

This is what you need to put inside your ClassB header file:

@class ClassB;

@protocol ButtonDelegate
- (void)changeButtonFor:(ClassB *)aClass;
@end

@interface ClassB : ...
{
    id <ButtonDelegate> delegate;
}

@property (assign) id <ButtonDelegate> delegate;

Don't forget to synthesize it in the implementation file:

@synthesize delegate;

Now, inside the header file of ClassA, declare that it adopts the ButtonDelegate protocol:

#import "ClassB.h"

@interface ClassA: ... <ButtonDelegate>

Then implement the changeButtonFor method in the implementation to do what you want with the butted:

@implementation ClassA

- (void)changeButtonFor:(ClassB *)aClass
{
    button1.alpha = 0.5;
    ...
}

这篇关于在对象c中更改alpha并启用ClassA的UIButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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