gui 绑定按钮事件 您所在的位置:网站首页 gui按钮绑定事件 gui 绑定按钮事件

gui 绑定按钮事件

2023-08-19 03:53| 来源: 网络整理| 查看: 265

gui 绑定按钮事件

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Outlet.  

可可中的插座是对GUI控件的持久引用。 它将属性(变量)连接到控件。 例如,通常为文本字段GUI控件创建一个Outlet,然后通过该Outlet更改此字段中显示的文本。

The normal way to use an Outlet is:  In the 64-bit Xcode, you just add a property with the IBOutlet keyword, synthesize it, and set new text via that property, as follows:

使用插座的通常方法是:在64位Xcode中,只需添加具有IBOutlet关键字的属性,对其进行合成,然后通过该属性设置新文本,如下所示:

In the interface section you declare a new property:

在接口部分,您声明一个新属性:

@property (retain) IBOutlet NSTextField * text;

@property(保留)IBOutlet NSTextField *文本;

In the implementation section you synthesize it; that is, have the compiler generate the get/set handlers:

在实现部分中,您可以对其进行综合; 也就是说,让编译器生成get / set处理程序:

@synthesize text;

@合成文字;

If you want to set text to the control, just assign the property a value:

如果要将文本设置为控件,只需为属性分配一个值:

text.stringValue = @"Hello, World!";

text.stringValue = @“你好,世界!”;

To get data from the control, just access the property:

要从控件获取数据,只需访问属性:

NSString* str = text.stringValue;

NSString * str = text.stringValue;

The same can be done without manual coding.  Doing it via the IDE is sometimes necessary, and it is useful and interesting to know how to do this.  

无需手动编码即可完成相同操作。 有时需要通过IDE进行操作,并且知道如何进行操作非常有用且有趣。

示例1-将文本输入绑定到标签 (Example 1 -- Bind a text input to a label ) 让我们来做一个测试应用程序: 1.在Xcode中创建新的Mac OS X Cocoa项目 (1. Create new Mac OS X Cocoa project in Xcode) New Project wizard in Xcode. Choose Mac OS X, Cocoa application template Set the project name and save it. 2.添加一个字符串属性 (2. Add a string property) 在应用程序委托类中,我添加了一个字符串: Application Delegate Class files in the project #import @interface TextNoOutletAppDelegate : NSObject { NSWindow *window; } @property (assign) IBOutlet NSWindow *window; @end #import @interface TextNoOutletAppDelegate : NSObject // Few lines removed from this place @property (assign) IBOutlet NSWindow *window; // New property added here @property (copy) NSString *text; @end #import "TextNoOutletAppDelegate.h" @implementation TextNoOutletAppDelegate @synthesize window; // synthesize new property @synthesize text; - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { // Insert code here to initialize your application } @end 3.添加标签控件 (3. Add the label control) 在Interface Builder中,在MainWindow.xib文件上单击两次以将其打开,然后将“文本字段”放在原型窗口上。 让我们在此文本字段下放置一个Label控件。 安排大小和位置。 Make GUI for the project in Interface Builder 4.设置绑定 (4. Set up the binding) 在原型窗口中选择文本字段。 按Command-4打开“绑定检查器”: Binding. Text Field control.

In the Value section check the checkbox Bind To.  Press on the button on the right and in the popup menu select the application delegate named Text No Outlet Application Delegate:

在“ 值”部分中,选中“ 绑定到 ”复选框。 按下右侧的按钮,然后在弹出菜单中选择名为Text No Outlet Application Delegate的应用程序代表

Binding. Text Field control.

Type text into the Key Model Path input field.  You should see something like this picture:

在“ 关键模型路径”输入字段中输入文本 。 您应该看到类似以下图片的内容:

Binding. Text Field control.

Select the label.  Press Command-4 to switch to the Binding Inspector. Check the Bind To checkbox, set the application delegate and modify the Modal Key Path in the same way as for the text field.

选择标签。 按Command-4切换到“绑定检查器”。 选中“ 绑定到”复选框,设置应用程序委托并以与文本字段相同的方式修改“模式键路径”。

Binding. Text Field control. 5.生成并运行程序 (5. Build and Run the program) 它看起来像这样: Binding. Text Field control. 示例2-将文本输入绑定到窗口标题栏 (Example 2 -- Bind a text input to the window caption bar ) 让我们将绑定应用于应用程序窗口:

In Interface Builder, select the window object in the Document window, press Command-4 to switch to the Binding Inspector and bind the title of the window in the same way as the label before:

在“界面生成器”中,在“文档”窗口中选择窗口对象,按Command-4切换到“绑定检查器”,并以与之前的标签相同的方式绑定窗口标题:

Bind the window title to the text property. Launch the test application. Window title changed. 示例3-将Slider控件绑定到标签 (Example 3 -- Bind a Slider control to a Label ) 我想再用滑块控件展示一个例子。 相同的原理:我将向应用程序委托类添加一个属性,例如“值”,并将其与滑块控件和标签绑定。

The property added to the application delegate is marked with the comment in the code below:

在下面的代码中,添加到应用程序委托的属性用注释标记:

#import @interface TextNoOutletAppDelegate : NSObject @property (assign) IBOutlet NSWindow *window; @property (copy) NSString *text; // New property for the Slider control @property (assign) NSInteger value; @end #import "TextNoOutletAppDelegate.h" @implementation TextNoOutletAppDelegate @synthesize window; @synthesize text; // Added line to synthesize new property @synthesize value; - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { // Insert code here to initialize your application } @end Slider control binding.

I'd like to set Continuous for the slider control:

我想为滑块控件设置“ 连续 ”:

Slider control attribute modification. Text Field. Binding via "Value with Pattern". Running test application 摘要: (Summary:) 在本文中,我们看到了GUI工作的一种“简化”:

We did not use Outlets of the GUI controls and

我们没有使用GUI控件的Outlets和

we connected more than one control to a property

我们将多个控件连接到一个属性

The first section of this article showed how to do the binding using Outlets.  I just used the IBOutlet keyword and then called the methods that get/set a value to/from a control.

本文的第一部分显示了如何使用Outlets进行绑定。 我只使用了IBOutlet关键字,然后调用了从控件获取/设置值的方法。

Then, in the other examples, I specified a key that is used by Cocoa to figure out which property to match up to this key:  The property text was added to the application delegate class and key ('text') was used to bind the GUI controls with the application delegate.

然后,在其他示例中,我指定了一个可可可用来确定与该键匹配的属性的键:该属性文本已添加到应用程序委托类中,并且键('text')用于绑定该键。具有应用程序委托的GUI控件。

参考文献: (References:) 您可以在以下文章中找到有关Objective-C 2.0中的键值编码的更多信息:

1. Mac OS Reference Library. Key-Value Coding Programming Guide.

1. Mac OS参考库。 键值编码编程指南。

2. Mac OS X Reference Library. Cocoa Bindings Programming Topics.

2. Mac OS X参考库。 可可绑定编程主题。

3. Cocoa with Love. 5 key-value coding approaches in Cocoa

3. 可可与爱。 可可中的5种键值编码方法

4. The Unix Geek. An Introduction to Key-Value Coding in Cocoa.

4. Unix极客。 可可中的键值编码简介。

翻译自: https://www.experts-exchange.com/articles/3381/Simple-Binding-Cocoa-GUI-Application-without-Outlets.html

gui 绑定按钮事件



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有