How to use UIPickerView on second ViewController - iphone

" 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];
}

Related

UiPickerView with Images Error

I have UiPickerView and colorCode.Plist - which contains names of images.
I need to add these images to my picker View.
UipickerView
4 component
10 images in first 3 component
4 images in 4th component
Programs like Resistor Colour Code for Electrical use. (Error thrown)
{
#interface ResistorsColorView ()
{
NSArray *colorCode;
}
#end
#implementation ResistorsColorView
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"colorCode" ofType:#"plist"];
colorCode = [[NSArray alloc] initWithContentsOfFile:filePath];
}
# pragma mark - Datasource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return colorCode.count;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return [colorCode[component]count];
}
//- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent: (NSInteger)component{
//
// return colorCode[component][row];
//}
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent: (NSInteger)component reusingView:(UIView *)view{
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];
imageView.image = colorCode[component][row];
imageView.image = [UIImage imageNamed:[record objectForKey:#"row1"]];
[v addSubview:imageView];
return v;
}
#end
reason: '-[__NSCFString _isResizable]: unrecognized selector sent to instance 0x6e6e300' –

Get value from detailTextLabel after PickerInputViewCell

I have a Custom cell that shows a pickerView when you select a row.
Then you change the value and put it on detailTextLabel.
But this method get the value before it changes.
//Custom Cell
SimplePickerInputTableViewCell *cell = (SimplePickerInputTableViewCell *) [_tableView dequeueReusableCellWithIdentifier:cellIndentifier2];
if (!cell) {
cell = [[SimplePickerInputTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIndentifier2];
UIImageView *lineView2 = [[UIImageView alloc] initWithFrame:CGRectMake(-2, 45, 320, 10)];
lineView2.image = [UIImage imageNamed:#"linha_horizontal.png"];
[cell addSubview:lineView2];
_opcaoTitle = [[UILabel alloc] initWithFrame:CGRectMake(10, 15, 200, 20)];
_opcaoTitle.textAlignment = UITextAlignmentLeft;
_opcaoTitle.textColor = [UIColor blackColor];
_opcaoTitle.text = #"Pista - Feminino";
[cell addSubview:_opcaoTitle];
_opcaoValueF = [[UILabel alloc] initWithFrame:CGRectMake(180, 15, 100, 20)];
_opcaoValueF.textColor = [UIColor blackColor];
[cell addSubview:_opcaoValueF];
}
_opcaoValueF.text = [NSString stringWithFormat:#"R$ %#,00", _eventFemininoValue];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
[cell setValue:#"0"];
return cell;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[_tableView deselectRowAtIndexPath:indexPath animated:YES];
NSString *s = [[[tableView cellForRowAtIndexPath:indexPath] detailTextLabel] text];
NSLog(#"String is: %#", s);
int i = [s intValue];
NSLog(#"This is the int value: %d", i);
}
SimplePicker.m
#import "SimplePickerInputTableViewCell.h"
#implementation SimplePickerInputTableViewCell
#synthesize delegate;
#synthesize value;
__strong NSArray *values = nil;
+ (void)initialize {
values = [NSArray arrayWithObjects:#"0", #"1", #"2", #"3", #"4", #"5", #"6", #"7", #"8", #"9", #"10", nil];
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
self.picker.delegate = self;
self.picker.dataSource = self;
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
// Initialization code
self.picker.delegate = self;
self.picker.dataSource = self;
}
return self;
}
- (void)setValue:(NSString *)v {
value = v;
self.detailTextLabel.text = value;
self.detailTextLabel.textColor = [UIColor whiteColor];
self.detailTextLabel.backgroundColor = [UIColor clearColor];
if (![value isEqualToString:#"0"]) {
[self.picker selectRow:[values indexOfObject:value] inComponent:0 animated:YES];
}
_backgroundValue = [[UIImageView alloc] initWithFrame:CGRectMake(285, 3, 40, 40)];
_backgroundValue.image = [UIImage imageNamed:#"home_icon_popular.png"];
[self addSubview:_backgroundValue];
[self sendSubviewToBack:_backgroundValue];
}
#pragma mark -
#pragma mark UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return [values count];
}
#pragma mark -
#pragma mark UIPickerViewDelegate
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [values objectAtIndex:row];
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
return 44.0f;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
return 300.0f; //pickerView.bounds.size.width - 20.0f;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
self.value = [values objectAtIndex:row];
if (delegate && [delegate respondsToSelector:#selector(tableViewCell:didEndEditingWithValue:)]) {
[delegate tableViewCell:self didEndEditingWithValue:self.value];
}
}
The problem is that you are getting the value as soon as the new cell is created (the cell is probably not even drawn on the screen at this time). To solve this you should capture the value when the use dismisses the picker view.
From the additional code you provided
1) Set up a delegate for the table view cell:
cell.delegate = self;
You can do this right after you create it
2) Add an implementation of tableViewCell:didEndEditingWithValue:
- (void)tableViewCell:(UITableViewCell*)cell didEndEditingWithValue:(NSValue*)value
{
NSLog(#"Value is %#", value);
}

How do I open one of the many View controllers on selection of a UIPickerPicker view

I am creating an app in IOS5 using storyboards. I created a UiPickerView and when an option is selected I want to open one of the many UIViewControllers. I just want the user to select which view controller he wants to use. How do I go about connecting multiple view controllers.
Thanks
Prerna
Please use the following delegates and data sources for your picker view
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
if(button_Number == 1)
{
UILabel *label =[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 280, 30)];
NSString *nameStr=[arr_countryName objectAtIndex:row];
// NSString *nameStr=[programNameArr objectForKey:#"programname"];
label.text = nameStr;
label.font = [UIFont boldSystemFontOfSize:14.0f];
label.textAlignment = UITextAlignmentLeft;
label.backgroundColor = [UIColor clearColor];
[label autorelease];
return label;
}
if (button_Number == 2)
{
UILabel *label =[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 280, 30)];
NSString *nameStr=[arr_currencyCode objectAtIndex:row];
// NSString *nameStr=[programNameArr objectForKey:#"programname"];
label.text = nameStr;
label.font = [UIFont boldSystemFontOfSize:18.0f];
label.textAlignment = UITextAlignmentLeft;
label.backgroundColor = [UIColor clearColor];
[label autorelease];
return label;
}
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
//return (NSString*)[clientListArray objectAtIndex:row];
if(button_Number == 1)
{
return (NSString*)[arr_countryName objectAtIndex:row];
}
if (button_Number == 2)
{
return (NSString*)[arr_currencyCode objectAtIndex:row];
}
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
if(button_Number == 1)
{
return [arr_countryName count];
}
if (button_Number == 2)
{
return [arr_currencyCode count];
}
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
selectedScrollIndex = row;
// clientNameTxtFld.text = [clientListArray objectAtIndex:row];
// LBL.text = [clientListArray objectAtIndex:row];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 0)
{
}
if (buttonIndex == 1 && button_Number == 1)
{
countryTxtFld.text = [arr_countryName objectAtIndex:selectedScrollIndex];
//selected_Client_ID = [clientIDArray objectAtIndex:selectedScrollIndex];
selectedScrollIndex = 0;
}
if (buttonIndex == 1 && button_Number == 2)
{
currencyTxtFld.text = [arr_currencyCode objectAtIndex:selectedScrollIndex];
//selected_Client_ID = [clientIDArray objectAtIndex:selectedScrollIndex];
selectedScrollIndex = 0;
}
}
Create instance for all the view controllers in .h file and set the tag value for all of them as per the sequence of row-touch-title array in UIPickerView.
Once this is done, inherit UIPickerViewDelegate and implement required delegate method.
- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row
inComponent:(NSInteger)component {
// Handle the selection
switch (row) {
case 0:
[self presentModalViewController:viewController1 animated:YES];
break;
case 1:
[self presentModalViewController:viewController2 animated:YES];
break;
case 2:
[self presentModalViewController:viewController3 animated:YES];
break;
}
}

How to connect UIPickerView datasource and delegate in storyboard

I have created a new project in Ios5 using storyboards and my project has a UIPickerView.
I created a datasource and delegate and I cannot find the files owner to make connections in IOs5 . What should I make the connections to?
One difference between storyboards and loading .xib files is that the view controller for each "scene" in a storyboard file is included in that file. So, assuming that your view controller is also the data source and delegate for your picker (this is common), connect the view controller to those outlets instead of the file's owner proxy. (With .xib files, it's typical to instantiate the view controller in code and give it a .xib file from which to load its view, so the view controller is the file's owner.)
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
if(button_Number == 1)
{
UILabel *label =[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 280, 30)];
NSString *nameStr=[arr_countryName objectAtIndex:row];
// NSString *nameStr=[programNameArr objectForKey:#"programname"];
label.text = nameStr;
label.font = [UIFont boldSystemFontOfSize:14.0f];
label.textAlignment = UITextAlignmentLeft;
label.backgroundColor = [UIColor clearColor];
[label autorelease];
return label;
}
if (button_Number == 2)
{
UILabel *label =[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 280, 30)];
NSString *nameStr=[arr_currencyCode objectAtIndex:row];
// NSString *nameStr=[programNameArr objectForKey:#"programname"];
label.text = nameStr;
label.font = [UIFont boldSystemFontOfSize:18.0f];
label.textAlignment = UITextAlignmentLeft;
label.backgroundColor = [UIColor clearColor];
[label autorelease];
return label;
}
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
//return (NSString*)[clientListArray objectAtIndex:row];
if(button_Number == 1)
{
return (NSString*)[arr_countryName objectAtIndex:row];
}
if (button_Number == 2)
{
return (NSString*)[arr_currencyCode objectAtIndex:row];
}
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
if(button_Number == 1)
{
return [arr_countryName count];
}
if (button_Number == 2)
{
return [arr_currencyCode count];
}
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
selectedScrollIndex = row;
// clientNameTxtFld.text = [clientListArray objectAtIndex:row];
// LBL.text = [clientListArray objectAtIndex:row];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 0)
{
}
if (buttonIndex == 1 && button_Number == 1)
{
countryTxtFld.text = [arr_countryName objectAtIndex:selectedScrollIndex];
//selected_Client_ID = [clientIDArray objectAtIndex:selectedScrollIndex];
selectedScrollIndex = 0;
}
if (buttonIndex == 1 && button_Number == 2)
{
currencyTxtFld.text = [arr_currencyCode objectAtIndex:selectedScrollIndex];
//selected_Client_ID = [clientIDArray objectAtIndex:selectedScrollIndex];
selectedScrollIndex = 0;
}
}

How to give this picker view a datasource

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.