1
-(void)dismissActionSheet:(id)sender {
[actionSheet dismissWithClickedButtonIndex:0 animated:YES];
}

-(IBAction) pressButton:(id)sender{



actionSheet = [[UIActionSheet alloc] initWithTitle:nil 
                                                         delegate:nil
                                                cancelButtonTitle:nil
                                           destructiveButtonTitle:nil
                                                otherButtonTitles:nil];

[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];

CGRect pickerFrame = CGRectMake(0, 40, 0, 0);



UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = pickerArray;
pickerView.delegate = self;

[actionSheet addSubview:pickerView];
[pickerView release];

UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"OK"]];
closeButton.momentary = YES; 
closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blackColor];
[closeButton addTarget:self action:@selector(dismissActionSheet:) forControlEvents:UIControlEventValueChanged];
[actionSheet addSubview:closeButton];

[closeButton release];

[actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];

[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];

[actionSheet release];
}

ボタンを押すとキーボードのようにポップアップするピッカーを作成しました。今、私は自分のサイトから配列を取り込もうとしています。すなわちhttp://www.mywebsite.com/app/array.txt

これは私がこれまでに持っているものです:

- (void)viewDidLoad{
NSURL *url = [NSURL URLWithString:@"http://www.tntmax.com/app/gQL420pt.txt"];
NSString *x = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
self.pickerArray = x;

[x release];

うまくいかないようです。私はさまざまな方法を試しましたが、これは最新の試みです。

私のウェブサイトのページは次のようになります。

@"object1", @"thing2", @"person3", @"stuff4", nil

テキストの形式を、コードに最適な形式に変更できます。お知らせ下さい。

これを機能させるにはどうすればよいですか?

4

2 に答える 2

0

UIPickerViewのドキュメントを読む必要があります。をデータソースとして使用することはできません。代わりに、プロトコルNSStringに準拠するオブジェクトが必要です。UIPickerViewDataSource

また、Web サイトから を読み込もうとしている場合はNSArray、データを plist としてフォーマットしてから を使用できます[NSArray arrayWithContentsOfURL:url]。これにより、UIPickerViewDataSource

// plist header here (look it up...)
<plist>
<array>
    <string>object1</string>
    <string>thing1</string>
    // more strings
</array>
</plist>
于 2012-06-28T19:13:26.607 に答える
0

これを使って:

 - (void)viewDidLoad
 {
   NSURL *url = [NSURL URLWithString:@"http://www.tntmax.com/app/gQL420pt.txt"];
   NSString *reply= [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
   NSArray *txtItems= [reply componentsSeparatedByString:@","];
   //if u want remove @"" from all values then replace items in array withReplacingocurrenceOfString with @ and "
   self.pickerArray = txtItems; // as pickerArray is nsarray if not(nsmutableArray) then use [txtItems mutableCopy];
 }
于 2012-06-28T19:30:53.320 に答える