Xcode 自创按钮背后的操作赢得';不行。

Xcode 自创按钮背后的操作赢得';不行。,xcode,uibutton,action,css-selectors,Xcode,Uibutton,Action,Css Selectors,我有一个简单的问题,但我不知道为什么它不起作用。 我通过编程创建了一个按钮,我想将“backLogin”方法添加到操作中。 奇怪的是,我没有收到任何错误,但按钮不会以任何方式响应。无论我在互联网上看到什么地方,这段代码都应该是有效的。 知道我做错了什么吗 -(void) loginAppear{ UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; myButton.frame = CGRectMake(

我有一个简单的问题,但我不知道为什么它不起作用。 我通过编程创建了一个按钮,我想将“backLogin”方法添加到操作中。 奇怪的是,我没有收到任何错误,但按钮不会以任何方式响应。无论我在互联网上看到什么地方,这段代码都应该是有效的。 知道我做错了什么吗

-(void) loginAppear{

UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(20, 20, 75, 37); // position in the parent view and set the size of the button
[myButton setTitle:@"Back" forState:UIControlStateNormal];
// add targets and actions
[myButton addTarget:self action:@selector(backLogin) forControlEvents:UIControlEventTouchUpInside]; 
// add to a view
[myButton setAlpha:1];
[loginImage addSubview:myButton];      
}
“backLogin”背后的代码已经在其他情况下起作用:

-(void) backLogin{

CALayer *layer = loginImage.layer;    

//spin frame
CABasicAnimation *anim1 =
[CABasicAnimation animationWithKeyPath: @"transform" ];
anim1.toValue = [ NSValue valueWithCATransform3D:
                 CATransform3DMakeRotation(180 * M_PI, 0.0, 1.0, 0) ];
anim1.duration = .2;
anim1.cumulative = YES;
anim1.repeatCount = 1;
[layer addAnimation: anim1 forKey: @"transformAnimation" ];       

}

不要将myButton添加到loginImage,而是尝试将其添加到主视图(例如[self.view addSubview:myButton])。您可能需要更改帧坐标以正确定位按钮。

您是否尝试在backLogin方法中添加NSLog以确认该方法是否被调用。。。也许那里的代码不起作用什么是loginImage?您是否看到按钮被按下的任何视觉指示?您好,loginImage是UIImageView实例。我现在就试试NSLog…谢谢!现在,我从我的按钮得到一个反应,但是一个错误;[loginViewController backLogin:]:无法识别的选择器发送到实例0x1ad2c0,但我没有看到任何错误?[loginViewController backLogin:]中的:似乎表示它希望backLogin是一个接受一个参数的函数。也许您需要:-(void)backLogin:(UIButton*)发送者。对于addTarget调用,使用@selector(backLogin:)。更好的是,将backLogin更改为:-(void)backLogin:(id)sender