我的分段控件工作正常..只要我不为其附加目标动作侦听器。但是,我有必要能够检测到它的事件。
我的代码:
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];