1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

NSControlのプロパティの変更とアクションの実行はどちらが先? という話

Posted at

NSSliderをいっこだけ配置したアプリケーションを作ってみる。

slider.png

構造は極めて単純で

  • NSSliderのvalueには AppDelegateの sliderValueプロパティを bindする
  • sliderの操作は AppDelegateの sliderActionで受ける

図にするとこう。
fig0.png

実装も単純に。

setSliderValue
-(void)setSliderValue:(NSInteger)v
{
    NSLog(@"setSliderValue");
    
    if (_sliderValue == v)
        return;
    
    _sliderValue = v;
}
sliderAction
-(IBAction)sliderAction:(id)sender
{
    NSLog(@"sliderAction");
}

で、実行してみた

2014-05-17 09:59:49.638 SliderActionAndBind[1832:303] setSliderValue
2014-05-17 09:59:49.639 SliderActionAndBind[1832:303] sliderAction
2014-05-17 09:59:52.174 SliderActionAndBind[1832:303] setSliderValue
2014-05-17 09:59:52.175 SliderActionAndBind[1832:303] sliderAction
2014-05-17 09:59:52.960 SliderActionAndBind[1832:303] setSliderValue
2014-05-17 09:59:52.960 SliderActionAndBind[1832:303] sliderAction
...

どうやらプロパティの変更→アクションの実行の順で行われるようだ。他のコントロールは試してないし、実行のたびに変わるのかもしれない。この辺Appleの公式ドキュメントのどこに書かれているんだろう・・・

そもそもプロパティの setterだけでいいんじゃないの

アクションとプロパティを別々に書く必要なんてあるの、setterの中にactionの処理書いても同じなんじゃない、という話もあるんだけど、そうとも言えない問題が起きたのが今回の疑問の発端。

この辺についてもうちょっと書こうと思ったけど少し考えをまとめてから別記事に。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?