LoginSignup
0
1

More than 5 years have passed since last update.

[VS-Mac]VisualStudio for Mac (Cocoa)でNSTextFieldのChangeイベント

Posted at

VisualStudio for Mac Cocoa App.用の個人的な備忘録その2。

概要

  • ViewにTextFieldとラベルを追加。
  • TextFieldのテキスト編集に連動してラベルのテキストが変更される。

利用するAPI:

ソリューションの新規作成

省略

InterfaceBuilderで編集

  1. ViewControllerのViewにTextFieldとLabelをドラッグ
  2. 適当にサイズ変更
  3. ウィンドウリサイズでマージンを設定したい場合は、ステータスバー「Add New Constraints」でRed I-Beamを変更。 (参照:Hello, Mac Creating the Interface 7.)
    NSTextField01.png

  4. AssistantEditorにViewController.hを表示して、TextFieldをCtrlキーを押したままドラッグ。 Connection:Outlet Name:TextField1 Type:NSTextFieldとしてConnectNSTextField02.png

  5. 同じく、ラベルをドラッグ。 Connection:Outlet Name:Label1 Type:NSTextFieldとしてConnect

  6. Outletが追加されたViewController.hNSTextField03.png

  7. SaveしてVisualStudioへ戻る。

コードの編集(ViewController.cs)

ViewDidLoad()を以下に変更

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            // Do any additional setup after loading the view.
            TextField1.Changed += (sender, e) =>
            {
                Label1.StringValue = TextField1.StringValue;
            };
        }

ラムダ式によるイベント記述ができるようだ。

実行画面

NSTextField04.png
TextFieldの文字列入力に連動してラベルが変化する。

Reference:

  1. Xamarine - Developers - Guides - Mac - Standard Controls - Working with Text Controls
  2. Xamarine - Developers - Guides - Mac - Application Fundamentals - Working with Databases - Databases and ComboBoxes - Displaying Data and Responding to Events

License:

Copyright (c) 2017 grayhead0603
Released under the MIT license

0
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
0
1