如何在第二个ViewController上使用UIPickerView

时间:2012-11-23 06:47:48

标签: iphone uipickerview

“我正在为iPhone创建一个应用程序,我必须在其中创建一个登录页面。在从服务器获取响应之后..将导航用户到下一个视图。在下一个视图中,我使用UIPickerView。但它是不工作.. becoz我只知道如何在个人应用程序中使用它。但我不知道如何在应用程序的第二个ViewController中使用它?“

2 个答案:

答案 0 :(得分:1)

您可以使用此

获取Picker
-(void)getValuePicker
{
    ViewForValuePicker = [[UIView alloc]initWithFrame:CGRectMake(0, 219, 320, 266)];

    UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
    toolBar.barStyle = UIBarStyleBlackOpaque;

    UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneBtnPressToGetValue)];

    [toolBar setItems:[NSArray arrayWithObject:btn]];
    [ViewForValuePicker addSubview:toolBar];


    valuePicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, 320, 216)];
    valuePicker.delegate=self;
    valuePicker.dataSource=self;
    valuePicker.showsSelectionIndicator=YES;

    [ViewForValuePicker addSubview:valuePicker];

    [appDelegate.window addSubview:ViewForValuePicker];
}  

及其代表方法

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

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

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
    NSMutableArray *ary = [[NSMutableArray alloc] initWithArray:pickerValueAry];
    id str=[ary objectAtIndex:row];
    return str;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{    
    NSLog(@"selectedRowInPicker >> %d",row);
}

答案 1 :(得分:0)

in .h file  this method

 UIViewController<UIPickerViewDataSource,UIPickerViewDelegate>

{

    IBOutlet UIPickerView *picker;
     NSMutableArray *pickerArrayType;
}

//in .m file

- (void)viewDidLoad
{
    [super viewDidLoad];
pickerArrayType = [[NSMutableArray alloc] initWithObjects:@"Type 1",@"Type 2",@"Type 3",@"Type 4",@"Type 5", nil]
}


# pragma mark - picker Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
    return 1;
}

- (void)pickerView:(UIPickerView *)pickerView1 didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{


 lbl.text = [pickerArrayType objectAtIndex:row];

}

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

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
    return [pickerArrayType objectAtIndex:row];
}