0
Мой сегментированный элемент управления работает нормально .. пока я не прикрепляю к нему прослушиватель целевых действий. Однако мне необходимо иметь возможность обнаруживать его события.UISegmentedControl не меняет фоновое изображение при использовании целевого действия
Мой код:
NSArray *itemArray = [NSArray arrayWithObjects: @"Following", @"Everybody", @"Nearby", nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
UIFont *font = [UIFont fontWithName:@"PatuaOne-Regular" size:12.0f];
UIColor *notChosenButtonColor = [UIColor colorWithRed:(201.0/255.0f) green:(198.0/255.0f) blue:(191.0/255.0f) alpha:1.0];
UIColor *chosenButtonColor = [UIColor colorWithRed:(235.0/255.0f) green:(218.0/255.0f) blue:(102.0/255.0f) alpha:1.0];
NSDictionary *normalAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
font, UITextAttributeFont,
notChosenButtonColor, UITextAttributeTextColor,
[UIColor clearColor], UITextAttributeTextShadowColor,
nil];
NSDictionary *selectedAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
font, UITextAttributeFont,
chosenButtonColor, UITextAttributeTextColor,
nil];
[segmentedControl setTitleTextAttributes:normalAttributes forState:UIControlStateNormal];
[segmentedControl setTitleTextAttributes:selectedAttributes forState:UIControlStateSelected];
[segmentedControl setBackgroundImage:[UIImage imageNamed:@"standard_bt.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[segmentedControl setBackgroundImage:[UIImage imageNamed:@"standard_bt_h.png"] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
segmentedControl.frame = CGRectMake(5, 20, 280, 25);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBordered;
[segmentedControl setDividerImage:[UIImage imageNamed:@"separator.png"]
forLeftSegmentState:UIControlStateNormal
rightSegmentState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[segmentedControl addTarget:self
action:@selector(segmentToggled:)
forControlEvents:UIControlEventValueChanged];
[headerView addSubview:segmentedControl];
- (void)segmentToggled:(UISegmentedControl*)sender
{
NSInteger index = sender.selectedSegmentIndex;
NSLog(@"index: %d",index);
if(index == 0){
sender.selectedSegmentIndex = 0;
[self.feedDescription removeAllObjects];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{[[WebAPI sharedInstance]getFeed:0 max:100 count:5 controller:self];});
[self.collectionView reloadData];
}else if(index == 1){
sender.selectedSegmentIndex = 1;
[self.feedDescription removeAllObjects];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{[[WebAPI sharedInstance]getFeed:0 max:100 count:5 controller:self];});
[self.collectionView reloadData];
}else if(index == 2){
sender.selectedSegmentIndex = 2;
[self.feedDescription removeAllObjects];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{[[WebAPI sharedInstance]getFeed:0 max:100 count:5 controller:self];});
[self.collectionView reloadData];
}
}
Как я могу реализовать слушателю цель действий выше, и по-прежнему обновить свой UISegmentedControls фоновые изображения в соответствии с:
[segmentedControl setBackgroundImage:[UIImage imageNamed:@"standard_bt.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[segmentedControl setBackgroundImage:[UIImage imageNamed:@"standard_bt_h.png"] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
ty, я получил *** Завершение приложения из-за неперехваченного исключения «NSInvalidArgumentException», причина: '- [UISegment setBackgr oundImage: forState: barMetrics:]: непризнанный селектор отправлен в экземпляр 0xb81af70 ' – Eyeball
проверить мой обновленный ответ –
такое же исключение im fear – Eyeball