I added a UIPickerView to a UITableViewCell. I am adding about 5 entries to it. Now when I try to select a value, the - (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component function is called with the correct row. But on the scrren the UIPickerView "scrolls" back to the first element and does not stick to the selected value.
Anyone had the same problem?
EventSelectCell.h
#class EventSelectCell;
#protocol EventSelectCellDelegate <NSObject>
- (void)selectedEvent:(Event*)selectedEvent;
#end
#interface EventSelectCell : UITableViewCell <UIPickerViewDataSource, UIPickerViewDelegate>
#property (strong, nonatomic) id <EventSelectCellDelegate> delegate;
#property (weak, nonatomic) IBOutlet UIPickerView *selectWheel;
#end
EventSelectCell.m
#import "EventSelectCell.h"
#implementation EventSelectCell
{
EventManager* eventManager;
NSArray* listOfEvents;
}
- (void)awakeFromNib {
self.selectWheel.delegate = self;
self.selectWheel.dataSource = self;
// get the EventManger
eventManager = [EventManager sharedEventManager];
listOfEvents = [eventManager getListOfEvents];
[self.selectWheel reloadAllComponents];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
return [listOfEvents count];
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
Event* selectedEvent = [listOfEvents objectAtIndex:row];
return [selectedEvent name];
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
Event* selectedEvent = [listOfEvents objectAtIndex:row];
[self.delegate selectedEvent:selectedEvent];
}
#end
the delegate
- (void)selectedEvent:(Event*)selectedEvent {
[playersInGame removeAllObjects];
[playersInGame addObjectsFromArray:[self.eventManager getPlayersInEvent:selectedEvent onlyActive:YES]];
[selectedEvent setIsActive:[NSNumber numberWithInt:1]];
activeEvent = selectedEvent;
[self.tableView reloadData];
}
Okay, I found the problem. The UIPickerView is in a UITableViewCell. Whenever someone selected something with the UIPickerView I called [self.tableView reloadData], this caused the table to reload and also initialized the UIPickerView again.
Now I am just reloading a singel section in the table.
I want to use picker view to display 12 digits in it that digit comes dynamic and it has to be light flight schedule display . Digits will be increasing always that i want to make it through picker. As i am new to IOS need your help. Thank you
#import "DetailViewController.h"
#interface DetailViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
#end
#implementation DetailViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIPickerView *pickerview = [[UIPickerView alloc]init];
pickerview.delegate = self;
pickerview.dataSource = self;
[self.view addSubview:pickerview];
}
- (void)pickerView:(UIPickerView *)pV didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return 12;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSNumber *digit = #(row);
return [digit stringValue];
}
#end
You can use Simple Library and Then Add array of Digits .
here are Few Simple Library , try it once.
CPPickerView
ActionSheetPicker-3.0
AKPickerView
So I've followed everything this post Default Picker Value iphone says, but I can't seem to get my picker to work.
Here's my .m snippet:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.arrPercent = #[#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9",#"10",#"11",#"12",#"13",#"14",#"15",#"16",#"17",#"18",#"19",#"20",#"21",#"22",#"23",#"24",#"25",#"26",#"27",#"28",#"29",#"30",#"31",#"32",#"33",#"34",#"35",#"36",#"37",#"38",#"39",#"40",#"41",#"42",#"43",#"44",#"45",#"46",#"47",#"48",#"49",#"50"];
self.arrPeople = #[#"1",#"2",#"3",#"4",#"5",#"6",#"7",#"8",#"9",#"10",#"11",#"12",#"13",#"14",#"15",#"16",#"17",#"18",#"19",#"20"];
self.percent = [NSNumber numberWithInt:15];
self.people = [NSNumber numberWithInt:5];
self.strSubTotal = #"";
self.myPicker.dataSource = self;
self.myPicker.delegate = self;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
//[self.myPicker selectRow:self.percent.integerValue inComponent:0 animated:YES];
[self.myPicker selectRow:5 inComponent:0 animated:YES];
[self.myPicker selectRow:5 inComponent:1 animated:YES];
[self.myPicker reloadAllComponents]; // tried commenting out this line with no luck
}
Here's my .h snippet:
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#interface ViewController : UIViewController <UIPickerViewDataSource,UIPickerViewDelegate>
#property (nonatomic,strong) NSArray *arrPercent;
#property (nonatomic,strong) NSArray *arrPeople;
#property (strong, nonatomic) IBOutlet UIPickerView *myPicker;
#property (strong, nonatomic) NSString *strSubTotal;
#property (strong, nonatomic) NSNumber *percent;
#property (strong, nonatomic) NSNumber *people;
#end
Here's the delegate stuff, I think...
// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component
{
if(component== 0)
{
return [self.arrPercent count];
}
else
{
return [self.arrPeople count];
}
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if(component == 0)
{
return [NSString stringWithFormat:#"%#%#", [self.arrPercent objectAtIndex:row], #"%"];
}
else
{
return [self.arrPeople objectAtIndex:row];
}
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if(component == 0)
{
self.percent = [self.arrPercent objectAtIndex:row];
}
else
{
self.people = [self.arrPeople objectAtIndex:row];
}
[self updateSubTotal:-3];
}
When I start the emulator, the picker doesn't animate to the position I tell it to. Am I doing anything wrong? Please help!
As it turns out, the error was that for some reason, the connection between the picker in the UI was disconnected with the code. I had to hold the Control key while dragging the picker from the UI to the .h file.
I saw this because I noticed a little "empty button icon" next to the method declaration in the .h file. Once Xcode understood that the picker is controlled by the piece of code, everything works as expected.
Thank you so much!
how to change the language in IOS using UIPicker?
Languages: English, Chinese (Simplified), Bahasa Malaysia
When user selects Chinese (Simplified) it changes the contents of the labels based on languages above.
here's the code
ChangeLanguage.h
#interface ChangeLanguageViewController :UIViewController<UIPickerViewDataSource UIPickerViewDelegate>
#property (nonatomic, strong)IBOutlet UIPickerView *myPickerView;
#oroperty (nonatomic, strong)NSArray *languageArray;
#import "ChangeLanguageViewController.h"
#interface ChangeLanguageViewController ()
#end
- (void)viewDidLoad
{
myPickerView.delegate = self;
myPickerView.dataSource = self;
languageArray = [[NSArray alloc] initWithObjects:#"English", #"Chinese (Simplified)", #"Bahasa
Malaysia", nil];
#pragma mark - UIPickerView Delegate
//return the number of columns to display
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
//returns the number of rows in each component.
-(NSInteger)pickerView:(UIPicker *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [languageArray count];
#pragma mark -UIPickerView Delegate
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
return 30.0;
}
-(NSString *)pickerView:(UIPicker *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [languageArray objectAtIndex:row];
}
I am trying to create a PickerView view programatically.
I have followed all of the necessary steps however the picker just simply appears and then disappears straight away. No data can be seen.
I am setting the delegate and data source and I have implemented the necessary methods.
Here is my code:
in .h
#interface NJATimeEntryViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>
#property (nonatomic, strong) UIPickerView *customerPicker;
#property (nonatomic, strong) NSArray *pickerTitles;
#end
in .m
- (void)viewDidLoad
{
[super viewDidLoad];
self.pickerTitles = #[#"ONE", #"TWO", #"THREE"];
self.customerPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, 320, 200)];
self.customerPicker.dataSource = self;
self.customerPicker.delegate = self;
[self.view addSubview:self.customerPicker];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return self.pickerTitles.count;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [[self pickerTitles] objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSLog(#"Selected Row %d", row);
}
All help is appreciated thanks.
FIXED:
All i did was set the background color of the view controllers view and now it works. I see this as a bug as no where is it stated that the background color has to be set.
Set Some Background color ( except black )of your view, then try,
self.view.backgroundColor=[UIColor yellowColor];