Bar button methods in toolbar are not calling - ios

I have created a bar button items programatically. But the respective methods of cancel and done are not firing.
I am adding the picker view as input view for textfield programatically. On top of picker view i placed a toolbar with done and cancel buttons. Toolbar is the subview of pickerview.
Here is my code :
(void)awakeFromNib
{
contactAgeRange = [[NSMutableArray alloc]init];
for (int i = 18; i <= 100; i++)
{
[contactAgeRange addObject:[NSNumber numberWithInt:i]];
}
personGender = [[NSMutableArray alloc]initWithObjects:#"Male", #"Female", nil];
[self initializePickerView];
self.cellTextField.userInteractionEnabled = NO;
self.cellTextField.delegate = self;
UIToolbar *toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,[UIScreen mainScreen].bounds.size.width,44)];
[toolBar setBarStyle:UIBarStyleBlackOpaque];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:nil action:#selector(doneButtonPressed:)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:nil action:#selector(cancelButtonPressed)];
UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[toolBar setItems:[NSArray arrayWithObjects:cancelButton, space, doneButton, nil]];
[self.pickerView addSubview:toolBar];
}
-(void) configureCell
{
switch (self.cellType) {
case ContactCellTypeAge:
self.cellTextField.inputView = self.pickerView;
break;
case ContactCellTypeGender:
self.cellTextField.inputView = self.pickerView;
break;
}
}
-(void)initializePickerView
{
self.pickerView = [[UIPickerView alloc]init];
self.pickerView.delegate = self;
self.pickerView.dataSource = self;
self.pickerView.showsSelectionIndicator = YES;
}
-(NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
switch (self.cellType)
{
case ContactCellTypeAge:
return [contactAgeRange count];
break;
case ContactCellTypeGender:
return [personGender count];
break;
}
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (self.cellType == ContactCellTypeAge)
{
NSString *age = [[contactAgeRange objectAtIndex:row] stringValue];
return age;
}
else if (self.cellType == ContactCellTypeGender)
{
NSString *gender = [personGender objectAtIndex:row];
return gender;
}
return nil;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (self.cellType == ContactCellTypeAge)
{
pickerRowText = [[contactAgeRange objectAtIndex:row] stringValue];
//self.cellTextField.text = pickerRowText;
}
else if (self.cellType == ContactCellTypeGender)
{
pickerRowText = [personGender objectAtIndex:row];
//self.cellTextField.text = pickerRowText;
//[self.cellTextField endEditing:YES];
}
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (self.setNextResponderActionBlock)
{
self.setNextResponderActionBlock();
}
return YES;
}
-(void) cancelButtonPressed
{
[self.cellTextField resignFirstResponder];
}
-(void) doneButtonPressed : (id)sender
{
if (self.cellType == ContactCellTypeAge)
{
self.cellTextField.text = pickerRowText;
}
if (self.cellType == ContactCellTypeGender)
{
self.cellTextField.text = pickerRowText;
}
}
Please help me. Thanks in advance

configureCell in this Method add self.cellTextField.inputAccessoryView = toolbar;

Related

Objective C how to re add the deleted items in NSMutableArray

In my code, three text fields are there for picker views. For example in the first text field, the user selects A+ blood group, in the second text field we don't want to display A+ blood group. Remaining items need to display. Text field 3 also same. For example, in text field 2 the user selects B+ blood group, in text field 3 B+ and A+ blood group needs to remove in array list. For example the user again selects the first text field and reselects another blood group like A- ve group re add the A+ blood group in array list. The same thing in text field2 and text field3. I already completed the one task, i.e removing items from array. The pending task is re adding the items in array.
- (void)viewDidLoad {
dataArray = [[NSMutableArray alloc]initWithObjects:#"A+",#"A-",#"B+",#"B-",#"O+",#"O-", nil];
bloodGroup = [[UITextField alloc] initWithFrame:CGRectMake(10, logoImg.frame.origin.y+logoImg.frame.size.height+45, screenWidth-20, 50)];
bloodGroup.borderStyle = UITextBorderStyleRoundedRect;
bloodGroup.font = [UIFont systemFontOfSize:15];
bloodGroup.placeholder = #"Please Select Your Option";
bloodGroup.delegate = self;
[self.view addSubview:bloodGroup];
txtField1 = [[UITextField alloc] initWithFrame:CGRectMake(10, ansField1.frame.origin.y+ansField1.frame.size.height+45, screenWidth-20, 50)];
txtField1.borderStyle = UITextBorderStyleRoundedRect;
txtField1.font = [UIFont systemFontOfSize:15];
txtField1.placeholder = #"Please Select Your Option";
txtField1.delegate = self;
[self.view addSubview:txtField1];
txtField2 = [[UITextField alloc] initWithFrame:CGRectMake(10, ansField2.frame.origin.y+ansField2.frame.size.height+45, screenWidth-20, 50)];
txtField2.borderStyle = UITextBorderStyleRoundedRect;
txtField2.font = [UIFont systemFontOfSize:15];
txtField2.placeholder = #"Please Select Your Option";
txtField2.delegate = self;
[self.view addSubview:txtField2];
myPickerView = [[UIPickerView alloc] init];
[myPickerView setDataSource: self];
[myPickerView setDelegate: self];
myPickerView.showsSelectionIndicator = YES;
bloodGroup.inputView = myPickerView;
bloodGroup.inputAccessoryView = toolBar;
// txtField1
txtField1.inputView = myPickerView;
txtField1.inputAccessoryView = toolBar;
txtField2.inputView = myPickerView;
txtField2.inputAccessoryView = toolBar;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
if (isBloodGroupFieldSelected) {
return 1;
}
else if (!isBloodGroupFieldSelected) {
return 1;
}
else if (!isGenderGroupFieldSelected) {
return 1;
}
return 0;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (isBloodGroupFieldSelected) {
return [dataArray count];
}
else if (!isBloodGroupFieldSelected) {
return [dataArray count];
}
else if (!isGenderGroupFieldSelected) {
return [dataArray count];
}
return 0;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (isBloodGroupFieldSelected) {
return dataArray[row];
}
else if ((!isBloodGroupFieldSelected) && (isGenderGroupFieldSelected)) {
return dataArray[row];
}
else if (!isGenderGroupFieldSelected) {
return dataArray[row];
}
return 0;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (isBloodGroupFieldSelected) {
bloodGroup.text = dataArray[row];
}
else if ((!isBloodGroupFieldSelected) && (isGenderGroupFieldSelected)) {
txtField1.text = dataArray[row];
}
else if (!isGenderGroupFieldSelected) {
txtField2.text= dataArray[row];
}
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
if (textField == bloodGroup) {
isBloodGroupFieldSelected = YES;
}
else if (textField == txtField1) {
isBloodGroupFieldSelected = NO;
isGenderGroupFieldSelected = YES;
}
else if (textField == txtField2) {
isGenderGroupFieldSelected = NO;
isBloodGroupFieldSelected = NO;
}
[myPickerView reloadAllComponents];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[myPickerView selectRow:0 inComponent:0 animated:YES];
}
- (void)done:(id)sender {
[myPickerView selectRow:0 inComponent:0 animated:YES]; // On 27/04/2020
[bloodGroup resignFirstResponder];
[txtField1 resignFirstResponder];
[txtField2 resignFirstResponder];
}

Objective C how to remove items from array based on other arrays items

I have three UIPickerviews. I have a json response. I have a three text fields. If user select first picker view the related data is displaying in first textfields. And second and third picker view also same. My requirement Is if user select the the particular item in first picker view second and third picker view does not showing the item. And second and third also same.
-(void)viewDidLoad
{
dataArray = [[NSMutableArray alloc]initWithObjects:#"A+",#"A-",#"B+",#"B-",#"O+",#"O-", nil];
bloodGroup = [[UITextField alloc]initWithFrame:CGRectMake(10, logoImg.frame.origin.y+logoImg.frame.size.height+45, screenWidth-20, 50)];
bloodGroup.borderStyle = UITextBorderStyleRoundedRect;
bloodGroup.font = [UIFont systemFontOfSize:15];
bloodGroup.placeholder = #"Please Select Your Option";
bloodGroup.delegate = self;
[self.view addSubview:bloodGroup];
txtField1 = [[UITextField alloc]initWithFrame:CGRectMake(10, ansField1.frame.origin.y+ansField1.frame.size.height+45, screenWidth-20, 50)];
txtField1.borderStyle = UITextBorderStyleRoundedRect;
txtField1.font = [UIFont systemFontOfSize:15];
txtField1.placeholder = #"Please Select Your Option";
txtField1.delegate = self;
[self.view addSubview:txtField1];
txtField2 = [[UITextField alloc]initWithFrame:CGRectMake(10, ansField2.frame.origin.y+ansField2.frame.size.height+45, screenWidth-20, 50)];
txtField2.borderStyle = UITextBorderStyleRoundedRect;
txtField2.font = [UIFont systemFontOfSize:15];
txtField2.placeholder = #"Please Select Your Option";
txtField2.delegate = self;
[self.view addSubview:txtField2];
myPickerView = [[UIPickerView alloc] init];
[myPickerView setDataSource: self];
[myPickerView setDelegate: self];
myPickerView.showsSelectionIndicator = YES;
bloodGroup.inputView = myPickerView;
bloodGroup.inputAccessoryView = toolBar;
// txtField1
txtField1.inputView = myPickerView;
txtField1.inputAccessoryView = toolBar;
txtField2.inputView = myPickerView;
txtField2.inputAccessoryView = toolBar;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
if (isBloodGroupFieldSelected) {
return 1;
}
else if(!isBloodGroupFieldSelected){
return 1;
}
else if(!isGenderGroupFieldSelected)
{
return 1;
}
return 0;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (isBloodGroupFieldSelected) {
return [dataArray count];
}
else if(!isBloodGroupFieldSelected)
{
return [dataArray count];
}
else if (!isGenderGroupFieldSelected)
{
return [dataArray count];
}
return 0;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (isBloodGroupFieldSelected)
{
return dataArray[row];
}
else if((!isBloodGroupFieldSelected) && (isGenderGroupFieldSelected))
{
return dataArray[row];
}
else if(!isGenderGroupFieldSelected)
{
return dataArray[row];
}
return 0;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (isBloodGroupFieldSelected) {
bloodGroup.text = dataArray[row];
}
else if((!isBloodGroupFieldSelected) && (isGenderGroupFieldSelected))
{
txtField1.text = dataArray[row];
}
else if(!isGenderGroupFieldSelected)
{
txtField2.text= dataArray[row];
}
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
if (textField == bloodGroup) {
isBloodGroupFieldSelected = YES;
}
else if (textField == txtField1){
isBloodGroupFieldSelected = NO;
isGenderGroupFieldSelected = YES;
}
else if (textField == txtField2) {
isGenderGroupFieldSelected = NO;
isBloodGroupFieldSelected = NO;
}
[myPickerView reloadAllComponents];
}
You have most of the code you need already. The pickerView:didSelectRow:inComponent: delegate method will be called when any of your pickers have been changed, assuming they each have their delegates set to the controller above. Although, I only see one picker view defined in the code above. Let's assume your picker views are called myPickerView, myPickerView2, and myPickerView3. The code would roughly be:
- (void)pickerView:(UIPickerView *)pickerView
didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
if (pickerView == pickerView1) {
// the first picker view lseection has changed
// update myPickerView2 and myPickerView3 to reflect
// the values you want based on this selection
} else if (pickerView == pickerView2) {
// update the other views based on this selection
} else ...
....
}

UIPickerView selectedRowInComponent:0 always return zero first when I try to set data in second component

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

Add Static Row To UIPickerView

In my app, I query Parse.com to get a list of groups to populate a UIPickerView. I want to add one more option to the UIPickerView at the very top that is called "Create New Group", in which it will pull up a UITextField to make a new group and move from there. How do I go about adding this to all the other fields? Here is how I have it created so far.
-(void)addPickerView{
__block NSMutableArray *arr = [[NSMutableArray alloc] init];
PFQuery *rejectedNumber = [PFQuery queryWithClassName:#"Group"];
[rejectedNumber orderByAscending:#"GroupName"];
[rejectedNumber findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!objects) {
// Did not find any UserStats for the current user
NSLog(#"NotFound");
} else {
pickerArray = objects;
[myPickerView reloadAllComponents];
}
}];
//pickerArray = GoGo;
self.theView.signUpView.additionalField.delegate = self;
[self.theView.signUpView.additionalField setPlaceholder:#"Choose Group"];
myPickerView = [[UIPickerView alloc]init];
myPickerView.dataSource = self;
myPickerView.delegate = self;
myPickerView.showsSelectionIndicator = YES;
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]
initWithTitle:#"Done" style:UIBarButtonItemStyleDone
target:self action:#selector(finishIt)];
UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:
CGRectMake(0, self.view.frame.size.height-
self.theView.signUpView.additionalField.frame.size.height-50, 320, 50)];
[toolBar setBarStyle:UIBarStyleBlackOpaque];
NSArray *toolbarItems = [NSArray arrayWithObjects:
doneButton, nil];
[toolBar setItems:toolbarItems];
self.theView.signUpView.additionalField.inputView = myPickerView;
self.theView.signUpView.additionalField.inputAccessoryView = toolBar;
}
#pragma mark - Picker View Data source
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component{
return [pickerArray count];
}
#pragma mark- Picker View Delegate
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:
(NSInteger)row inComponent:(NSInteger)component{
[self.theView.signUpView.additionalField setText:self.theGroup];
}
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
PFObject *object = pickerArray[row];
//Assuming "Category" is a string here for your title
self.theGroup = object[#"GroupName"];
return object[#"GroupName"];
}
You can add achieve that in two ways:
Add an extra object at the first index of your array for showing "Create New Group"
You can use tweak the datasource and delegate methods of UIPickerView. Like:
-(NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
return [pickerArray count] + 1; // +1 for the additional row
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:
(NSInteger)row inComponent:(NSInteger)component
{
if (row == 0)
{
// Create New Group clicked
}
else
{
// Some other option selected
}
}
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString *groupName = "Create New Group";
if (row != 0)
{
PFObject *object = pickerArray[row - 1]; // -1 to handle the array index
self.theGroup = object[#"GroupName"];
groupName = object[#"GroupName"];
}
return groupName;
}

ios : display the array value in uitextfield?

display the key value in textfield.
this is how I am fetching data:
-(void)textFieldDidBeginEditing:(UITextField *)textField{
if (txtviolation.editing == YES)
{
pickerV = [[UIPickerView alloc]init];
pickerV.dataSource = self;[![enter image description here][1]][1]
pickerV.delegate = self;
pickerV.showsSelectionIndicator = YES;
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(done)];
[[UIBarButtonItem appearance] setTintColor:[UIColor whiteColor]];
UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-pickerV.frame.size.height-50, 320, 50)];
[toolBar setBarStyle:UIBarStyleBlackOpaque];
NSArray *toolbarItems = [NSArray arrayWithObjects:doneButton, nil];
[toolBar setItems:toolbarItems];
txtviolation.inputView = pickerV;
txtviolation.inputAccessoryView = toolBar;
flag=1;
}
else if (txttype.editing == YES)
{
pickerV = [[UIPickerView alloc]init];
pickerV.dataSource = self;
pickerV.delegate = self;
pickerV.showsSelectionIndicator = YES;
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(done)];
[[UIBarButtonItem appearance] setTintColor:[UIColor whiteColor]];
UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-pickerV.frame.size.height-50, 320, 50)];
[toolBar setBarStyle:UIBarStyleBlackOpaque];
NSArray *toolbarItems = [NSArray arrayWithObjects:
doneButton, nil];
[toolBar setItems:toolbarItems];
txttype.inputView = pickerV;
txttype.inputAccessoryView = toolBar;
flag=2;
}
}
this is how display data:
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (flag==1)
{
return ViolationA.count;
}
else if (flag==2)
{
return VTypeA.count;
}
return 0;
}
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (flag==1)
{
return [NSString stringWithFormat:#"%#", [ViolationA objectAtIndex:row]];
}
else if (flag==2)
{
return [NSString stringWithFormat:#"%#", [VTypeA objectAtIndex:row]];
}
return 0;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (flag==1)
{
// [txtviolation setText:[[ViolationA objectAtIndex:row] forKeyPath:#"violation_name"]];
txtviolation.text =[NSString stringWithFormat:#"%#", [ViolationA objectAtIndex:row]];
}
else if (flag==2)
{
txttype.text = [NSString stringWithFormat:#"%#", [VTypeA objectAtIndex:row]];
}
}
I am not able to bind:
click on textfield then open the uipicker view but not display title name only display the '{' sign.
then roll it to set value of array key and value also but I am only set the value.
uitextfield setvalue:
{ "violation_name" = "Street Light"; }
and
uipickerview display:
{
{
{
then to select any value to set in text field.
uipickerview perform to select the text field not use any button
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (flag==0)
{
return ViolationA.count;
}
else if (flag==1)
{
return VTypeA.count;
}
return 0;
}
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (flag==0)
{
return [[ViolationA objectAtIndex:row] valueForKey:#"violation_name"];
}
else if (flag==1)
{
return [[VTypeA objectAtIndex:row] valueForKey:#"priorirty_name"];
//[NSString stringWithFormat:#"%#", [VTypeA objectAtIndex:row]];
//violation_name
}
return 0;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (flag==0)
{
txtviolation.text =[[ViolationA objectAtIndex:row] valueForKey:#"violation_name"];
//[NSString stringWithFormat:#"%#",[ViolationA objectAtIndex:row]];
}
else if (flag==1)
{
txttype.text = [[VTypeA objectAtIndex:row] valueForKey:#"priorirty_name"];
}
}
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
pickerV = [[UIPickerView alloc]init];
pickerV.dataSource = self;
pickerV.delegate = self;
pickerV.showsSelectionIndicator = YES;
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(done)];
[[UIBarButtonItem appearance] setTintColor:[UIColor whiteColor]];
UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-pickerV.frame.size.height-50, 320, 50)];
[toolBar setBarStyle:UIBarStyleBlackOpaque];
NSArray *toolbarItems = [NSArray arrayWithObjects:doneButton, nil];
[toolBar setItems:toolbarItems];
if (txtviolation.editing == YES)
{
txtviolation.inputView = pickerV;
txtviolation.inputAccessoryView = toolBar;
flag=0;
}
else if (txttype.editing == YES)
{
txttype.inputView = pickerV;
txttype.inputAccessoryView = toolBar;
flag=1;
}
}

Resources