gpt4 book ai didi

iphone - 动态声明函数名?

转载 作者:行者123 更新时间:2023-12-03 17:32:46 25 4
gpt4 key购买 nike

我正在创建一个带有操作函数的栏按钮列表(sFuncName 如下),它是动态更改的。

当用户点击按钮时,sFuncName 将被调用。

for(int i = 0; i < 3 ; i++){

NSString* sFuncName = [NSString stringWithFormat:@"OnFunc_%d:", i ];
barButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:systemItem target:self
action:NSSelectorFromString(sFuncName)];
}

问题是:我有办法动态声明 sFuncName 来响应按钮吗?

示例:

for(int i = 0; i < 3 ; i++){

NSString* sFuncName = [NSString stringWithFormat:@"OnFunc_%d:", i ];
- (void)sFuncName: (id)sender; //sFuncName: OnFunc_0, OnFunc_1, OnFunc_2
}

最佳答案

我没有看到动态创建方法声明的优势,因为这些方法中的代码也不是动态的。

因此,您应该声明一个由每个按钮调用的单个方法,向该方法传递一个标签或对发送者(按钮)的引用或一些其他值,使您的方法能够告诉您之前已点击了哪个按钮到被调用的方法。你可以这样做:

-(void)buttonPressed: (int)tag {

if (tag == 0) {
// Code for first button
}

if (tag == 1) {
// Code for second button
}

// prepare code for further buttons...

}

关于iphone - 动态声明函数名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3768358/

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