2011-08-12 8 views
-1

私はいつも働いているXML文書を読む必要があります。しかし今、私はそのXMLから値を持つUISegmentedcontrolsを書く必要があります。解析される値は、次の形式です。UISegmentedControlsをプログラムで値で設定するにはどうすればよいですか?

<wcqAnswerValues>slecht;matig;voldoende;goed</wcqAnswerValues> 

セミコロンで区切られた4つの値です。したがって、UISegmentedControlは4つのセグメントを取得する必要があります。最初の2つのセグメントには、アクションが必要です。これら4つの値をどのように分割してUISegmentedControlに入れるのですか?

最初の2つの値が選択されている場合は、テキストフィールドが飛び出す必要があります。これどうやってするの?

static CGFloat y1 = 100.0f; 
do { 
    if ([[TBXML elementName:element] isEqualToString:@"wcqAnswerValues"]) { 
     UISegmentedControl *answer = [[UISegmentedControl alloc] initWithFrame:CGRectMake(50, y1, 250, 50)]; 

     answer.segmentedControlStyle = UISegmentedControlStyleBar; 

     [scrollView addSubview:answer]; 

     [formulierText removeFromSuperview]; 

     [answer release]; 
     y1 += 50.0f; 
    } 

答えて

2

使用componentsSeperatedByString:配列に文字列を分割する方法。そして、addTarget:action:forControlEvents:セグメント選択アクションを処理するメソッドを使用してください。

NSString *segmentItemsStr = @"slecht;matig;voldoende;goed"; 
NSArray *segmentItemsArray = [segmentsStr componentsSeperatedByString:@";"]; 
UISegmentedControl *answer = [[UISegmentedControl alloc] initWithItems:segmentItemsArray]; 
[answer addTarget:self action:@selector(someMethod:) forControlEvents:UIControlEventValueChanged]; 
... 

のsomeMethod:方法は次のようになります。

- (void)someMethod:(UISegmentedControl *)sender { 

    if (sender.selectedSegmentIndex == 0 || sender.selectedSegmentIndex == 1) { 

     // Code to pop up the text field 
    } 
} 
+0

ありがとう、それは私が必要としていたものでした! –

+0

うん!ウェルカムジャック! – EmptyStack

関連する問題