gpt4 book ai didi

objective-c - 如何发送按钮事件 - UIControlEventTouchUpInside

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:27:16 26 4
gpt4 key购买 nike

在我的简单程序中,按钮有问题。按钮是 View 的 subview , View 是 myViewController.view 的 subview 。

此按钮是动态创建的,但它的操作不起作用。如何发送事件?

这是我的代码。

// View Controller

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{

bookCount = 0;

self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
UIView *subview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
[self.view addSubview:subview];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setFrame: subview.bounds];
[btn addTarget:self action:@selector(onBtnchild:) forControlEvents:UIControlEventTouchUpInside];
[subview addSubview:btn];
.....
}
return self;

}
-(void) onBtnchild:(id) sender{
NSLog(@"HI");
}

我可以看到我的按钮,但我无法点击。有什么想法请教教我。

最佳答案

你的代码应该是这样的:

 UIView *subview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];

UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

[btn setFrame: subview.bounds];

[btn addTarget:self action:@selector(onBtnchild:)
forControlEvents:UIControlEventTouchUpInside];

[subview addSubview:btn];

[self.view addSubview:subview];

关于objective-c - 如何发送按钮事件 - UIControlEventTouchUpInside,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7471411/

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