简体   繁体   English

objective-c多按钮单击方法

[英]objective-c multiple button click method

I have 3 buttons on my main view (btn_easy, btn_medium, btn_hard) that I dragged onto my main view via the xCode Interface Builder Library - Round Rect Button item. 我的主视图上有3个按钮(btn_easy,btn_medium,btn_hard),我通过xCode Interface Builder Library - Round Rect Button项目拖到我的主视图上。 after declaring them in my ViewController.h and ViewController.m files like so: 在我的ViewController.h和ViewController.m文件中声明它们后,如下所示:

 //ViewController.h
 IBOutlet UIButton  *btn_easy;
 IBOutlet UIButton  *btn_medium;
 IBOutlet UIButton  *btn_hard;

 @property(nonatomic,retain) IBOutlet UIButton  *btn_easy;
 @property(nonatomic,retain) IBOutlet UIButton  *btn_medium;
 @property(nonatomic,retain) IBOutlet UIButton  *btn_hard;

 //ViewController.m
 @synthesize btn_easy,btn_medium,btn_hard;

I then proceeded to make Connection Outlets in the Interface Builder to the "File's Owner" by clicking on the btn_easy, btn_medium, btn_hard (sequentially) in my View, going to Connection Inspector and dragging the "New Referencing Outlet" to "File's Owner" (not sure if that was what I should have done here). 然后,我依次单击视图中的btn_easy,btn_medium,btn_hard,将其在Interface Builder中的连接出口设置为“文件的所有者”,转到Connection Inspector并将“新引用出口”拖动到“文件的所有者” (不确定这是否是我应该在这里执行的操作)。

My method to make the buttons work in my ViewController.m file is as follows: 使我的ViewController.m文件中的按钮工作的方法如下:

 -(void)buttonPressed: (id) sender{
      NSLog(@"button clicked = %@",sender);
 }

My problem is that when I click any of the buttons nothing appears in my NSLog when I should actually see "button clicked = btn_easy" when btn_easy is clicked, so on and so forth. 我的问题是,当我单击任何按钮时,当我点击btn_easy时,实际上应该看到“button clicked = btn_easy”时,我的NSLog中没有任何内容,等等。

Please help..... Thank you 请帮忙.....谢谢

You've confused outlets and actions, I think. 我认为你混淆了出口和行动。 An outlet - as you've defined - gives one class an outward connection to another. 一个出口 - 正如你所定义的那样 - 使一个类别与另一个类别向外连接。 An action is something a control can trigger. 控件可以触发一个动作。

You should add buttonPressed to your .h as: 您应该将buttonPressed添加到.h中:

- (IBAction)buttonPressed:(id)sender;

To have Interface Builder recognise it as an action. 使Interface Builder将其识别为动作。 You can then control-drag a link from a button to your class (which seems to be file owner) and connect the two up. 然后,您可以控制拖动一个按钮到您的类的链接(该类似乎是文件所有者)并将两者连接起来。 Which, I guess, you'll want to do three times. 我想,你想做三次。

This is the same as dragging a link from 'touch up inside' to your class, that being when buttons take effect on iOS. 这与将链接从“内部润饰”拖动到班级相同,即按钮在iOS上生效时。

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

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