0

我知道很多人都问过同样的问题,我已经尝试了很多,我不知道我错过了什么,我仍然可以让它工作。

我有一个 9*9 的表格,我想显示和更改,因为 Iphone 屏幕的大小,我想一次显示一列。我想要的是按下左上角的按钮,UIPickerView 将弹出允许我选择要显示的列,然后重新加载 UITextFields。

谁能给我一个详细的答案?在这(几个月)中我还是比较新的。先感谢您。

9*9桌

UIActionSheet *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 = self;
pickerView.delegate = self;

[actionSheet addSubview:pickerView];


UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray     arrayWithObject:@"Close"]];
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)];

上面是我找到的代码,它弹出好了,我在将数组添加到pickerview时遇到问题,谁能告诉我dismissActionSheet方法应该如何?谢谢你。

4

1 回答 1

0

好的,所以我想我明白你在问什么,如果这不是你需要帮助的,请纠正我......但是要将一个数组(可能是 NSStrings)添加到你需要使用- (NSString *)pickerView:titleForRow:forComponent:委托方法的选择器视图中。这是一个简单的例子:

//Say you have an array of strings you want to present in the pickerview like this
NSArray *arrayOfStrings = [NSArray arrayWithObjects:@"One", @"Two", @"Three", @"Four", nil];

//First we need to say how many rows there will be in the pickerview
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return [arrayOfStrings count];
}

//Do the same for components (columns) in pickerview
    - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}
//Here we set the actual title/string on the pickerview
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if(component==0){
        //Grab the nth string from array
        return [arrayOfStrings objectAtIndex:row];
    }
}
//This is called if a user taps a row
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if(component==0)
    NSLog(@"The User Selected the row titled %@", [arrayOfStrings objectAtIndex:row]);
}

与操作表类似,您需要添加委托方法来处理用户单击操作表上的按钮

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if(buttonIndex==0){
        NSLog(@"First Button Clicked");
    } else if( buttonIndex == 1){
        NSLog(@"Second Button Clicked");
    }
}
于 2012-10-30T17:22:32.017 回答