Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/94.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UISegmentedControl在使用目标操作时不更改背景图像_Ios_Cocoa Touch_Uisegmentedcontrol - Fatal编程技术网

Ios UISegmentedControl在使用目标操作时不更改背景图像

Ios UISegmentedControl在使用目标操作时不更改背景图像,ios,cocoa-touch,uisegmentedcontrol,Ios,Cocoa Touch,Uisegmentedcontrol,我的分段控件工作正常。。只要我没有将目标操作侦听器附加到它。然而,我有必要能够检测到它的事件 我的代码: NSArray *itemArray = [NSArray arrayWithObjects: @"Following", @"Everybody", @"Nearby", nil]; UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray]; UIFont *fon

我的分段控件工作正常。。只要我没有将目标操作侦听器附加到它。然而,我有必要能够检测到它的事件

我的代码:

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];
请尝试以下代码:

    - (void)segmentToggled:(UISegmentedControl*)sender
            {
            for (int i=0; i<[sender.subviews count]; i++)
                {
                    if ([[sender.subviews objectAtIndex:i] respondsToSelector:@selector(isSelected)] && [[sender.subviews objectAtIndex:i]isSelected])
                    {

                     [sender setBackgroundImage:[UIImage imageNamed:@"standard_bt_h.png"] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];

                    }
                    if ([[sender.subviews objectAtIndex:i] respondsToSelector:@selector(isSelected)] && ![[sender.subviews objectAtIndex:i] isSelected])
                    {
                    [sender setBackgroundImage:[UIImage imageNamed:@"standard_bt.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];


                    }
                }
            }

Hope it Help You
-(void)分段切换:(UISegmentedControl*)发送方
{

对于(int i=0;ity,我收到***由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[UISegment setBackgroundImage:forState:barMetrics::]:未识别的选择器发送到实例0xb81af70'好了!给你5颗星抱歉,我无法将你的方法与我想要实现的结合起来。让我们一起吧