ios5以编程方式设置setSelectedSegmentIndex不起作用

时间:2012-03-01 02:41:05

标签: iphone ipad ios5 uisegmentedcontrol

我看过UISegmentedControl change event not firing in iOS5 双重检查,三重检查,仍然无法弄明白

.h

@interface HomeViewController : UIViewController <PSStackedViewDelegate, UITableViewDelegate, UITableViewDataSource> {
    UISegmentedControl *segmentedControl;    
}

@property (nonatomic, retain) UISegmentedControl *segmentedControl;
-(IBAction) segmentedControlIndexChanged: (id) sender;


.m
@synthesize segmentedControl;

在viewDidLoad

UIView *containerHeaderView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 60)] autorelease];

NSArray *itemArray = [NSArray arrayWithObjects: @"Favoutites", @"All stories", nil];
segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];


segmentedControl.frame = CGRectMake( 85.0, 10.0, 300.0, 30.0);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
[segmentedControl setTintColor: [UIColor colorWithRed:0.80 green:0.80 blue:0.80 alpha:1.0]];

[segmentedControl addTarget:self action:@selector(segmentedControlIndexChanged:) forControlEvents:UIControlEventValueChanged];

segmentedControl.selectedSegmentIndex = 0;

[containerHeaderView addSubview:segmentedControl];

// Add header to table
_tableView.tableHeaderView = containerHeaderView;

然后是同一个.m

中的一个
    // select the first segment
    NSLog(@"Switching to News %d",segmentedControl.selectedSegmentIndex);
    // iOS4 style
    //self.segmentedControl.selectedSegmentIndex = UISegmentedControlNoSegment;
    self.segmentedControl.selectedSegmentIndex = 1;
    segmentedControl.selectedSegmentIndex = 1;

    //iOS5 style

    [self.segmentedControl setSelectedSegmentIndex:1];
    [segmentedControl setSelectedSegmentIndex:1];
    [self.segmentedControl sendActionsForControlEvents:UIControlEventValueChanged];

    NSLog(@"After Switch %d",segmentedControl.selectedSegmentIndex);

打印出来......

Switching to News 0
After Switch 0

......段不会改变颜色,索引不会改变。

没有xib

单击uisegmentcontrol工作正常,但不会以编程方式更改。虽然在创建索引时设置索引确实有效。

为什么不会setSelectedSegmentIndex ??????

TIA

更新:

似乎问题在于使用PSStackedViewDelegate导致视图被创建两次....

0 个答案:

没有答案