自行创建的按钮后面的操作将不起作用。

发布于 2024-12-12 00:54:12 字数 1114 浏览 0 评论 0原文

我有一个简单的问题,但我不知道为什么它不起作用。 我以编程方式创建了一个按钮,我想将方法​​“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傾旎 2024-12-19 00:54:12

不要将 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文