gpt4 book ai didi

iOS - UIButton 作为 UIAlertController 上的 subview 未触发事件

转载 作者:行者123 更新时间:2023-11-29 00:08:00 25 4
gpt4 key购买 nike

我在 UIAlertController 上添加了 UIButton,但它没有在任何事件上触发该方法。

UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[cancelBtn setFrame:CGRectMake((_alertController.view.frame.size.width/2)-30, _alertController.view.frame.size.height-50, 60, 30)];
[cancelBtn setTitle:@"Cancel" forState:UIControlStateNormal];
[cancelBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[cancelBtn setBackgroundColor:[UIColor orangeColor]];
[cancelBtn addTarget:self action:@selector(forceFullDismiss_FireDelegate:) forControlEvents:UIControlEventTouchUpInside];
cancelBtn.showsTouchWhenHighlighted = YES;

[_alertController.view setUserInteractionEnabled:YES];
[_alertController.view addSubview:cancelBtn];

按钮似乎突出显示,但相应的事件未触发。

这里有什么问题?

最佳答案

这来自 UIAlertController documentation .

The UIAlertController class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.

如果要添加取消按钮,可以使用

UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
// action on cancel
}];

[_alertController addAction:cancel];

关于iOS - UIButton 作为 UIAlertController 上的 subview 未触发事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47429469/

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