我正在尝试保存按下按钮的顺序,然后重播该顺序并按照最初按下按钮的顺序运行分配给按钮的操作?谁能帮我解决这个问题?
最佳答案
每个 UIControl
元素都有一个标签,您可以使用它来识别将要点击的各种按钮。当点击每个按钮时,将调用与该按钮关联的方法(选择器)(您甚至可以为所有按钮调用一个选择器并通过它们的标签区分它们)。
当每个按钮被点击时,通过将每个按钮的标签添加到队列(或在 Objective-C 中:NSMutableArray
)来跟踪哪个按钮被点击。然后要重放操作,您只需从队列中读取标签值并调用相应的选择器即可。
举例说明:
@property (nonatomic, strong) NSMutableArray *taskArray;
// in your init or viewDidLoad:
_taskArray = [NSMutableArray new];
// in the selector that is called by *all* buttons
-(IBAction) buttonTapped:(id)sender {
[_taskArray addObject:[NSNumber numberWithInteger:sender.tag]];
[self executeActionWithTag:sender.tag];
}
-(void) executeActionWithTag:(NSUInteger)tag {
if(tag == 1) {
// perform specific action 1 ...
} else if (tag == 2) {
// perform specific action 2 ...
}
// ...
}
-(void) replayButtonActions {
for (NSNumber *tag in _taskArray) {
[self executeActionWithTag:[tag integerValue]];
}
}
关于ios - Xcode - 保存按钮点击顺序并按保存顺序重播操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23992109/