我正在尝试以UISegmentedControl
编程方式创建一个。我UIViewController
在情节提要中有一个,里面什么都没有。
.h 文件
UISegmentedControl *segmentedControl;
NSString *feedBackButtonTitle;
NSString *contactsButtonTitle;
这是属性声明。
@property (nonatomic,retain) IBOutlet UISegmentedControl *segmentedControl;
-(void) segmentedControlIndexChanged;
在viewDidLoad:
我已经初始化并添加了UISegmentedControl
.
NSString *language = [[NSLocale preferredLanguages] objectAtIndex:0];
if ([language isEqualToString:@"en"]){
contactsButtonTitle = [[[configFileDictionary objectForKey:@"Contacts"] objectForKey:@"Label"] objectForKey:@"en"];
feedBackButtonTitle = [[[[[configFileDictionary objectForKey:@"Contacts"] objectForKey:@"Contact"]objectForKey:@"Feedback"]objectForKey:@"Label"]objectForKey:@"en"];
}
else if([language isEqualToString:@"fr"]){
contactsButtonTitle = [[[configFileDictionary objectForKey:@"Contacts"] objectForKey:@"Label"] objectForKey:@"fr"];
feedBackButtonTitle = [[[[[configFileDictionary objectForKey:@"Contacts"] objectForKey:@"Contact"]objectForKey:@"Feedback"]objectForKey:@"Label"]objectForKey:@"fr"];
}
NSArray *itemsArray = [[NSArray alloc] initWithObjects:contactsButtonTitle, feedBackButtonTitle, nil];
segmentedControl = [[UISegmentedControl alloc] initWithItems:itemsArray];
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
//segmentedControl.selectedSegmentIndex = 0;
segmentedControl.frame = CGRectMake(0.0f, 0.0f, 320.0f,40.0f);
[segmentedControl addTarget:self action:@selector(segmentedControlIndexChanged) forControlEvents:UIControlEventValueChanged];
// [self.view addSubview:segmentedControl];
// Create view for contact display.
[self createViews];
而这个(void)segmentedControlIndexChanged
-(void)segmentedControlIndexChanged
{
switch (self.segmentedControl.selectedSegmentIndex)
{
case 0:
[self createViews];
break;
case 1:
[self showFeedbackForm];
break;
default:
break;
}
}
这在屏幕上显示分段控件就好了,但是当我单击分段控件中的选项时,它总是进入选项案例 0。分段控件并打开[self createViews];
在 line 插入断点时case 0
,我注意到 segmentedControl 中的选项 _selectedSegment 为 1。这没有任何意义。