编辑:我还需要答案!
我有两个UIViewControllers(我们称它们为ParentVC和SecondVC SecondVC上有11个UISwitch元素,还有一个按钮用于将每个UISwitch的状态从SecondVC发送到ParentVC。 在parentVC中,我需要一个NSArray,它将包含NSDictionary的密钥(在IBAction中描述)。我有IBA操作: 方法 默认情况下,filteredPlaceTypeArray_包含所有11个位置类型。 提出SecondVC的方法是: 我有很多问题: 我希望我的问题是正确的,谢谢,阿特姆。NSDictionary *resultDic = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool: [_cafeSwitch isOn]], @"Cafe",
.....
[NSNumber numberWithBool: [_bowlingSwitch isOn]], @"BowlingClub",
nil];
NSMutableArray *resultArray = [[NSMutableArray alloc] init];
for (NSString *key in resultDic) {
NSNumber *value = [resultDic valueForKey:key];
if ([value compare:[NSNumber numberWithInt:1]] == NSOrderedSame)
[resultArray addObject:key];
}
[_delegate sendSelectedPlaceTypeArray:[NSArray arrayWithArray:resultArray]];
[self dismissViewControllerAnimated:YES completion:^{}];
sendSelectedPlaceTypeArray
是在ParentVC中接收此NSArray的委托方法。默认状态下会选择所有切换器。在ParentVC中,我实现了委托方法:- (void)sendSelectedPlaceTypeArray:(NSArray *)placeTypeArray {
filteredPlaceTypeArray_ = placeTypeArray;
}
filteredPlaceTypeArray_ = [NSArray arrayWithObjects:@"Cafe", ... ,@"BowlingClub", nil];
- (IBAction)showPlaceTypeFilter:(id)sender {
UIStoryboard *iPhoneStoryboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
PlaceTypeFilterViewController *destViewController = [iPhoneStoryboard instantiateViewControllerWithIdentifier:@"PlaceTypeFilterViewController"];
destViewController.delegate = self;
destViewController.currentFilteredPlaceTypesArray = filteredPlaceTypeArray_;
[self presentViewController:destViewController animated:YES completion:nil];
}
据我所知,以下是从视图中获取所有对象的方法:
for (UISwitch *mySwitch in [self.view subviews]) {
if ([mySwitch isKindOfClass:[UISwitch class]]) {
NSLog(@"State == %@",[mySwitch description]);
//Do what ever you want to do with your switches
}
}
编辑:此答案与编辑前的原始问题有关。
如何以编程方式设置每个UISWitch的状态