自行创建的按钮后面的操作将不起作用。
我有一个简单的问题,但我不知道为什么它不起作用。 我以编程方式创建了一个按钮,我想将方法“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" ];
}
I've got a simple problem but I don't know why it doesn't work.
I created a button programatically and I'd like to add the method "backLogin" to the action.
Strange enough, I don't get any errors, but the button just won't respond in any way. Anywhere I look on the internet, this code should definitely work.
Any ideas what I'm doing wrong?
-(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];
}
The code behind "backLogin" has already worked in other circumstances:
-(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" ];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要将 myButton 添加到 loginImage,而是尝试将其添加到主视图(例如 [self.view addSubview:myButton])。您可能需要更改框架坐标才能正确定位按钮。
Instead of adding myButton to loginImage, try adding it to the main view (e.g. [self.view addSubview:myButton]). You may need to change the frame coordinates to position the button properly.