Hello guys thanks for reading this question. So i have been stuck on this issue for a while now and need a bit of help. I am attempting to implement multiple pickers in IOS but they do not come out as planned. I successfully done one picker by itself but attempting to add another one in the same view i have ran into problems.
So at first i thought it was a problem to do with this line of code. But when using the return 1: it would just return the same ui picker twice. And return 2 returns the same issue as above but with two rows.
-(NSInteger)numberOfComponentsInPickerView: (UIPickerView *)pickerView {
/*
return 1;
*/
if(pickerView == _picker) {
return _colourSourceArray.count;
} else if(_picker == _mustachepicker) {
return _mustacheArray.count;
}
}
Both of my datasources are linked to the same view, would this be causing the problem?
What my .h looks like:
////// Frame Colour Picker
#property (strong, nonatomic)NSArray *colourSourceArray;
#property (strong, nonatomic)NSString *selectedcolour;
#property (weak, nonatomic) IBOutlet UIPickerView *picker;
////// Mustahce Picker
#property (weak, nonatomic) IBOutlet UIPickerView *mustachepicker;
#property (weak, nonatomic) NSArray *mustacheArray;
#property (weak,nonatomic) NSString *selectedMostache;
/////////////////////////
And my .m methods:
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
NSInteger comp = 0;
if(_picker == _mustachepicker){
comp= _mustacheArray.count;
} else if(pickerView == _picker) {
comp= _colourSourceArray.count;
}
return comp;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSString *title;
if(_picker == _mustachepicker){
title= _mustacheArray[row];
} else if(pickerView == _picker){
title = _colourSourceArray[row];
}
return title;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if(_picker == _mustachepicker){
NSString *mustacheString= _mustacheArray[row];
NSLog(mustacheString);
_selectedMostache=mustacheString;
} else if(pickerView == _picker){
NSString *resultString = _colourSourceArray[row];
_selectedcolour= resultString;
NSLog(resultString);
}
NSLog(_selectedcolour);
NSLog(_selectedMostache);
}
Any help would be appreciated :)
As you have created two different pickers, then why are you setting the tag to the pickers just compare the UIPicker in there DataSource and Delegate methods like this
-(NSInteger)numberOfComponentsInPickerView: (UIPickerView *)pickerView {
if(pickerView == _picker) {
return _colourSourceArray.count;
} else if(_picker == _mustachepicker) {
return _mostacheArray.count;
}
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
NSInteger comp = 0;
if(_picker == _mustachepicker){
comp= _mostacheArray.count;
} else if(pickerView == _picker) {
comp= _colourSourceArray.count;
}
return comp;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow(NSInteger)row forComponent:(NSInteger)component {
NSString *title;
if(_picker == _mustachepicker){
title= _mostacheArray[row];
} else if(pickerView == _picker){
title = _colourSourceArray[row];
}
return title;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if(_picker == _mustachepicker){
NSString *mostacheString= _mostacheArray[row];
NSLog(mostacheString);
_selectedMostache=mostacheString;
} else if(pickerView == _picker){
NSString *resultString = _colourSourceArray[row];
_selectedcolour= resultString;
NSLog(resultString);
}
NSLog(_selectedcolour);
NSLog(_selectedMostache);
}
Related
I'm new to iOS programming.
Recently, I'm trying to make a UIPickerView as an inputView in UITextfield.
The data in UIPickerView is about all the iOS built-in fonts.
So I want to make two components in UIPickerView: the first is familyType, and the second is all the fonts in that familyType.
I simulate the code from this answer, but I meet some problem I can't solve. Any help is welcome!
My question is here:
Why rowOneSelected in this function always get 0 first, even I use selectedRowInComponent in advance?
// The number of rows of data
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if(component == 0)
{
return _fontTypeArray.count;
}
else
{
NSInteger rowOneSelected = [_pickerFont selectedRowInComponent:0];
FontType *temp = _fontTypeArray[rowOneSelected];
NSLog(#"%ld", (long)rowOneSelected); // I use this to debug, and there is a main question: why every time it logs 0 first?
return temp.font.count;
}
}
All my relative code is here:
In ViewController.h:
#import <UIKit/UIKit.h>
#import "MenuLayerTwoPlusThree.h"
#interface ViewController : UIViewController
#property MenuLayerTwoPlusThree *layerTwoPlusThree;
- (void)createLayerTwoPlusThree;
#end
In ViewController.m:
- (void)viewDidLoad {
[super viewDidLoad];
[self createLayerTwoPlusThree];
}
- (void)createLayerTwoPlusThree
{
_layerTwoPlusThree = [MenuLayerTwoPlusThree alloc];
[_layerTwoPlusThree createFontArray];
[_layerTwoPlusThree createSelectPanel];
}
In FontType.h:
#ifndef FontType_h
#define FontType_h
#import <Foundation/Foundation.h>
#interface FontType : NSObject
#property NSString *familyName;
#property NSMutableArray *font;
#end
#endif /* FontType_h */
In FontType.m:
#import <Foundation/Foundation.h>
#import "FontType.h"
#implementation FontType
#synthesize familyName;
#synthesize font;
#end
In MenuLayerTwoPlusThree.h:
#ifndef MenuLayerTwoPlusThree_h
#define MenuLayerTwoPlusThree_h
#import <UIKit/UIKit.h>
#import "FontType.h"
#interface MenuLayerTwoPlusThree : NSObject<UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate>
#property UITextField *textFieldFont;
#property NSMutableArray *fontTypeArray;
#property UIPickerView *pickerFont;
#property UIBarButtonItem *doneButton;
#property UIBarButtonItem *spaceButton;
#property UIBarButtonItem *cancelButton;
#property UIToolbar *toolBar;
#property NSArray *toolBarItems;
#property NSInteger familyType;
#property NSInteger fontType;
#property NSString *fontName;
- (void)createFontArray;
- (IBAction)pickerViewButtonClicked:(id)sender;
#end
In MenuLayerTwoPlusThree.m
- (void)createFontArray
{
_fontTypeArray = [[NSMutableArray alloc]initWithCapacity:80];
int number = 0;
for(NSString* family in [UIFont familyNames])
{
//NSLog(#"%#", family);
//number++;
FontType *temp = [[FontType alloc]init];
temp.familyName = family;
temp.font = [[NSMutableArray alloc]init];
int flag = 0;
for(NSString* name in [UIFont fontNamesForFamilyName:family])
{
//NSLog(#" %#", name);
//number++;
flag++;
[temp.font addObject:name];
}
// add Heiti SC, Heiti TC, Telugu Sangam MN, and Bangla Sangam MN to font array
if(flag == 0)
{
[temp.font addObject:family];
}
[_fontTypeArray addObject:temp];
}
// print all fonts test
for(FontType *x in _fontTypeArray)
{
number++;
NSLog(#"%#", x.familyName);
for(NSString *y in x.font)
{
//number++;
NSLog(#"\t%#", y);
}
}
NSLog(#"//////////////////////////////");
NSLog(#"%d", number);
}
- (void)createSelectPanel
{
[self createSelectPanelForPancel1];
[self createSelectPanelForPancel2];
[self createSelectPanelForFont];
[self createSelectPanelForShape];
[self createSelectPanelForEraser];
}
- (void)createSelectPanelForFont
{
_textFieldFont = [[UITextField alloc]initWithFrame:CGRectMake(19, 148, 150, 12)];
[_textFieldFont setBackground:[UIImage imageNamed:#"font-type-bar.png"]];
_textFieldFont.rightViewMode = UITextFieldViewModeAlways;
_textFieldFont.delegate = self;
[_textFieldFont setPlaceholder:#"Heiti TC"];
_textFieldFont.font = [_textFieldFont.font fontWithSize:10 * _aspectRatio];
// resize right view image
UIImageView *rightViewImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 12 * _aspectRatio, 12 * _aspectRatio)];
[rightViewImage setImage:[UIImage imageNamed:#"font-type-bar-roll.png"]];
_textFieldFont.rightView = rightViewImage;
_pickerFont = [[UIPickerView alloc]init];
_pickerFont.dataSource = self;
_pickerFont.delegate = self;
_pickerFont.showsSelectionIndicator = YES;
_doneButton = [[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(pickerViewButtonClicked:)];
_spaceButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
_cancelButton = [[UIBarButtonItem alloc]initWithTitle:#"Cancel" style:UIBarButtonItemStylePlain target:self action:#selector(pickerViewButtonClicked:)];
_toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 45)];
[_toolBar setBarStyle:UIBarStyleDefault];
_toolBarItems = [NSArray arrayWithObjects:_cancelButton, _spaceButton, _doneButton, nil];
[_toolBar setItems:_toolBarItems];
_textFieldFont.inputView = _pickerFont;
_textFieldFont.inputAccessoryView = _toolBar;
if (#available(iOS 9.0, *)) {
_textFieldFont.inputAssistantItem.leadingBarButtonGroups = #[];
} else {
// Fallback on earlier versions
}
if (#available(iOS 9.0, *)) {
_textFieldFont.inputAssistantItem.trailingBarButtonGroups = #[];
} else {
// Fallback on earlier versions
}
// I want to add these codes to select row in advanced to make sure first time NSLog will print 9, but it doesn't work.
// [_pickerFont reloadAllComponents];
// _familyType = 9;
// _fontType = 0;
// _fontName = #"Heiti TC";
// [_pickerFont selectRow:_familyType inComponent:0 animated:YES];
[_selectPanelFontView addSubview:_textFieldFont];
}
And this is the delegate, which I am written in the MenuLayerTwoPlusThree.m:
// The number of columns of data
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
// The number of rows of data
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if(component == 0)
{
return _fontTypeArray.count;
}
else
{
NSInteger rowOneSelected = [_pickerFont selectedRowInComponent:0];
FontType *temp = _fontTypeArray[rowOneSelected];
NSLog(#"%ld", (long)rowOneSelected); // I use this to debug, and there is a main question: why every time it logs 0 first?
return temp.font.count;
}
}
// The data to return for the row and component (column) that's being passed in
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if(component == 0)
{
FontType *temp = _fontTypeArray[row];
return temp.familyName;
}
else
{
NSInteger rowOneSelected = [_pickerFont selectedRowInComponent:0];
FontType *temp = _fontTypeArray[rowOneSelected];
return [temp.font objectAtIndex:row];
}
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if(component == 0)
{
[_pickerFont reloadComponent:1];
}
// This block is moved to pickerViewButtonClicked (sender == _doneButton)
// else
// {
// NSInteger rowOneSelected = [_pickerFont selectedRowInComponent:0];
// FontType *temp = _fontTypeArray[rowOneSelected];
// [_textFieldFont setText:temp.font[row]];
// }
}
- (IBAction)pickerViewButtonClicked:(id)sender
{
if(sender == _doneButton)
{
// Save value when I clicked the done button.
_familyType = [_pickerFont selectedRowInComponent:0];
_fontType = [_pickerFont selectedRowInComponent:1];
// NSLog(#"family: %ld", _familyType);
// NSLog(#"font: %ld", _fontType);
FontType *temp = _fontTypeArray[_familyType];
_fontName = temp.font[_fontType];
[_textFieldFont setText:_fontName];
// NSLog(#"font name: %#", _fontName);
[_textFieldFont endEditing:YES];
}
else if(sender == _cancelButton)
{
[_textFieldFont endEditing:YES];
// I want to turn back to the last selected value when I clicked the cancel button.
[_pickerFont reloadAllComponents];
[_pickerFont selectRow:_familyType inComponent:0 animated:NO];
[_pickerFont selectRow:_fontType inComponent:1 animated:NO];
}
}
It really seems that when the UIPickerView appears as an inputView after you have selected row 9 in component 0, the selectedRowInComponent: 0 returns 0 in numberOfRowsInComponent: 1, but then returns 9 in titleForRow: row forComponent: 1.
I don't think you are doing anything wrong, so it looks like a bug in UIKit.
As a workaround, I suggest you don't ask the picker view for the selected row, but track the selected row yourself (initialize your own variable in your view controller and update it in didSelectRow: row inComponent: 0)
Sorry, I forgot to post my solution.
I use _fontTypeIsFirst to see if UIPickerView has been selected or not.
When you roll the picker view, you should set _fontTypeIsFirst to NO and reload component 1, so it can return to row 0 in component 1 as usual.
Here is my code:
Some delegates:
// The number of columns of data
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
// The number of rows of data
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if(component == 0)
{
return _fontTypeArray.count;
}
else
{
NSInteger rowOneSelected;
if (_fontTypeIsFirst == YES)
{
rowOneSelected = _selectedRow;
}
else
{
rowOneSelected = [_pickerFont selectedRowInComponent:0];
}
FontType *temp = _fontTypeArray[rowOneSelected];
return temp.font.count;
}
}
// The data to return for the row and component (column) that's being passed in
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if(component == 0)
{
FontType *temp = _fontTypeArray[row];
return temp.familyName;
}
else
{
NSInteger rowOneSelected;
if (_fontTypeIsFirst == YES)
{
rowOneSelected = _selectedRow;
}
else
{
rowOneSelected = [_pickerFont selectedRowInComponent:0];
}
FontType *temp = _fontTypeArray[rowOneSelected];
return [temp.font objectAtIndex:row];
}
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
_fontTypeIsFirst = NO;
if(component == 0)
{
[_pickerFont reloadComponent:1];
}
}
And this is action of done and cancel button:
- (IBAction)pickerViewButtonClicked:(id)sender
{
if(sender == _doneButton)
{
_selectedRow = [_pickerFont selectedRowInComponent:0];
_familyType = [_pickerFont selectedRowInComponent:0];
_fontType = [_pickerFont selectedRowInComponent:1];
FontType *temp = _fontTypeArray[_familyType];
_fontName = temp.font[_fontType];
[_textFieldFont setText:_fontName];
[_textFieldFont endEditing:YES];
_fontTypeIsFirst = YES;
}
else if(sender == _cancelButton)
{
[_textFieldFont endEditing:YES];
_fontTypeIsFirst = YES;
[_pickerFont selectRow:_familyType inComponent:0 animated:NO];
[_pickerFont selectRow:_fontType inComponent:1 animated:NO];
}
}
And you can add codes like this to select default value of picker view in viewDidLoad or some place:
_fontTypeIsFirst = YES;
_selectedRow = 9;
_familyType = 9;
_fontType = 0;
_fontName = #"Heiti TC";
[_pickerFont selectRow:_familyType inComponent:0 animated:NO];
[_pickerFont selectRow:_fontType inComponent:1 animated:NO];
I'm trying to build a screen with two dependent components in a picker view. I connected picker with property, dataSource and delegate with yellow button of view controller on the top of the scene, and I also connected button action with buttonPressed method.
After building I receive white screen with empty picker without data. Why does it happen?
#import "BIDDependentComponentPickerViewController.h"
#define kStateComponent 0
#define kZipComponent 1
#interface BIDDependentComponentPickerViewController ()
#property (strong, nonatomic) NSDictionary *stateZips;
#property (strong, nonatomic) NSArray *states;
#property (strong, nonatomic) NSArray *zips;
#property (weak, nonatomic) IBOutlet UIPickerView *dependentPicker;
#end
#implementation BIDDependentComponentPickerViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSBundle *bundle = [NSBundle mainBundle];
NSURL *plistURL = [bundle URLForResource:#"statedictionary"
withExtension:#"plist"];
self.stateZips = [NSDictionary dictionaryWithContentsOfURL:plistURL];
NSArray *allStates = [self.stateZips allKeys];
NSArray *sortedStates = [allStates sortedArrayUsingSelector:
#selector(compare:)];
self.states = sortedStates;
NSString *selectedState = self.states[0];
self.zips = self.stateZips[selectedState];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
}
- (IBAction)buttonPressed:(id)sender {
NSInteger stateRow = [self.dependentPicker
selectedRowInComponent:kStateComponent];
NSInteger zipRow = [self.dependentPicker
selectedRowInComponent:kZipComponent];
NSString *state = self.states[stateRow];
NSString *zip = self.zips[zipRow];
NSString *title = [[NSString alloc] initWithFormat:
#"You selected zip code %#.", zip];
NSString *message = [[NSString alloc] initWithFormat:
#"%# is in %#", zip, state];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component {
if (component == kStateComponent) {
return [self.states count];
} else {
return [self.zips count];
}
}
- (NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component
{
if (component == kStateComponent) {
return self.states[row];
} else {
return self.zips[row];
}
}
- (void)pickerView:(UIPickerView *)pickerView
didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
if (component == kStateComponent) {
NSString *selectedState = self.states[row];
self.zips = self.stateZips[selectedState];
[self.dependentPicker reloadComponent:kZipComponent];
[self.dependentPicker selectRow:0
inComponent:kZipComponent
animated:YES];
} }
#end
Make sure you have added statedictionary file in bundle.
Also you can Log the array/dictionary to check if values are there.
I have Picker with 3 components. My goal is create dependency the second component from first, and third from second. For example: when customer pick car maker AUDI in first component he will see list of models in second component: #"A3", #"A4", #"A5" etc. When he pick model #"A3" he will see list of engines for this certain model: #"1.8 TFSI", #"2.0 TFSI" etc. which list is be different for model #"A4": #"2.0 TFSI", #"2.0 TDI". So, certain model has its own list of engines.
I did first part, when I choose car maker I've got list of models, but have no idea how handle another - make dependency between models and engines.
I appreciate any help, thanks.
Here is my code:
MpgPresetDataViewController.h
#property (weak, nonatomic) IBOutlet UILabel *picCarLabel;
#property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
MpgPresetDataViewController.m Properties
#property (strong, nonatomic) NSMutableArray *make;
#property (strong, nonatomic) NSMutableArray *audi;
#property (strong, nonatomic) NSMutableArray *bmw;
#property (strong, nonatomic) NSMutableArray *cadillac;
#property (strong, nonatomic) NSMutableArray *tesla;
#property (strong, nonatomic) NSString *carMaker;
#property (strong, nonatomic) NSString *model;
View Did Load
- (void)viewDidLoad
{
[super viewDidLoad];
_make = [[NSMutableArray alloc] initWithObjects:#"AUDI", #"BMW", #"CADILLAC", #"TESLA",
nil];
_audi = [[NSMutableArray alloc] initWithObjects:#"A3", #"A4", #"A5", #"A6", #"A8",
#"Allroad", #"Q3", nil];
_bmw = [[NSMutableArray alloc] initWithObjects:#"1 series", #"2 series", #"3 series", #"4
series", #"5 series", #"7 series", nil];
_cadillac = [[NSMutableArray alloc] initWithObjects:#"ELR", #"CTS COUPE", #"ATS", #"CTS",
#"XTS", nil];
_tesla = [[NSMutableArray alloc] initWithObjects:#"Model S", #"Model X", nil];
Number Of Components Method
-(NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 3;
}
Did Select Row Method
-(void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:
(NSInteger)component {
if (component == 0) {
_carMaker = [[NSString alloc] initWithFormat:#"%#", [_make objectAtIndex:row]];
}
[pickerView reloadComponent:1];
}
Number Of Rows Method
-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:
(NSInteger)component {
//Set numbers of rows
if (component == 0) {
return [_make count];
}
else {
if ([_carMaker isEqualToString:#"AUDI"]) {
return [_audi count];
}
if ([_carMaker isEqualToString:#"BMW"]) {
return [_bmw count];
}
if ([_carMaker isEqualToString:#"CADILLAC"]) {
return [_cadillac count];
}
if ([_carMaker isEqualToString:#"TESLA"]) {
return [_tesla count];
}
}
return 0;
}
Title for Row Method
-(NSString *) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row
forComponent:(NSInteger)component {
if (component == 0) {
return [_make objectAtIndex:row];
}
else {
if ([_carMaker isEqualToString:#"AUDI"]) {
return [_audi objectAtIndex:row];
}
if ([_carMaker isEqualToString:#"BMW"]) {
return [_bmw objectAtIndex:row];
}
if ([_carMaker isEqualToString:#"CADILLAC"]){
return [_cadillac objectAtIndex:row];
}
else {
return [_tesla objectAtIndex:row];
}
}
return 0;
}
I have 2 picker views on my story board, everything done separately. However for some reason my second viewer is exactly copying the first one.
#import <UIKit/UIKit.h>
#interface MaisOuiViewController : UIViewController <UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate>
#property (strong, nonatomic) IBOutlet UIPickerView *from;
#property (strong, nonatomic) IBOutlet UIPickerView *to;
#property (strong, nonatomic) NSArray * fromlang;
#property (strong,nonatomic) NSArray * tolang;
#end
#synthesize from;
#synthesize to;
#synthesize fromlang = _fromlang;
#synthesize tolang= _tolang;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.inWord.delegate = self;
//Load NSArray fromlang
_fromlang= [[NSArray alloc] initWithObjects:#"English",#"Spanish",#"German", nil];
//Load NSArray tolang
_tolang= [[NSArray alloc] initWithObjects:#"Hindi",#"Chinese",#"Check", nil];
}
#pragma mark - UIPickerView Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)numberOfComponentsInPickerView1:(UIPickerView *)pickerView1
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return _fromlang.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [_fromlang objectAtIndex:row];
}
- (NSInteger)pickerView1:(UIPickerView *)pickerView1 numberOfRowsInComponent1:(NSInteger)component1
{
return _tolang.count;
}
- (NSString *)pickerView1:(UIPickerView *)pickerView1 titleForRow:(NSInteger)row forComponent1:(NSInteger)component1
{
return [_tolang objectAtIndex:row];
}
#end
Can somebody please point out where am I going wrong ?
Thanks
The UIPickerViews from and to looks same because the delegate and datasource methods are same. iOS will call only the methods which are mentioned below as per the apple iOS documents refers
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return _fromlang.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [_fromlang objectAtIndex:row];
}
You can instead set tag for to and from and use the code mentioned below
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.inWord.delegate = self;
//Set tags to differentiate
from.tag=1;
to.tag=2;
//Load NSArray fromlang
_fromlang= [[NSArray alloc] initWithObjects:#"English",#"Spanish",#"German", nil];
//Load NSArray tolang
_tolang= [[NSArray alloc] initWithObjects:#"Hindi",#"Chinese",#"Check", nil];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
if(pickerView.tag==1)
{
return 1;
}
else
{
return 1;
}
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if(pickerView.tag==1)
{
return _fromlang.count;
}
else
{
return _tolang.count;
}
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if(pickerView.tag==1)
{
return [_fromlang objectAtIndex:row];
}
else
{
return [_tolang objectAtIndex:row];
}
}
Hope this will help.
If you have two buttons to show that then take one integer variable and set some value or take BOOL value to make the difference or set the tag of your picker then after based on your condition call your array in picker view.
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString *titleString;
if (currentSelectedDropDown == 1) {
AIFillAccountTypeParserModel *modal = [[AIFillAccountTypeParserModel alloc]init];
if (isSearching) {
modal = [filteredArray objectAtIndex:row];
}else{
modal = [accountTypesPickerArray objectAtIndex:row];
}
titleString = modal.accountType;
accountTypeTxtField.text = titleString;
}
else if(currentSelectedDropDown == 2) {
AIFillRatingParserModel *modal = [[AIFillRatingParserModel alloc]init];
if (isSearching) {
modal = [filteredArray objectAtIndex:row];
}else{
modal = [accountTypesPickerArray objectAtIndex:row];
}
titleString = modal.rating_Description;
ratingTxtField.text = titleString;
}
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (currentSelectedDropDown == 1) {
AIFillAccountTypeParserModel *modal = nil;
if (isSearching) {
modal = [filteredArray objectAtIndex:row];
}
else{
modal = [accountTypesPickerArray objectAtIndex:row];
}
NSString *titleString = modal.accountType;
accountTypeTxtField.text = titleString;
accountTypeId = modal.accountId;
NSUserDefaults *accountType = [NSUserDefaults standardUserDefaults];
[accountType setObject:modal.accountId forKey:#"SavedaccountType"];
}
else if (currentSelectedDropDown == 2) {
AIFillOwnershipParserModel *modal = nil;
if (isSearching) {
modal = [filteredArray objectAtIndex:row];
}
else{
modal =[accountTypesPickerArray objectAtIndex:row];
}
NSString *titleString = modal.ownership;
ownershipTypeTxtField.text = titleString;
ownerShipId = modal.ownerShipID;
}
}
This is just a example to make you understand
I have this:
AddSightingViewController.h
#import <UIKit/UIKit.h>
#class MovieSighting;
#interface AddSightingViewController : UIViewController <UITextFieldDelegate, UIPickerViewDelegate, UIPickerViewDataSource>
#property (weak, nonatomic) IBOutlet UITextField *movieTitleInput;
#property (weak, nonatomic) IBOutlet UITextField *directorInput;
#property (weak, nonatomic) IBOutlet UITextField *estrenoInput;
#property (strong, nonatomic) MovieSighting *movieSighting;
#end
and this:
AddSightingViewController.m
#import "AddSightingViewController.h"
#import "MovieSighting.h"
#interface AddSightingViewController ()
#property (strong, nonatomic) NSArray *array;
#property (strong, nonatomic) UITextField *genero;
#end
#implementation AddSightingViewController
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if ((textField == self.movieTitleInput) || (textField == self.estrenoInput) || (textField == self.directorInput)) {
[textField resignFirstResponder];
}
return YES;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *generos = [[NSArray alloc] initWithObjects:#"Drama",#"Fantástico",#"Aventuras",#"Policíaco",#"Romántica",#"Comedia",#"Documental",#"Terror", nil];
self.array = generos;
}
#pragma mark Picker Data Source Methods
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return [_array count];
}
#pragma mark Picker Delegate Methods
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [_array objectAtIndex:row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
NSString *generoInput = [self.array objectAtIndex:row];
}
//
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"ReturnInput"]) {
if ([self.movieTitleInput.text length] || [self.directorInput.text length]|| [self.estrenoInput.text length] )
{
MovieSighting *sighting;
NSUInteger *valor = (NSUInteger *)[[self.estrenoInput text] integerValue];
sighting = [[MovieSighting alloc] initWithName:self.movieTitleInput.text anyo:valor genero: director:self.directorInput.text];
self.movieSighting = sighting;
}
}
}
#end
How can I use:
NSString *generoInput = [self.array objectAtIndex:row];
To transform to a property UITextField that I created:
#property (strong, nonatomic) UITextField *genero;
And then use that with the method sighting at the end?
Then I can use it to compare on the method called textFieldShouldReturn.
I´ve been doing that:
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
NSString *generoInput = [self.array objectAtIndex:row];
UITextField *nuevo = (UITextField*)[generoInput capitalizedString];
_genero = nuevo;
}
And prepareForSegue method:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"ReturnInput"]) {
if ([self.movieTitleInput.text length] || [self.directorInput.text length]|| [self.estrenoInput.text length]
|| [self.genero.text length])
{
MovieSighting *sighting;
NSUInteger *valor = (NSUInteger *)[[self.estrenoInput text] integerValue];
sighting = [[MovieSighting alloc] initWithName:self.movieTitleInput.text anyo:valor genero:self.genero.text director:self.directorInput.text];
self.movieSighting = sighting;
}
}
}
But don´t work the app... Any idea?
When I add other movie and clic on Add:
Appeared that:
Error code:
2013-12-19 21:43:19.875 MovieWatching[920:70b] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<AddSightingViewController 0x8a63190> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key staticDataSource.'
You want:
self.genero.text = [generoInput capitalizedString];