1

我希望我的应用程序具有可以在用户按下 UISegmented 控件中的段时更改图像的设置,但是,我只知道在导航栏中显示它。我将如何以一种设置特定视图的方式实现它,当用户按下一个段时,当按下完成时,它将恢复为具有该背景图像的新视图。

4

3 回答 3

1

试试这行代码。

 NSArray *ary=[sgmntControl subviews];
 NSInteger intCount=0;
 for (id seg in ary) 
  for (id imageview in [seg subviews]) 
 if ([imageview isKindOfClass:[UIImageView class]])
  {
   if(intCount==1)
    {
    [imageview setimage:[UIImage imagenamed:@""]; 

    }
    else 
    {
   [imageview setimage:[UIImage imagenamed:@""]; 

    }


   }
于 2012-05-07T05:04:46.253 回答
0

您可以像这样更改分段控件的图像 UISegmentedControl *segment;

[segment setImage:UIImage  forSegmentAtIndex:segmentIndex];

或者

[segment setBackgroundImage:UIImage  forState:ControlState barMetrics:UIBarMetrics];

编辑:

您可以尝试将值传递给 newViewController

- (IBAction)didChangeSegmentControl:(UISegmentedControl *)control {
   newViewController = [[NewViewController alloc] initWithNibName:@"NewViewController" bundle:nil];
newViewController. selectedIndex =control.selectedSegmentIndex;
[self.navigationController pushViewController: newViewController animated:NO];    
}

在此之前,您必须在 newViewController 中设置一个属性

@interface NewViewController : UIViewController
{
NSInteger selectedIndex;
}

@property(nonatomic)NSInteger selectedIndex;

@end

并在实施中

@implementation NewViewController

@synthesize selectedIndex;
//your codes
@end
于 2012-05-07T04:45:00.817 回答
0
 One of the solution is you can add imageView under UISegmented controller with same size , set alpha for UISegmented 0.01 and change the imageview image in UISegmented action method.like below

self.imageViewSegment.image = [UIImage imageNamed:[NSString stringWithFormat:@"segment%d.png",self.segmentedControl.selectedSegmentIndex]];
//here your image names like segment0.png,segment1.png,segment2.png..etc
于 2012-05-07T05:10:10.837 回答