单击按钮后如何创建UIPickerView?

时间:2011-03-19 17:49:08

标签: iphone uipickerview

单击按钮后,可以帮助澄清如何创建UIPickerView吗?请帮忙......非常感谢!

实际上,如下所示是我所做的代码,一旦我点击了选择器视图显示但没有任何数据的按钮,请找到以下代码: -

-(IBAction)buttonPressed1:(id)sender{

    CGSize pickerSize = [myLocationPickerView sizeThatFits:CGSizeZero];
    myLocationPickerView.frame = CGRectMake(0.0,250,pickerSize.width,pickerSize.height);

    [self.view addSubview:myLocationPickerView];}   

- (void)viewDidLoad {

    [super viewDidLoad];

    arrayNo = [[NSMutableArray alloc] init];
    [arrayNo addObject:@" example 1 "];
    [arrayNo addObject:@" example 2 "];
    [arrayNo addObject:@" example 3 "];
    [arrayNo addObject:@" example 4 "];
    [myLocationPickerView selectRow:0 inComponent:0 animated:NO];

}



- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)myLocationPickerView;
{
    return 1;
}


- (void)myLocationPickerView:(UIPickerView *)myLocationPickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;
{
    mlabel.text= [arrayNo objectAtIndex:row];
}



- (NSInteger)myLocationPickerView:(UIPickerView *)myLocationPickerView numberOfRowsInComponent:(NSInteger)component;
{
    return [arrayNo count];
}



- (NSString *)myLocationPickerView:(UIPickerView *)myLocationPickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
    return [arrayNo objectAtIndex:row];

}

1 个答案:

答案 0 :(得分:-1)

根据您的具体情况,有几种方法可以做到这一点。最直接的方法是在界面构建器中创建一个xib,并在单击该按钮时推送该新视图。

另一种方法是以编程方式组装视图,然后添加选择器视图。你真的需要澄清点击的按钮,你遇到困难的地方以及你已经做了多少。

编辑:我忘记了我最常使用的方法。以模态方式呈现xib:

// push modal controller
EditViewController *editController = [[EditViewController alloc] initWithNibName:@"EditView" 
                                                                          bundle:nil];
editController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
editController.delegate = self;

editController.selectedSyllableNumber = tempSyllable.nameNumber;
editController.selectedDirectionNumber = tempSyllable.directionNumber;
editController.sequenceType = tempSyllable.exercise.typeNumber;

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:editController];
[self presentModalViewController:navController
                        animated:YES];