gpt4 book ai didi

iphone - iOS - 动态按钮

转载 作者:可可西里 更新时间:2023-11-01 03:42:51 25 4
gpt4 key购买 nike

我正在尝试在我的项目中使用通过代码(无 IB)创建的动态按钮,它们出现在我想要的位置和方式,但它们不会触发它们的 Action 。

UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button1.frame = CGRectMake(475, 302, 49, 58);
[button1 setTitle:@"1"
forState:(UIControlState)UIControlStateNormal];
[button1 addTarget:self
action:@selector(goToFirstTrailer)
forControlEvents:(UIControlEvents)UIControlEventTouchDown];
[myImageView addSubview:button1];


-(void)goToFirstTrailer {
[self startAnimator:@"OutsideToTrailer1_" forNumFrames:60];
}

放置它的 imageView 有按钮和启用的用户交互。

如果您能提供任何帮助,我们将不胜感激。

最佳答案

我认为您的操作方法签名错误,请将该行更改为

-(void) goToFirstTrailer:(id)sender {

以及您将操作设置为

[button1 addTarget:self action:@selector(goToFirstTrailer:) forControlEvents:....

重要的是消息名称后的冒号,因为我更改了操作方法签名以包含发件人。

编辑 我在 MainWindow.xib 中编写了一个只有一个 imageView 的小示例项目,并以编程方式创建了一个按钮,如下所示

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

// Override point for customization after application launch.

UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button1.frame = CGRectMake(0.f, 0.f, 50.f, 50.f);
[button1 setTitle:@"1"
forState:(UIControlState)UIControlStateNormal];
[button1 addTarget:self
action:@selector(goToFirstTrailer:)
forControlEvents:(UIControlEvents)UIControlEventTouchDown];
imageView.userInteractionEnabled = YES; // <--- this has to be set to YES
[imageView addSubview:button1];
[self.window makeKeyAndVisible];

return YES;
}

它又快又脏,是的,我误用了应用程序委托(delegate)作为 View Controller 。我知道这很糟糕。

这是 Action 方法

- (void) goToFirstTrailer:(id)sender {
imageView.backgroundColor = [UIColor redColor];
}

在父 imageView 上设置 userInteractionEnabled 属性会有所不同。如果将其设置为默认值 NO,则不会将任何事件路由到按钮。

关于iphone - iOS - 动态按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5017865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com