0

我正在尝试以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。这没有任何意义。

4

2 回答 2

3

好的,所以我在我UIViewController的 a 属性中创建了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没关系 - 请注意我使用点符号来获取分段控制。这可能是这种情况,因为当您不使用点表示法时 - 您没有调用 getter。

编辑

总结一下 .h 文件中的一点-segmentedControl 是多余的-它只是另一个 .h 文件UISegmentedControl。您的属性可以通过点符号引用 - self.segmentedControl。或通过合成名称(默认生成)_segmentedControl

所以你正在做的是在初始化和添加你的UISegmentedControllersegmentedControlIndexChanged被调用时引用这个 .h 对象 - 你在你的属性中更改它甚至不可见。

于 2013-04-17T21:42:34.497 回答
1

好的,这里有几件事:

  1. 既然你有一个属性,使用self.segmentedControl而不是segmentedControl
  2. 选择器签名的简单更改会变得更容易:

[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;
    }
}
于 2013-04-17T22:03:07.240 回答