【发布时间】:2016-12-06 01:16:55
【问题描述】:
我有一个程序,用户通过文本字段将字符串输入 NSMutableArray (myArray)。这个数组被传递到下一个视图控制器,其中有一个标签(myLabel)和两个按钮。打印到标签的是来自 myArray 的随机字符串。 ButtonA 按下时显示来自数组的不同随机字符串,而 ButtonB 删除打印到标签的当前字符串,然后将数组中的随机字符串显示到标签。
这是我目前的解决方案:
- (void)viewDidLoad {
self.myLabel.text = [self.myArray objectAtIndex:arc4random() % [myArray count]];
-(IBAction)ButtonA:(id)sender {
self.myLabel.text = [self.myArray objectAtIndex:arc4random() % [myArray count]];
}
-(IBAction)ButtonB:(id)sender {
NSInteger index = [myArray indexOfObject: //what goes here?];
[self.myArray removeObjectAtIndex:index];
self.myLabel.text = [self.myArray objectAtIndex:arc4random() % [myArray count]];
}
有没有办法获取显示的随机字符串的索引,然后将其从数组中删除?我希望它继续这样做,直到数组中的所有项目都被删除。谢谢
【问题讨论】:
标签: ios objective-c arrays nsmutablearray