我正在尝试以编程方式创建UISegmentedControl 我在情节UIViewController中有一个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。这没有任何意义。

===============>>#1 票数: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文件中总结出一个UISegmentedControl是多余的-它只是另一个UISegmentedControl 您的属性可以用点符号self.segmentedControl 或通过综合名称(默认设置) _segmentedControl

因此,您正在做的是在初始化和添加UISegmentedController时调用此.h对象,但是在调用segmentedControlIndexChanged时-您在甚至不可见的属性中对其进行了更改。

===============>>#2 票数: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;
    }
}

  ask by Ashish Agarwal translate from so

未解决问题?本站智能推荐: