iphone - objective C 迭代方法调用

标签 iphone objective-c ios methods call

例如,我如何迭代一个方法调用。

[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/

相关文章:

ios - 仅适用于 iPhone 的方向锁定

ios - 未安装应用程序时检索深层链接 (iOS)

ios - 在 Xcode 6.4 中处理强引用循环

ios - 从 didReceiveRemoteNotification 调用的第一个 HTTP 请求总是失败

iphone - iPhone 7 Plus 双摄像头的深度图

iphone - CALayer 使用的简单示例——也许在 UITextField 中

ios - 在 SKView 前面显示 UIView - iOS - SpriteKit

iphone - 如何在 Objective-C 中编写计时器?

iOS:本地化的 UILabel 文本未对齐

iphone - 分页和 UITableView?