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
檢查我更新的答案 –
同樣的異常恐怕 – Eyeball