我正在尝试以编程方式创建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;
}
}
这显示屏幕上的分段控件很好,但是当我点击segmentedcontrol中的选项时,它总是进入选项案例0.分段控件并打开[self createViews];
在第case 0
行插入断点时,我注意到segmentedControl中的选项_selectedSegment为1.这没有任何意义。
答案 0 :(得分:3)
好的,所以我在我的UIViewController
创建了一个属性segmentedControl
,然后只写了那段代码:
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *itemsArray = [[NSArray alloc] initWithObjects:@"a", @"b", nil];
self.segmentedControl = [[UISegmentedControl alloc] initWithItems:itemsArray];
self.segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
self.segmentedControl.selectedSegmentIndex = 0;
self.segmentedControl.frame = CGRectMake(0.0f, 0.0f, 320.0f,40.0f);
[self.segmentedControl addTarget:self action:@selector(segmentedControlIndexChanged) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:self.segmentedControl];
}
-(void)segmentedControlIndexChanged
{
switch (self.segmentedControl.selectedSegmentIndex)
{
case 0:
NSLog(@"createviews");
break;
case 1:
NSLog(@"showfeedbackform");
break;
default:
break;
}
}
它非常适合我 - 记录正确的选择。
如你所说_segmentedControl
没问题 - 请注意我使用点符号来获取segmentedControl。这种情况可能就是这种情况,因为当你不使用点符号时 - 你没有调用getter。
修改强>
总结一点 - 来自.h文件的segmentedControl是多余的 - 它只是另一个UISegmentedControl
。您的属性可以用点符号引用 - self.segmentedControl
。或者通过合成名称(默认情况下制作)_segmentedControl
。
所以你正在做的是在初始化和添加你的UISegmentedController
时引用这个.h对象,但是当调用segmentedControlIndexChanged
时 - 你在你的属性中更改它甚至不可见。
答案 1 :(得分:1)
这里有一些事情:
self.segmentedControl
代替segmentedControl
[self.segmentedControl addTarget:self
action:@selector(segmentedControlIndexChanged:)
forControlEvents:UIControlEventValueChanged]; // Notice the colon
-(void)segmentedControlIndexChanged:(id)sender
{
switch ([sender selectedSegmentIndex])
{
case 0:
[self createViews];
break;
case 1:
[self showFeedbackForm];
break;
default:
break;
}
}