0

这是我在SOF上的第一篇文章。 我是Objective-C的新程序员。 继承人我正在处理的“问题”检测哪个按钮被按下? (然后行动)

我创建了2个UIbutton:一个从屏幕顶部拉UIImageView,另一个将其推回。 我有代码的行动**,但我不知道如何将它与if语句如果(按钮1按下),然后拉视图其他(按下按钮2),然后推回查看。

-(void)viewDidLoad{ 
{super viewDidLoad] 

UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
button1.frame = CGRectMake(200, 0, 90, 30); 
[button1 addTarget:self action:@selector(buttonPressed1) forControlEvents:UIControlEventTouchUpInside]; 
[self.view addSubview:button1]; 

UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
button2.frame = CGRectMake(400, 0, 90, 30); 
[button2 addTarget:self action:@selector(buttonPressed2) forControlEvents:UIControlEventTouchUpInside]; 
[self.view addSubview:button2]; 
} 


** 

-(void)buttonPressed1 { 

double speed = 1/round(random() % 100) + 1.0; 

UIImageView *banetteView2 = [[UIImageView alloc] initWithImage:banetteImage]; 
banetteView2.frame = CGRectMake(100, -740, 568, 790); 
banetteView2.opaque = NO; 
[self.view addSubview:banetteView2]; 

[UIView beginAnimations:nil context:banetteView2]; 
[UIView setAnimationDuration: 2*speed ]; 

//banetteView2.frame = CGRectMake(100, -1, 568, 790); 

UIImageView *banetteView = [[UIImageView alloc] initWithImage:banetteImage]; 
banetteView.frame = CGRectMake(100, -740, 568, 790); 
banetteView.opaque = NO; 
banetteView.hidden = YES; 
[self.view addSubview:banetteView]; 


[UIView setAnimationDidStopSelector:@selector(onAnimationComplete:finished:context:)]; 
[UIView setAnimationDelegate:self]; 
[UIView commitAnimations]; 


} 

- (void)onAnimationComplete:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context { 

UIImageView *banetteView2 = context; 
[banetteView2 release]; 


double speed = 1/round(random() % 100) + 1.0; 
banetteView2.frame = CGRectMake(100, 0, 568, 790); 
banetteView2.opaque = NO; 
[self.view addSubview:banetteView2]; 

[UIView beginAnimations:nil context:banetteView2]; 
[UIView setAnimationDuration: 2*speed ]; 

//banetteView2.frame = CGRectMake(100, -740, 568, 790); 

} 

回答

3

我不知道这是否适用于UIButtons,但对于普通可可按钮,您将添加一个参数有关发件人是这样的:

-(void)buttonPressed1:(id)sender 
{ 
    // And now you can check which button is the sender 
    if(sender==button1) 
     // Do stuff.... 
} 

然后ofcourse你就需要添加一个冒号就当你设置按钮动作的选择:(buttonPressed1 :) @selector

+0

这也适用于UIButton。 – Eiko 2010-10-14 15:04:07

+0

我认为这应该可行,但事实是我的button1是'未声明'。我试图在buttonPressed1功能中重新创建它,但也没有工作。 – Gaspard 2010-10-15 09:16:18

+0

这是因为它被限制在它声明的方法范围内。为了从几个方法访问它,你需要声明它在类中的宽度。你可以通过在你的类的接口,在.h文件中声明它,或者如果它不需要在接口中可见,你可以在你的类中声明@implementation和第一种方法。 – Splashdust 2010-10-15 13:00:23