How to place UIPickerView programmatically in a subView with out using Interface Builder?
Hey! U can make UIPickerView programmatically....
In- viewController.m file
- (void)viewDidLoad {
[super viewDidLoad];
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,100,320, 500)];
[self.view addSubview:pickerView];
[pickerView setDelegate:self];
[pickerView release];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return ; //give components here
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
return ; //give rows here
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { return ; // give titles }
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
//Do the step here
}
And in your viewController.h class never forgot to do this
#interface viewController : UIViewController <UIPickerViewDelegate>
Hope this may help u.....for more help follow the link ..
Related
Im really close on this one...For the purposes of this i have two text boxes. when pressed they invoke a ui picker.
text box one returns values 1,2,3,4
text box two returns values A, B, C, D
The problem is that when the picker is presented for text box two it shows the values from text box one, although will select the appropriate row from text box two. Could someone check over my code and tell me where im making an error, thanks.
.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate,UITextFieldDelegate>
{
//decalre picker
UIPickerView *select;
//declare NSArrray
NSArray *arrStatus;
NSArray *arrStatus2;
}
#property (strong, nonatomic) IBOutlet UITextField *text1;
- (IBAction)Value:(id)sender;
#property (strong, nonatomic) IBOutlet UITextField *text2;
#end
.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize text2;
#synthesize text1;
- (void)viewDidLoad
{
select = [[UIPickerView alloc] initWithFrame:CGRectZero];
select.delegate = self;
select.dataSource = self;
[select setShowsSelectionIndicator:YES];
text1.inputView = select;
text2.inputView = select;
arrStatus = [[NSArray alloc] initWithObjects:#"1",#"2",#"3",#"4",nil];
arrStatus2 = [[NSArray alloc] initWithObjects:#"A",#"B",#"C",#"D",nil];
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
//One column
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
//set number of rows
return arrStatus.count;
return arrStatus2.count;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
//set item per row
return [arrStatus objectAtIndex:row];
return [arrStatus2 objectAtIndex:row];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[self setText1:nil];
[self setText2:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (IBAction)Value:(id)sender {
NSInteger selectedRow = [select selectedRowInComponent:0];
if([text1 isFirstResponder])
text1.text = [arrStatus objectAtIndex: selectedRow];
[text1 resignFirstResponder];
if ([text2 isFirstResponder])
text2.text = [arrStatus2 objectAtIndex: selectedRow];
[text2 resignFirstResponder];
}
#end
So to sum up im trying to get, 1 picker view, with 1 colum, 2 arrays, 2 text boxes. Ive tried creating another picker view and that didnt work either
The return statement exits the method. Therefore , if you have 2 return statements , it will never get to the second one. You should modify these methods as such:
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if([text1 isFirstResponder])
return arrStatus.count;
else
return arrStatus2.count;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if([text1 isFirstResponder])
return [arrStatus objectAtIndex:row];
else
return [arrStatus2 objectAtIndex:row];
}
Hope this helps. Cheers!
This is Code Which is able to show picker view with the given array and textfield will have the first data of the UIPickerView.Please let me know if you have any issue.I did it for one textfield for next textfield you just need to figure out using tag of textfield and set the Text.
-(void)viewDidLoad
{
[super viewDidLoad];
arrayNo = [[NSArray alloc] initWithObjects:#"1",#"2",#"3",#"4",nil];
[pickerView selectRow:1 inComponent:0 animated:NO];
self.text1.text= [arrayNo objectAtIndex:[pickerView selectedRowInComponent:0]];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
return 1;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
mlabel.text= [arrayNo objectAtIndex:row];
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
return [arrayNo count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
return [arrayNo objectAtIndex:row];
}
I have a UIPickerView that appears when a textfield is selected:
-(void) showPicker
{
[self.genere resignFirstResponder];
pickerGenero = [[UIPickerView alloc] initWithFrame:CGRectMake(0,215,320,0)];
pickerGenero.delegate = self;
pickerGenero.dataSource = self;
pickerGenero.showsSelectionIndicator = YES;
[self.parentViewController.tabBarController.view addSubview:pickerGenero];
pickerGenero=nil;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [genreArray count];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
self.genere.text =[genreArray objectAtIndex:row];
[self.pickerGenero removeFromSuperview];
}
But is not removed when a row is selected. Textfield is updated with the selected value, but the picker view is not removed. I've tried to use as a trial .sethideen=True but it doesn't work, too.
Many thanks
Actually your's is not the correct way of coding. When you click the textField you just hide the keyboard by using resignFirstResponder and invoking pickerView.Instead of doing like that, you have to add your pickerView as inputView for genere textField. and try this code then
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
self.genere.text =[genreArray objectAtIndex:row];
[genere resignFirstResponder];
}
I got a slight problem, I'm getting a error saying "'UIPickerView' undeclared on line 1 of the below code. I copied right out of the book that I have and I'm not sure whats wrong?
could I get some help? it would be greatly appreciated, thanks :)
this is the entire .m file code.
#import "InstatwitViewController.h"
#implementation InstatwitViewController
- (void)viewDidLoad {
[super viewDidLoad];
activities = [[NSArray alloc] initWithObjects:#"sleeping",
#"eating", #"working", #"thinking", #"crying", #"begging",
#"leaving", #"shopping", #"hello worlding", nil];
feelings = [[NSArray alloc] initWithObjects:#"awesome",
#"sad", #"happy", #"ambivalent", #"nauseous", #"psyched",
#"confused", #"hopeful", #"anxious", nil];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)
pickerView {
return 2;
}
- (NSInteger)PickerView:(UIPickerView *)pickerViewnumberOfRowsInComponent : (NSInteger)component {
if (component == 0) {
return [activities count];
}
else {
return [feelings count];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
switch (component) {
case 0:
return [activities objectAtIndex:row];
case 1:
return [feelings objectAtIndex:row];
}
return nil;
}
}
- (void)dealloc {
[activities release];
[feelings release];
[super dealloc];
}
#end
Where did you copy it? It should be in the #implementation ... #end section of the .m file of the class you are implementing. It should not be in the #interface ... #end part, which is typically in a .h file, or elsewhere, i.e. outside the #implementation ... #end section.
Please show a little more of your code.
Edit
This:
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
lacks a closing } before - (NSString *)pickerView: .... In other words, change it thus:
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
I have been trying for 2 days now to get a UIPickerView to show and I am at a loss. I have implemented all the methods to connect a data source (NSArray) and have no idea why I can't get it to show. The assignment is now due tonight and the professor is less than useless, I've given up asking her.
This is the .m file of the subview containing the UIPickerView. All I need is to figure out why the picker won't display! Help appreciated I'm exasperated....
#import "SpotPicker.h"
#implementation SpotPicker
#synthesize spotPicker;
NSArray *pickerLocations;
NSString *selectedLocation;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;
pickerLocations = [[NSArray alloc]initWithObjects:#"Grand Canyon", #"Mt Rushmore", #"Statue of Liberty", #"Empire State Building", #"Hollywood Sign", #"Lincoln Memorial", #"Space Needle", nil];
}
- (void)viewDidUnload
{
[self setSpotPicker:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)dismissPicker:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
return [pickerLocations count];
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [pickerLocations objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
//NSLog(#"Selected Color: %#. Index of selected color: %i", [pickerLocations objectAtIndex:row], row);
selectedLocation = [pickerLocations objectAtIndex:row];
}
-(NSString *) getLocation {
return selectedLocation;
}
#end
Here's the header file to just to make sure I haven't declared something incorrectly or something stupid
#import <UIKit/UIKit.h>
#interface SpotPicker : UIViewController
- (IBAction)dismissPicker:(id)sender;
-(NSString *) getLocation;
#property (weak, nonatomic) IBOutlet UIPickerView *spotPicker;
#end
You need to declare that your SpotPicker implements the UIPickerViewDelegate and UIPickerViewDataSource protocols:
#interface SpotPicker : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>
and set your UIPickerView's delegate to be your SpotPicker. You could either do this in IB, or programatically:
- (void)viewDidLoad
{
...
self.spotPicker.delegate = self;
self.spotPicker.dataSource = self;
}
I have a TableView with my custom cell class where I have a pickerview.
I want to pass an array to populate pickerview with data. How to pass it. Is my approach correct?
You don't pass anything to a TableViewCell. Instead you have to implement
UITableViewDelegate
UITableViewDatasource
to populate the tableView. And
UIPickerViewDelegate
UIPickerViewDatasource
for the pickers.
I have included pickerview delegate and datasource within table cell and it works fine...
Passing the array is same as to any view.
//////////////ScrollCell.h
#interface ScrollCell : UITableViewCell <UIPickerViewDataSource, UIPickerViewDelegate>{
UILabel *textlabel;
UIPickerView *pickerview;
NSString *textfieldValue;
NSString *scrollerValue;
NSArray *scrollerData;
}
#property (nonatomic,retain)UILabel *textlabel;
#property (nonatomic,retain)UIPickerView *pickerview;
#property (nonatomic,retain)NSArray *scrollerData;
#property (nonatomic,retain)NSString *textfieldValue;
#property (nonatomic,retain)NSString *scrollerValue;
-(NSString *)getTextfiledValue;
-(NSString *)getScrollerValue;
-(void)setScrollerData:(NSArray *)array;
#end
/////////ScrollerCell.m
//
// ScrollCell.m
// MultipleDetailViews
//
// Created by Ruslan Karimov on 12/5/10.
// Copyright 2010 Eventagrate. All rights reserved.
//
#import "ScrollCell.h"
#import "Answers.h"
#implementation ScrollCell
#synthesize textlabel, pickerview, scrollerData, textfieldValue, scrollerValue;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
textlabel = [[UILabel alloc]init];
textlabel.textAlignment = UITextAlignmentLeft;
textlabel.font = [UIFont systemFontOfSize:25];
textlabel.textColor =[UIColor blackColor];
[self.contentView addSubview:textlabel];
pickerview = [[UIPickerView alloc]init];
[self.contentView addSubview:pickerview];
scrollerData = [[NSArray alloc] init];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
CGRect contentRect = self.contentView.bounds;
CGFloat boundsX = contentRect.origin.x;
CGRect frame;
frame= CGRectMake(boundsX+10 ,+10, 300, 25);
textlabel.frame = frame;
//frame= CGRectMake(boundsX+200 ,0, 300, 100);
self.pickerview.frame = CGRectMake(boundsX+200 ,0, 300, 163);
self.pickerview.delegate = self;
//self.pickerview.
}
-(void)setScrollerData:(NSArray *)array
{
//[self.scrollerData arrayByAddingObjectsFromArray:array];
scrollerData = array;
NSLog(#"from scrolltable cell: %i",[self.scrollerData count]);
}
-(NSString *)getTextfiledValue
{
return self.textfieldValue;
}
-(NSString *)getScrollerValue
{
return self.scrollerValue;
}
//PICKER VIEW CONTROL
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
return [scrollerData count];
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
Answers *template = (Answers *)[self.scrollerData objectAtIndex:row];
return template.answer_title;
//return #"fff";
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
}
- (void)dealloc {
[super dealloc];
}
#end