VisualStudio for Mac Cocoa App.用の個人的な備忘録その2。
概要
- ViewにTextFieldとラベルを追加。
- TextFieldのテキスト編集に連動してラベルのテキストが変更される。
利用するAPI:
- NSTextField
- XamarinのWorking_with_Text_Controls によると、「Changed」「EditingBegan」「EditingEnded」の3つのイベントが利用出来るらしい。 Displaying Data and Responding to EventsのComboBoxのChangedイベントを参考にNSTextFieldで試してみた。
ソリューションの新規作成
省略
InterfaceBuilderで編集
- ViewControllerのViewにTextFieldとLabelをドラッグ
- 適当にサイズ変更
ウィンドウリサイズでマージンを設定したい場合は、ステータスバー「Add New Constraints」でRed I-Beamを変更。 (参照:Hello, Mac Creating the Interface 7.)
AssistantEditorにViewController.hを表示して、TextFieldをCtrlキーを押したままドラッグ。 Connection:Outlet Name:TextField1 Type:NSTextFieldとしてConnect
同じく、ラベルをドラッグ。 Connection:Outlet Name:Label1 Type:NSTextFieldとしてConnect
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;
};
}
ラムダ式によるイベント記述ができるようだ。
実行画面
Reference:
- Xamarine - Developers - Guides - Mac - Standard Controls - Working with Text Controls
- 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