I have a UIPickerView that shows up in storyboard but is invisible or at best a colored block if I change the background color when I compile the program. Do I need to populate it with data for it to display, i.e. does it no longer show Cupertino, Mountain View etc. once compiled?
BTW, I am trying to populate picker view with data but still invisible:
code:
in .h file:
#interface pickerVC : UIViewController<UIPickerViewDelegate,UIPickerViewDataSource>
#property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
#property (strong, nonatomic) IBOutlet UILabel *color;
#property (strong, nonatomic) NSArray *myArray;
in viewdidload;
UIPickerView *pickerView;
pickerView.delegate = self;
_myArray = [[NSArray alloc] initWithObjects:#"Blue",#"Green",#"Orange",#"Purple",#"Red",#"Yellow" , nil];
//PICKER METHODS
// tell the picker how many rows are available for a given component
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
// NSUInteger numRows = 3;
return _myArray.count;
}
// tell the picker how many components it will have
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
// tell the picker the title for a given component
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSString *title;
title = _myArray[row];
return title;
}
// tell the picker the width of each row for a given component
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
int sectionWidth = 300;
return sectionWidth;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component{
NSLog(#"Selected Row %d", row);
switch(row){
// Handle the selection
}
yes, you need to implement the data source and picker view delegates.
Yes it is not displaying. You have to set delegate and datasource of picker view and implement delegate methods in your controller.m file.
e.g.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return 10;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return #"ABC";
}
First of all add this
<UIPickerViewDelegate,UIPickerViewDataSource> in your .h file
after that add there Delegate Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return 5;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return #"xyz";
}
Related
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.
Similar to how amazon does for selecting quantity of an item. I want it to then save the number to display in another tableview. Here is the code I have for my .m file. When I click on the text field the picker is empty.
#interface ApparelViewController ()
#property(nonatomic, readonly) NSInteger numberOfComponents;
#end
#implementation ApparelViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIPickerView *picker = [[UIPickerView alloc] init];
self.quantity.inputView = picker;
//creating the arrays for the values in the picker wheel
self.quantity0 = #[#1, #2, #3, #4, #5];
}
//custom picker in text field
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return self.quantity0.count;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow: (NSInteger)row forComponent:(NSInteger)component {
return self.quantity0[row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
self.quantity.text = self.quantity0[row];
[self.quantity resignFirstResponder];
}
Looks like you forgot to set the UIPicker's delegate to self.
In your viewDidLoad, try this:
picker.delegate = self;
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];
I'm still new to iOS coding but am trying to get my UIPicker to display the options I want, and then be able to take the option selected and do something with it. But rather than having the picker show my options it's just showing ? marks instead of my options.
Here's my GuardCompany.h
#import <UIKit/UIKit.h>
#define COLOUR 0
#interface GuardCompany : UIViewController
<UIPickerViewDataSource,UIPickerViewDelegate>{
IBOutlet UIPickerView *ColourAndShadePicker;
NSMutableArray *arrayColour;
}
#end
And here's my GuardCompany.m
#import "GuardCompany.h"
#interface GuardCompany ()
#end
#implementation GuardCompany
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (component == COLOUR)
return [arrayColour count];
return 0;
}
- (NSString *)GuardCompany:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (component == COLOUR)
return [arrayColour objectAtIndex:row];
return 0;
}
#pragma mark - view lifecycle
-(void)viewDidLoad
{
[super viewDidLoad];
arrayColour = [[NSMutableArray alloc] init];
[arrayColour addObject:#"red"];
}
#end
At first glance this looks incorrect:
- (NSString *)GuardCompany:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
Change it to:
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component