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 文档。您不能使用 anNSString作为数据源,而是需要一个符合UIPickerViewDataSource协议的对象。

此外,如果您尝试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 回答