简体   繁体   English

如何从 controller objective-c 访问按钮事件

[英]how to access buttons events from controller objective-c

I just started to learn Objective-C and iPhone SDK programming.我刚开始学习 Objective-C 和 iPhone SDK 编程。 I have made 2 rect buttons and one label in my XIB design (it is appearing after clicking on views).我在我的 XIB 设计中制作了 2 个矩形按钮和一个 label(单击视图后出现)。 I have to make code in button click event in my controller.我必须在我的 controller 中的按钮单击事件中编写代码。 As in c#.net we simply take button.click event (dummy code).与在 c#.net 中一样,我们只需获取 button.click 事件(虚拟代码)。

Like I have 2 rect buttons and their title is 8 and 9 and one label.就像我有 2 个矩形按钮,它们的标题是 8 和 9 以及一个 label。 When I will click on button 8 my label will print 8 and similarly when I will click on button 9 my label will show 9. My concern is that is how will know in controller (.m file) which button is being pressed.当我单击按钮 8 时,我的 label 将打印 8,类似地,当我单击按钮 9 时,我的 label 将显示 9。我担心的是如何知道 Z594C103F2C6E04C3D8AB059F031E0 文件中的哪个按钮被按下。

Set the tag field of your buttons to different values, then define a method将按钮的tag字段设置为不同的值,然后定义一个方法

-(IBAction)buttonPressed:(id)sender;

and inside it call [(UIView *)sender tag] .并在其中调用[(UIView *)sender tag] Checking that value you will know what button has been pressed.检查该值,您将知道按下了哪个按钮。

Alternatively you may call two different methods, but since the behavior is such similar I'd prefer the first solution.或者,您可以调用两种不同的方法,但由于行为如此相似,我更喜欢第一种解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM