我正在尝试保存按钮的按下顺序,然后重播该顺序并按最初按下的顺序运行分配给按钮的操作?谁能帮我这个忙吗?

最佳答案

每个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-保存按钮单击顺序并按保存顺序重播 Action ?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23992109/

10-14 20:45
查看更多