开发者

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. after declaring them in my ViewController.h and ViewController.m files like so:

 //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 p开发者_运维知识库roceeded 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).

My method to make the buttons work in my ViewController.m file is as follows:

 -(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.

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:

- (IBAction)buttonPressed:(id)sender;

To have Interface Builder recognise it as an action. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜