问题
" I am creating a application for IPhone in which I have to creat a login page. after getting the response from the server..it will navigate the user to next view. in next view, i m using a UIPickerView. but it is not working.. becoz i only know that how to use it in individual app . but i dont knw how to use it in second ViewController of the application??"
回答1:
You can getPicker using this
-(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];
}
And its Delegete Method
-(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);
}
回答2:
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];
}
来源:https://stackoverflow.com/questions/13524294/how-to-use-uipickerview-on-second-viewcontroller