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";
}
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];
}
here's my code that I am using now but still getting all kinds of errors:
No visible #interface for 'HomeViewController' declares the selector 'getCount'
And
/Volumes/Lex/HomeViewController.h:12:12: Required for direct or indirect protocol 'UIPickerViewDataSource'
/Volumes/Lexar/HomeViewController.m:15:17: Incomplete implementation
My code (.m file)
- (void)viewDidLoad
{
[super viewDidLoad];
PFUser *currentUser = [PFUser currentUser];
if (currentUser) {
NSLog(#"Current user: %#" , currentUser.username);
}
else {
[self performSegueWithIdentifier:#"showLogin" sender:self];
self.pickerView.dataSource = self;
self.pickerView.delegate = self;
}
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component
{
if ([self getCount] == 0)
return 1;
return [self getCount];
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent: (NSInteger)component reusingView:(UIView *)view {
if ([self getCount] == 0)
return nil;
}
- (IBAction)logout:(id)sender {
[PFUser logOut];
[self performSegueWithIdentifier:#"showLogin" sender:self];
}
#end
And the header
/// .h controller
#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
#interface HomeViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>
#property (strong, nonatomic) IBOutlet UIPickerView *pickerView;
- (IBAction)logout:(id)sender;
#end
As you mentioned in your comment, you are just placing UIPickerView...but for using picker view you need to set Datasource like UITableView and have to implement all #required methods.
In picker view's data source protocol, there are 2 #required methods
// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
So you need to implement above two method in your controller (say MainViewController). and don't forget to set this class as delegate and datasource to UIPicker view as below
in ViewDidLoad of MainViewController
self.yourPickerView.datasource = self;
self.yourPickerView.delegate = self;
I just updated my project to ios6 and discovered that my UIPicker is no longer displaying the data in it. I really don't know what I could do to make sure that it does again. Any suggestions?
AddRoleTVC.h
#import <UIKit/UIKit.h>
#interface AddRoleTVC : UITableViewController <UIPickerViewDataSource, UIPickerViewDelegate> {
IBOutlet UIPickerView *pickerView;
NSMutableArray *arrayColors;
}
#end
AddRoleTVC.m
#import "AddRoleTVC.h"
#import "UIAlertView+error.h"
#import "API.h"
#include <CommonCrypto/CommonDigest.h>
#implementation AddRoleTVC
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
arrayColors = [[NSMutableArray alloc] init];
[arrayColors addObject:#"Red"];
[arrayColors addObject:#"Orange"];
[arrayColors addObject:#"Yellow"];
[arrayColors addObject:#"Green"];
[arrayColors addObject:#"Blue"];
[arrayColors addObject:#"Indigo"];
[arrayColors addObject:#"Violet"];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
return [arrayColors count];
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [arrayColors objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSLog(#"Selected Color: %#. Index of selected color: %i", [arrayColors objectAtIndex:row], row);
}
#end
You used the IBOutlet To Picker View, so You set Delegate and datasource in your .Xib File. It must work.