Here I am adding a pickerview programaticly
- (void)viewDidLoad {
[super viewDidLoad];
CGRect pickerFrame = CGRectMake(0,280,321,200);
UIPickerView *myPickerView = [[UIPickerView alloc] init]; //Not sure if this is the proper allocation/initialization procedure
//Set up the picker view as you need it
//Set up the display frame
myPickerView.frame = pickerFrame; //I recommend using IB just to get the proper width/height dimensions
//Add the picker to the view
[self.view addSubview:myPickerView];
}
But now I need to actually have it display content and somehow find out when it changes and what value it has changed to. How do I do this?
in .h file place this code
#interface RootVC : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>
assign the datasource and delegate to the picker
// this view controller is the data source and delegate
myPickerView.delegate = self;
myPickerView.dataSource = self;
use the following delegate and datasouce methods
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
return 200;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
return 50;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString *returnStr = #"";
if (pickerView == myPickerView)
{
returnStr = [[levelPickerViewArray objectAtIndex:row] objectForKey:#"nodeContent"];
}
return returnStr;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (pickerView == myPickerView)
{
return [levelPickerViewArray count];
}
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
You need to create a class that implements the UIPickerViewDataSource protocol and assign an instance of it to myPickerView.dataSource.
Related
" 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??"
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);
}
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];
}
I Learned how to use Picker views, singles and doubles. I would like to learn how to do a Picker view with images. Instead of numbers or words the scroll have different images to select one. Thanks. I think is using this code but it osent run me, I cant find the mistake. Thanks for your help.
#synthesize picker, Array, image;
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component;
{
return [self.Array count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [self.Array objectAtIndex:row];
}
- (UIView *) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent: (NSInteger)component reusingView:(UIView *)view
{
NSString *img_src = [NSString stringWithFormat:#"6-12AM.png", row];
UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:img_src]];
return image;
}
EDIT: I confused the function you need to implement in my initial answer. Sorry for that.
You need to implement the
- (UIView *) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
method of your UIPickerView delegate to return an UIImageView for a given row.
Reference page here.
for example (very dumb implementation without reusing views)
- (UIView *) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
NSString * img_src = [NSString stringWithFormat:#"image_%d.png", row];
UIImageView * retval = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:img_src]] autorelease];
return retval;
}
assuming you have images named image_[num].png in your resources.
I have got UIPickerview in my application working fine.
I want to change the default selection behavior of the UIPickerview, i mean i have a done button in the UIToolbar. The toolbar is added to the UIPickerview.
Whenever the user taps on the "Done" button, I want to consider the highlighted entry as selected.
That means user can move up and down , when the user taps on the "Done" button, i want to consider the selection now
Please let me know , how can I fetch the currently selected row for the UIPickerview when tapped "Done" button
Note: I have 3 pickers in my view ( i mean i want to have pickerview tag and pickerview selected row)
//Suppose you have created three picker views as firstPickerView, secondPickerView and ThirdPickerView
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if(pickerView == firstPickerView)
{
NSString *strFirstPickerView = [yourarray objectAtIndex:row];
}
else if(pickerView == secondPickerView)
{
NSString *strSecondPickerView = [yourarray objectAtIndex:row];
}
else
{
NSString *strThirdPickerView = [yourarray objectAtIndex:row];
}
}
selectedRowInComponent:
- Returns the index of the selected row in a given component.
- (NSInteger)selectedRowInComponent:(NSInteger)component
Parameters
component
A zero-indexed number identifying a component of the picker view.
Return Value
A zero-indexed number identifying the selected row, or -1 if no row is selected.
For Help
And to make a difference between different pickerViews add tag to each of the pickers:
self.pikcerView.tag = 1;
Hope this helps.
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSString *str = [yourarray objectAtIndex:row];
}
now str have a selected value. so you can use it when done button press.
Following example for the UIPickerview controller
.m files here
#import "SingleComponentPickerViewController.h"
#implementation SingleComponentPickerViewController
#synthesize singlePicker;
#synthesize pickerData;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
-(IBAction)buttonPressed1
{
NSInteger row = [singlePicker selectedRowInComponent:0];
NSString *selected = [pickerData objectAtIndex:row];
NSString *title = [[NSString alloc] initWithFormat:
#"you selected %#!", selected];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message : #"Thank you for choosing."
delegate:nil
cancelButtonTitle :#"Welcome"
otherButtonTitles :nil];
[alert show];
[alert release];
[title release];
}
- (void)viewDidLoad {
NSArray *array = [[NSArray alloc] initWithObjects:#"iPhone",#"iPad",#"iPod",#"iMac",#"Mac",
#"iBook",#"Safari",nil];
self.pickerData = array;
[array release];
[super viewDidLoad];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
}
- (void)dealloc {
[singlePicker release];
[pickerData release];
[super dealloc];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
return [pickerData count];
}
-(NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component
{
return[pickerData objectAtIndex:row];
}
#end
and .h file here
#import <UIKit/UIKit.h>
#interface SingleComponentPickerViewController : UIViewController {
IBOutlet UIPickerView *singlePicker;
NSArray *pickerData;
}
#property(nonatomic , retain) UIPickerView *singlePicker;
#property(nonatomic , retain) NSArray *pickerData;
-(IBAction)buttonPressed1;
#end
happy coding
#samuel
Have a UIPicker which should display two components (columns in the picker view), but is only displaying one.
Can't find the error; it builds correctly. Second component is empty; no data displayed.
InstaEmailViewController.h
#import <UIKit/UIKit.h>
#interface InstaEmailViewController : UIViewController
<UIPickerViewDataSource, UIPickerViewDelegate> {
NSArray* activities_ ;
NSArray* feelings_ ;
}
InstaEmailViewController.m
#import "InstaEmailViewController.h"
#implementation InstaEmailViewController
- (void)dealloc
{
[activities_ release];
[feelings_ release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
activities_ = [[NSArray alloc] initWithObjects:#"sleeping", #"working", #"thinking", #"crying", #"begging", #"leaving", #"shopping", #"hello worlding", nil];
feelings_ = [[NSArray alloc] initWithObjects: #"awesome", #"sad", #"ambivalent", #"nauseous",#"psyched", #"confused", #"hopeful", #"anxious", nil];
[super viewDidLoad];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (component == 0) {
return [activities_ objectAtIndex:row];
} else {
[feelings_ objectAtIndex:row];
}
return nil;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component {
if (component ==0) {
return [activities_ count];
} else {
return [feelings_ count];
}
}
#end
You are not returning the value in the pickerView:titleForRow:inComponent: method for the second component.
if (component == 0) {
return [activities_ objectAtIndex:row];
} else {
[feelings_ objectAtIndex:row];
}
You should do
return [feelings_ objectAtIndex:row];
I've searched here, but didn't see nothing like this error.
I'm trying to populate my pickerview on ViewDidLoad, but when I run my program, the pickerView is just populated with "?" instead my strings... what's going on here? Thanks in advance.
- (void)viewDidLoad {
[super viewDidLoad];
activities = [[NSArray alloc] initWithObjects:#"sleeping", #"eating", #"working", #"thinking", #"crying", #"begging", #"leaving", #"shopping", #"hello worlding", nil];
feelings = [[NSArray alloc] initWithObjects:#"awsome",#"sad",#"happy",#"ambivalent",#"nauseous",#"psyched",#"confused",#"hopeful",#"anxious",nil];
}
You need to implement the UIPickerViewDelegate and UIPickerViewDataSource protocols:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component == 0)
return [activities count];
else
return [feelings count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (component == 0)
return [activities objectAtIndex:row];
else
return [feelings objectAtIndex:row];
}