int alert()
{
    //Alerting
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"0%" message:nil preferredStyle:UIAlertControllerStyleAlert];

    UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(5, 5, 50.0F, 50.0F)];
    
    UIButton *sampleButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [sampleButton setFrame:CGRectMake(0,5,60.0F, 40.0F)];
    [sampleButton setBackgroundImage:[UIImage systemImageNamed:@"terminal.fill"] forState:UIControlStateNormal];

    [sampleButton addTarget:[[MyObject alloc] init] action:@selector(showTerminal) forControlEvents:UIControlEventTouchDown];
    
    [customView addSubview:sampleButton];

    [alert.view addSubview:customView];

    dispatch_async(dispatch_get_main_queue(), ^{
        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
    });
}

@implementation MyObject

-(void)showTerminal
{
    NSLog(@"Terminal opened!");
}

@end

从另一个@实现调用函数时,如何使@ Select 器工作! 我已经try 过此代码,但该函数从不执行"[sampleButton addTarget:[[MyObject Alalc]init]action:@selector(ShowTerm)for ControlEvents:UIControlEventTouchDown];",有人能帮我修复它吗?或者告诉我一个不涉及使用视图控制器的解决方法!

推荐答案

addTarget不是保留,所以您的对象被释放.您可以使用类对象和方法来检索事件

[sampleButton addTarget:[MyObject class] action:@selector(showTerminal) forControlEvents:UIControlEventTouchDown];

以及:

+ (void)showTerminal

Ios相关问答推荐

SwiftUI图表-默认情况下,X轴数据点的间距不相等

自定义UIControl不适用于UITapGestureRecognizer

SwiftData共享扩展阻止应用程序打开SQLite文件

多协议和类继承混乱,Swift

SwiftUI 故障/错误 - 在 LazyVStack 和 ScrollView 中使用 AVPlayer 时状态栏不显示

核心媒体基础知识,了解 CMSampleBuffer、CMBlockBuffer 和 CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer

如何在应用程序中显示我的小部件的快照?

从iOS键盘输入时Flutter TextField输入消失

小部件链接有效,但不执行任何操作

.overlay 和 body 是什么 swift 概念

Flutter 错误:CocoaPods not installed or not in valid state.

iOS 8 - 使用自定义演示关闭视图控制器后屏幕空白

检测 iPhone/iPad/iPod touch 的 colored颜色 ?

警告:iPad:Icon-72.png:图标尺寸(0 x 0)

UITableView 页脚,停止浮动内容

是否可以在没有 Mac 的情况下为 iOS 制作 PhoneGap 应用程序?

判断可选数组是否为空

SRCROOT 和 PROJECT_DIR 有什么区别?

闭包不能隐式捕获变异的 self 参数

将视图控制器从一个情节提要移动或复制到另一个情节提要