0

トリガーする特定のメソッドで特定の UIControl にアプローチしたいとしましょう。「sender」を使用してメソッドに UIControl へのポインターを送信できます。しかし、何らかの理由で、送信者のプロパティに直接アクセスできず、setX:forState: メソッドを使用する必要があります。プロパティに直接アクセスしても、エラーや警告は表示されません。それは単に何もしません...

次に例を示します。

h. file...
@interface MYViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIButton *dButton; //connected to a UIButton in IB
-(IBAction)dButtonClick:(UIButton*)sender; //connected to the same UIButton in IB
@end

それから...

m. file...
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.dButton.titleLabel.textColor = [UIColor greenColor]; //this is working...
}
-(IBAction)dButtonClick:(UIButton*)sender 
{
    sender.titleLabel.textColor = [UIColor redColor]; //this is not working...
    self.dButton.titleLabel.textColor = [UIColor redColor]; //this is also not working...
    [sender setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; //only this is working. 
    //But why?!?!?
}

これらのアイテムがどのように機能するかについていくつかの情報を検索しようとしましたが、その背後にあるロジックを十分に明確に説明するものは見つかりませんでした.

どんな助けでも...まあ...役に立つ...

4

1 に答える 1

2

これ:

sender.titleLabel.textColor

この:

[sender setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

ドット表記だけでなく、状態によっても異なります。ボタンは状態ごとに異なる色を持っているため、直接設定された色は状態の色によって上書きされます。

この行:

self.dButton.titleLabel.textColor = [UIColor redColor];

同じ問題であるか、コンセントを接続していない可能性があります。

于 2013-08-08T08:42:36.510 に答える