我的tableViewCell存储一个字符串数组。每次我在按下一个按钮后输入一个新的字符串,它就会被添加到该数组中。但是,我不希望将相同的字符串两次添加到此数组中。如何防止它们被添加?我还希望在发生复制时出现一条警告消息。
发布于 2015-12-16 19:55:18
在将任何对象添加到array
之前,请检查array
是否已经包含该对象,如下所示
if([yourArray containsObject:@"xyz"]){
// show alert
}
else{
[yourArray addObject:@"xyz"];
}
发布于 2015-12-16 19:57:32
使用if
& else
并在if
中进行检查-输入的元素是否存在于其中else
将该元素输入到数组中:)
发布于 2015-12-16 19:59:53
尝尝这个,
- (IBAction)btnClicked:(id)sender
{
if([arr_of_Strings containsObject:yourTextField.text])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"ERROR"
message:@"Same Element"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
else
{
[arr_of_Strings addObject:yourTextField.text];
}
}
https://stackoverflow.com/questions/34311317
复制相似问题