例如,我如何迭代一个方法调用。
[self revertBox1];
[self revertBox2];
[self revertBox3];
[self revertBox4];
[self revertBox5];
// They are as many as 20.
example1 * xmp = [[example1 alloc] initWithNibName:@"example1" bundle:nil];
// How can I iterate the class instantiation example1, example2, example3
// If I use this method..
NSString *classNameStr = [NSString stringWithFormat:@"example%d", i];
Class cls = NSClassFromString(classNameStr);
cls *obj = [[[cls alloc] initWithNibName:classNameStr bundle:nil];
//I receive an error [ Use of undeclared identifier 'obj' ]
最佳答案
你应该重新考虑你的设计和方法......而不是有 20 个方法名称 -revertBoxN,不如做一个方法:
- (void)revertBox:(int)index { }
然后您必须用特定大小的 C 数组替换您的 ivars 列表(exampleN)。
您可以使用简洁的循环遍历元素并避免重复代码。
希望您能理解我所说的...我认为我不必深入了解更多细节(否则您应该学习 Objective-C 的基础知识(包括 C!)。
关于iphone - objective C 迭代方法调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6502076/