Programmatically add uipickerview and its data - ios

In my view did load there is a for loop which selects data from sqlite and produce uipickerview dynamically
for (int k=0; k<dataQuestions.count; k++)
{
pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,656,768,44)];
pickerToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(pickerCancel:)];
[barItems addObject:cancelBtn];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(pickerDone:)];
[barItems addObject:doneBtn];
pickerToolbar.hidden=YES;
myPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0,700,768,260)];
myPicker.tag = [[[dataQuestions objectAtIndex:k] valueForKey:#"ids"] integerValue];
myPicker.showsSelectionIndicator = TRUE;
myPicker.dataSource = self;
myPicker.delegate = self;
myPicker.hidden = YES;
[myPicker setBackgroundColor:[UIColor grayColor]];
myPicker.tag=[[[dataQuestions objectAtIndex:k] valueForKey:#"ids"] integerValue];
UIButton *buttonControl = [UIButton buttonWithType:UIButtonTypeCustom];
buttonControl.tag = [[[dataQuestions objectAtIndex:k] valueForKey:#"ids"] integerValue];
[buttonControl setBackgroundImage:[UIImage imageNamed:#"combobox.png"] forState:UIControlStateNormal];
[buttonControl setTitle:#"Seçiniz" forState:UIControlStateNormal];
[buttonControl setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
buttonControl.frame = CGRectMake(30,sectionY + mainCategoryHeight+subCategoryHeight+questionLabel.frame.size.height +offsetQuestion,433,38);
[buttonControl addTarget:self action:#selector(selectSingleChoice:) forControlEvents:UIControlEventTouchUpInside];
NSMutableArray *choices=[[NSMutableArray alloc] initWithArray:[[DBHelper getSharedInstance:[LoggedinUser sharedCenter].dbFileName] getQuestionChoicesByQuestionID:[[dataQuestions objectAtIndex:k] valueForKey:#"ids"]]];
_singleSelectedData= [choices valueForKey:#"choiceName"];
[pickerToolbar setItems:barItems animated:YES];
[self.scrlview addSubview:pickerToolbar];
[self.scrlview addSubview:buttonControl];
}
so this is my code but the problem is it loads the last item's data to all uipickerviews
because there is only 1 array i have
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return _singleSelectedData.count;
}
EDIT:
my picker view delegate method:
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, pickerView.frame.size.width, 44)];
label.backgroundColor = [UIColor whiteColor];
label.textColor = [UIColor blackColor];
label.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:18];
label.text = [_singleSelectedData objectAtIndex:row];
return label;
}
all uipickerviews must show its own data. any suggestions will be appreciated

Your choices array allocating and initializing in for loop for every k values. Create array out of for loop like that;
NSMutableArray *choices = [NSMutableArray new];
for (int k=0; k<dataQuestions.count; k++)
{
...
...
[choices addObject:[[DBHelper getSharedInstance:[LoggedinUser sharedCenter].dbFileName] getQuestionChoicesByQuestionID:[[dataQuestions objectAtIndex:k] valueForKey:#"ids"]]];
_singleSelectedData = [[choices valueForKey:#"choiceName"] objectAtIndex:k];
}
I'm outside so can't try code, not sure to work correctly. Test it and let me know what's going on please.

Related

Adding IB constraints breaks UIPickerview - ObjC

I have a view built with interface builder. Whenever I add a pickerview (either via IB or programmatically) on top of the existing IB view, the pickerview will not work. The elements within the pickerview won't scroll properly... the spinner wheel always bounces back to the '0 position' & nothing can be selected. Also, the wheel no longer 'clicks'... and the wheel moves in horizontally as well as vertically... but nothing can be selected.
I even tried to add the pickerview to an actionsheet... same result.
If I completely remove ALL of the constraints in the view, the pickerview works as it should.
Any ideas on what's causing this and/or how to fix?
screen shot
Here's the code:
- (void)viewDidLoad {
[super viewDidLoad];
self.tabBarController.tabBar.hidden = YES;
[self.view setBackgroundColor:[UIColor clearColor]];
letterArray = #[#"",#"A",#"B",#"C",#"D"];
UIBarButtonItem *pBtn = [[UIBarButtonItem alloc] initWithTitle:#"picker"
style:UIBarButtonItemStylePlain
target:self
action:#selector(launchPicker:)];
self.navigationItem.rightBarButtonItem = pBtn;
}
- (void)launchPicker:(id)sender {
self.viewForMainPicker = [[UIView alloc]initWithFrame:CGRectMake(0, 400, [[UIScreen mainScreen] bounds].size.width, 220)];
UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 44)];
UIPickerview *mainPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 220)];
UIFont *arrowfont = [UIFont boldSystemFontOfSize:40];
UIFont *titlefont = [UIFont boldSystemFontOfSize:28];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:#"→"
style:UIBarButtonItemStylePlain
target:self
action:#selector(fakeEvent)];
UIBarButtonItem *title = [[UIBarButtonItem alloc] initWithTitle:#"Select Gender"
style:UIBarButtonItemStylePlain
target:nil
action:nil];
UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:self
action:nil];
[doneBtn setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:0.933 green:0.122 blue:0.145 alpha:1.00], NSForegroundColorAttributeName,
arrowfont, NSFontAttributeName, nil] forState:UIControlStateNormal];
[title setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:0.933 green:0.122 blue:0.145 alpha:1.00], NSForegroundColorAttributeName,
titlefont, NSFontAttributeName, nil] forState:UIControlStateNormal];
NSArray *btnArray = [[NSArray alloc] initWithObjects:flex,flex,title,flex,doneBtn,nil];
mainPicker.delegate = self;
mainPicker.dataSource = self;
[toolBar setItems:btnArray];
[viewForMainPicker addSubview:mainPicker];
[viewForMainPicker addSubview:toolBar];
[self.view addSubview:viewForMainPicker];
}
- (void)fakeEvent {
NSLog(#"selected");
}
- (NSInteger)numberOfComponentsInPickerView:(nonnull UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(nonnull UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return letterArray.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return letterArray[row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSString *newString = [NSString stringWithFormat:#"%#", letterArray[row]];
NSLog(#"passing: %#",newString);
}

Default value in textfield using picker view

I am new to iOS i need to display objectAtIndex(0) in textfield using uipickerview .while run the app textfield empty after selection in picker view only value can displayed in textfield i need initially textfield not be empty.
viewdidload code:
pktStatePicker .delegate = self;
pktStatePicker .dataSource = self;
[ pktStatePicker setShowsSelectionIndicator:YES];
txtText.inputView = pktStatePicker ;
// Create done button in UIPickerView
UIToolbar* mypickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 56)];
mypickerToolbar.barStyle = UIBarStyleBlackOpaque;
[mypickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(pickerDoneClicked)];
[barItems addObject:doneBtn];
[mypickerToolbar setItems:barItems animated:YES];
txtText.inputAccessoryView = mypickerToolbar;
picker view delegates:
- (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSLog([arrmsg1 objectAtIndex:row]);
txtText.text = (NSString *)[arrMsg objectAtIndex:row];
}
set like on your ViewDidLoad, but ensure once your arrMsg.count!=0
- (void)viewDidLoad
{
[super viewDidLoad];
txtText.text = (NSString *)[arrMsg objectAtIndex:0];
}
choice-2
or when you append the data in arrMsg after that call like
if (arrMsg.count >0)
{
txtText.text = (NSString *)[arrMsg objectAtIndex:0];
}
there after continue your work asuseual

UIToolbar UILabel x y offset not working

I encountered a strange problem. I need to put two UILabel in UIToolbar. I have a button and I want to share with respect to its center the UILabel, as if I did not change the values of x and y inside UILabel up at the bottom, they do not move. Tell me what could be the problem. I need a little lower down the UILabel.
= [NSMutableArray new];
_createdLabel = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, 100, 20)];
_createdLabel.backgroundColor = [UIColor clearColor];
_createdLabel.textColor = [UIColor whiteColor];
UIFont *myFont = [ UIFont fontWithName:#"GothamPro-Light" size:15.0f ];
_createdLabel.font = myFont;
_createdLabel.text = #"";
UIBarButtonItem *createdTitle = [[UIBarButtonItem alloc] initWithCustomView:_createdLabel];
[_items addObject:createdTitle];
UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[_items addObject:space];
UILabel *shareLabel = [[UILabel alloc] initWithFrame: CGRectMake(0, -1000, 85, 20)];
shareLabel.backgroundColor = [UIColor clearColor];
shareLabel.textColor = [UIColor whiteColor];
UIFont *shareFont = [ UIFont fontWithName:#"GothamPro-Light" size:15.0f ];
shareLabel.font = shareFont;
shareLabel.text = #"Поделиться";
[shareLabel sizeToFit];
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(socialSharing:)];
[shareLabel setUserInteractionEnabled:YES];
[shareLabel addGestureRecognizer:tap];
UIBarButtonItem *shareTitle = [[UIBarButtonItem alloc] initWithCustomView:shareLabel] ;
[_items addObject:shareTitle];
UIBarButtonItem *shareBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(socialSharing:)];
shareBtn.tintColor = [UIColor whiteColor];
[_items addObject:shareBtn];
[self.toolBar setItems:_items animated:YES];
Please remove [shareLabel sizeToFit]; in your code.
Use below code for share label.
UILabel *shareLabel = [[UILabel alloc] initWithFrame: CGRectMake(0, 4, 85, 16)];
You try this code, it's working fine
UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 40)];
[toolBar setBarTintColor: self.navigationController.navigationBar.tintColor];
NSMutableArray *items= [NSMutableArray new];
UILabel *createdLabel = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, 50, 20)];
createdLabel.backgroundColor = [UIColor clearColor];
createdLabel.textColor = [UIColor whiteColor];
UIFont *myFont = [ UIFont fontWithName:#"GothamPro-Light" size:15.0f ];
createdLabel.font = myFont;
createdLabel.text = #"Hai";
UIBarButtonItem *createdTitle = [[UIBarButtonItem alloc] initWithCustomView:createdLabel];
[items addObject:createdTitle];
UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[items addObject:space];
UILabel *shareLabel = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, 85, 20)];
shareLabel.backgroundColor = [UIColor clearColor];
shareLabel.textColor = [UIColor whiteColor];
UIFont *shareFont = [ UIFont fontWithName:#"GothamPro-Light" size:15.0f ];
shareLabel.font = shareFont;
shareLabel.text = #"Hello "; //#"Поделиться";
[shareLabel sizeToFit];
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(socialSharing)];
[shareLabel setUserInteractionEnabled:YES];
[shareLabel addGestureRecognizer:tap];
UIBarButtonItem *shareTitle = [[UIBarButtonItem alloc] initWithCustomView:shareLabel] ;
[items addObject:shareTitle];
UIBarButtonItem *shareBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(socialSharing)];
shareBtn.tintColor = [UIColor whiteColor];
[items addObject:shareBtn];
[toolBar setItems:items animated:YES];
[self.view addSubview:toolBar];
provide method like this,
-(void)socialSharing
{
NSLog(#"Hai");
}
Definitely helps to you.

how to pass two NSAarray to One UIPickerView Based on UITextFields

I did when I click when textField ,Open UIPickerView and then select PickerView Data it's display on UITextField.
But now i Have 3 text_Filed(city,typeofcars,branches) but I don't know about three TextFileds on Single UIPickerView. So Please give me any idea .
single text-Filed one UIPickerview code is:-
Businessname=[[UITextField alloc]init];
Businessname.frame=CGRectMake(0, 0, 221, 33);
Businessname.placeholder=#"Business Name";
[Businessname setFont:[UIFont fontWithName:#"Times New Roman" size:13]];
Businessname.backgroundColor=[UIColor whiteColor];
[Businessname setBorderStyle:UITextBorderStyleBezel];
Businessname.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
Businessname.layer.borderColor=[[UIColor yellowColor]CGColor];
Businessname.layer.borderWidth= 2.0f;
Businessname.textColor=[UIColor blackColor];
[scrol addSubview:Businessname];
yourpicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 400, 100, 150)];
[yourpicker setDataSource: self];
[yourpicker setDelegate: self];
yourpicker.showsSelectionIndicator = YES;
Businessname.inputView = yourpicker;
[yourpicker setShowsSelectionIndicator:YES];
mypickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 300, 100, 56)];
mypickerToolbar.barStyle = UIBarStyleBlackOpaque;
[mypickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(pickerDoneClicked)];
[barItems addObject:doneBtn];
[mypickerToolbar setItems:barItems animated:YES];
Businessname.inputAccessoryView = mypickerToolbar;
-(void)pickerDoneClicked
{
NSLog(#"Done Clicked");
[Businessname resignFirstResponder];
mypickerToolbar.hidden=YES;
yourpicker.hidden=YES;
}
- (NSInteger)numberOfComponentsInPickerView:
(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
return [pickarray count];
}
Above code is output is when I click text-filed and then open UIPIckerView and then choose the data. But now I have three TextFields and 3 NSArrays on Please how to pass data data to UIPickerView based on UITextField.
So please give me any idea about my problem
Thanks in Advanced
You can do this by setting tag value for each textfield. Also set delegate of UITextField for each textfield.
In delegate method of UITextField
- (void)textFieldDidBeginEditing:(UITextField *)textField
recongnize textfield by it's tag value.
Based on tag value do something like
// Assumed taht 100, 101, 102 is the tag values for textfield's
NSInteger tag = textField.tag;
switch(tag)
{
case 100:
pickarray = #[#"1",#"2",#"3",#"4"];
break;
case 101:
pickarray = #[#"10",#"20",#"30",#"40"];
break;
case 102:
pickarray = #[#"11",#"21",#"31",#"41"];
break;
default:
break;
}
by this you can assign different values to "pickarray" according to selected textfield and reload your UIPickerView.

UIPickerView doesn't "snap" to rows and moves out of bounds

I have a problem with UIPickerView. Following a tutorial on YT I created a UIPickerView in a popover after tapping on a textfield.
Everything looks fine, but the UIPickerView acts kind of strange. If I just tap on a row i want to select, it rolls up/down normally to this specific row and makes a selection. But if I try to scroll with my finger it doesn't roll as smooth as it should with a momentum and it doesn't snap on any of the rows. It also doesn't make a selection, but it makes a sound like it does. Worst of all, the picker doesn't seem to care about the number of rows and I can roll up and down as far as I want, although there are no selectable rows anymore.
Here is my code.
decayStatesArray contains four simple String-objects. The UITextfield und the activeDecayString are used in the "donePressed"-methode.
The main function:
-(void)showPicker:(UITextField *)textField {
activeTextfield = textField;
//for the default sizes of a UIPickerView
decayStatePicker = [[UIPickerView alloc] init];
UIViewController *popOverContent = [[UIViewController alloc] init];
UIView *popOverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [decayStatePicker frame].size.width, [decayStatePicker frame].size.height+44)];
decayStatePicker = [[UIPickerView alloc] initWithFrame:CGRectMake([popOverView frame].origin.x, [popOverView frame].origin.y + 44, 0, 0)];
pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake([popOverView frame].origin.x, [popOverView frame].origin.y, [popOverView frame].size.width, 44)];
[decayStatePicker setDataSource:self];
[decayStatePicker setDelegate:self];
[decayStatePicker setShowsSelectionIndicator:YES];
[decayStatePicker selectRow:0 inComponent:0 animated:YES];
//Create Toolbar
[pickerToolbar setBarStyle:UIBarStyleBlackTranslucent];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(donePressed)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(cancelPressed)];
[pickerToolbar setItems:[NSArray arrayWithObjects:flexibleSpace, doneButton, cancelButton, nil]];
[popOverView addSubview:decayStatePicker];
[popOverView addSubview:pickerToolbar];
[popOverContent setView:popOverView];
pickerPopoverController = [[UIPopoverController alloc] initWithContentViewController:popOverContent];
pickerPopoverController.popoverContentSize = CGSizeMake([popOverView frame].size.width, [popOverView frame].size.height);
[pickerPopoverController presentPopoverFromRect:[textField frame] inView:[self view] permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
The delegate methodes:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component {
return [decayStatesArray count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [decayStatesArray objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if ([pickerView isEqual:decayStatePicker]) {
NSLog(#"The Picker is: %#", pickerView);
NSLog(#"The item is: %#", [decayStatesArray objectAtIndex:row]);
}
activeDecayString = [decayStatesArray objectAtIndex:row];
}

Resources