所以,我有一个UIPicker视图,只要输入不是" NULL"就会从NSMutableArray中填充。
所以我的选择器会显示除NULL以外的所有值。
现在,我有一个UITextField框和一个按钮。所以无论我在文本字段中输入什么,我点击按钮,如果它与NSMutableArray(用于填充UIPickerView)中的任何内容匹配,它将其设置为NULL并刷新UIPicker以使其不会#39 ; t再次显示。
出于某种原因,我能够将值设置为NULL(使用NSLog检查),但是选择器永远不会获得更新,NSMutable数组也不会。
-(void) loadthepicker
{
NSMutableArray *getarray = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"FilerNamesArray"]];
pickerLoaderArray=[[NSMutableArray alloc] init];
for (int j=0; j<20; j++) {
if ([[getarray objectAtIndex:j] isEqualToString:@"NULL"])
{
// do nothing..don't load
}
else // add that filter to pickerLoaderArray
{
[pickerLoaderArray addObject:[getarray objectAtIndex:j]];
}
} // end of for
[pickerView reloadAllComponents];
[pickerView selectRow:0 inComponent:0 animated:NO];
}
-(NSInteger)numberOfComponentsInPickerView:(NSInteger)component
{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)picker numberOfRowsInComponent:(NSInteger)component
{
return [pickerLoaderArray count];
}
-(NSString *)pickerView:(UIPickerView *)picker titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [pickerLoaderArray objectAtIndex:row];
}
按钮:
- (IBAction)deleteButton:(id)sender {
NSUserDefaults *CheckFiltersUsed = [NSUserDefaults standardUserDefaults];
NSInteger myInt = [CheckFiltersUsed integerForKey:@"FiltersUsed"];
if (myInt<=20 && myInt>0) {
NSLog(@"number of filters used before deleting %ld",(long)myInt);
[CheckFiltersUsed setInteger:myInt-1 forKey:@"FiltersUsed"];
[CheckFiltersUsed synchronize];
// get names array
NSMutableArray *getarray = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"FilerNamesArray"]];
NSArray *get=getarray;
// at location where name matches with selectedfilter..put NULL
for (int j=0; j<20; j++) {
if ( [[getarray objectAtIndex:j] isEqualToString:_filterToDelete.text] && isFilterDeleted==NO )
{
NSLog(@"------currently %d is %@",j,[getarray objectAtIndex:j]);
[getarray insertObject:@"NULL" atIndex:j];
NSLog(@"------now %d is %@",j,[getarray objectAtIndex:j]);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"" message: @"Deleted" delegate: nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alert show];
isFilterDeleted=YES;
[[NSUserDefaults standardUserDefaults] setObject:getarray forKey:@"FilerNamesArray"];
[[NSUserDefaults standardUserDefaults]synchronize];
[self loadthepicker];
}
else
{
NSLog(@"No matching filter name");
}
} // end of for
//now save this array back.
}
else
{
NSUserDefaults *CheckFiltersUsed = [NSUserDefaults standardUserDefaults];
NSInteger myInt = [CheckFiltersUsed integerForKey:@"FiltersUsed"];
NSLog(@"Wrong number of filters!!!... %d",myInt);
}
}
答案 0 :(得分:0)
如果我得到你想要做的事情,你想要从数组和选择器中删除相等的字符串。但是,您只需将另一个NSString对象插入索引'j'
即可在deleteButton方法中:
而不是这一行
[getarray insertObject:@"NULL" atIndex:j];
呼叫
[getarray removeObjectAtIndex:j];
**更新
在loadPicker中只需删除if语句以检查字符串是否等于@“NULL”
所以而不是:
for (int j=0; j<20; j++) {
if ([[getarray objectAtIndex:j] isEqualToString:@"NULL"])
{
// do nothing..don't load
}
else // add that filter to pickerLoaderArray
{
[pickerLoaderArray addObject:[getarray objectAtIndex:j]];
}
}
执行:
for(NSString *pickerValue in getarray){
[pickerLoaderArray addObject:pickerValue];
}