问题描述
我正在使用以下数组:
NSMutableArray *buttonNames = [NSMutableArray arrayWithObjects:@"button1", @"button2", @"button3", nil];
然后我想遍历此数组,并使用每个数组元素作为对象名称来创建UIButton,如下所示:
I then want to loop through this array and create UIButtons with each array element as the object name, something like this:
for(NSString *name in buttonNames) {
UIButton name = [UIButton buttonWithType:UIButtonTypeCustom];
// ... button set up ...
}
但是这不起作用,我希望它能给我三个UIButton,分别是button1,button2和button3.
However this doesn't work, I would hope it would give me three UIButtons called button1, button2, and button3.
在Objective-C中这可能吗?我很确定这与指针/对象问题有关,但是我似乎找不到任何类似的例子.感谢您的任何回答,将不胜感激!
Is this possible in objective-c? I'm pretty sure this is to do with a pointer/object issue, but I can't seem to find any similar examples to go by. Thanks for any answers, they will be greatly appreciated!
推荐答案
在显示的代码中尝试执行的操作没有意义.您可以这样做:
No what you try to do in the code shown makes no sense. You can do this though:
for (NSString* name in buttonNames) {
UIButton* button = [UIButton buttonWithType: UIButtonTypeCustom];
button.title = name;
// TODO Add the button to the view.
}
这是你的意思吗?
这篇关于使用NSString作为对象名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!