Getting a "?" when populating my pickerview on load - iphone

I've searched here, but didn't see nothing like this error.
I'm trying to populate my pickerview on ViewDidLoad, but when I run my program, the pickerView is just populated with "?" instead my strings... what's going on here? Thanks in advance.
- (void)viewDidLoad {
[super viewDidLoad];
activities = [[NSArray alloc] initWithObjects:#"sleeping", #"eating", #"working", #"thinking", #"crying", #"begging", #"leaving", #"shopping", #"hello worlding", nil];
feelings = [[NSArray alloc] initWithObjects:#"awsome",#"sad",#"happy",#"ambivalent",#"nauseous",#"psyched",#"confused",#"hopeful",#"anxious",nil];
}

You need to implement the UIPickerViewDelegate and UIPickerViewDataSource protocols:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component == 0)
return [activities count];
else
return [feelings count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (component == 0)
return [activities objectAtIndex:row];
else
return [feelings objectAtIndex:row];
}

Related

How to make my UIPickerView display all elements of an array

I have a UIPickerView and an array though I can't seem to be able to input all the date from the array into the UIPicker.
I know that the syntax is meant to be like this:
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [treatments objectAtIndex:row];
}
Where treatments is the array name though when I use this it comes up with this error:
-[Treatment isEqualToString:]: unrecognized
I have searched my entire project and can't find the phrase: isEqualToString
Treatment.h file:
#import <Foundation/Foundation.h>
#interface Treatment : NSObject {
NSString *treatmentid;
NSString *treatmentName;
NSString *treatmentPrice;
}
#property (nonatomic, strong) NSString *treatmentid;
#property (nonatomic, strong) NSString *treatmentName;
#property (nonatomic, strong) NSString *treatmentPrice;
#end
All picker codes:
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return [treatments count];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [treatments objectAtIndex:row];
}
If you need any more code just say
Thanks in advance
you your Array contain Dictionar or NSMutableDictionar then you must specify value of array with it's key like bellow try to writtern you method like this:
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return[[treatments objectAtIndex:row]valueForKey:#"YourKey"];
}
Here is basic Implementation of Picker view sample example code:-
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thepickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thepickerView numberOfRowsInComponent:(NSInteger)component
{
return [treatments count];
}
customize displaying lable of UIpickerview like bellow:-
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 37)];
if (component == 0) {
label.font=[UIFont boldSystemFontOfSize:22];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.text = [NSString stringWithFormat:#"%d", row];
label.font=[UIFont boldSystemFontOfSize:22];
NSLog(#"%#",[yourpickerview selectedRowInComponent:component]);
}
return label;
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSLog(#"%#",[treatments objectAtIndex:row]);
}

Double picker view

Hello im working with a double picker view with images, one side text and the other images, but I cant make it work. Here is the picker components. There are 3 arrays, Array and Array1 content text, and ImagesArray content images. X its a variable that depends on what button you pressed. Thanks.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component;
{
if (X == 1) {
if (component == 0) {
return [self.Array count];
}
if (component == 1)
{
return [self.ImagesArray count];
}
}
if (X == 2) {
if (component == 0) {
return [self.Array1 count];
}
if (component == 1) {
return [self.ImagesArray count];
}
}
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
if (component == 1){
return [self.ImagesArray objectAtIndex:row];
}
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
if (X == 1){
if (component == 0)
{
return [self.Array objectAtIndex:row];
}
}
if (X == 2) {
if (component == 0){
return [self.Array1 objectAtIndex:row];
}
}
}
Let me point out a few issues with your code.
You have a ; at the end of your numberOfRows method before starting the method block. That won't work.
You are using capitalized first letters for instance variables - bad idea.
You are not using else clauses, causing tests to be run when they are no longer necessary.
The way you nest your if statements also creates repetitive code.
You are probably not seeing any images, because of what is in your images array. This array, the way you are using it, should contain UIImageViews. However, this would defeat the purpose of reusable views. Rather, you should have an array with images or images names and create the view only if necessary.
- (UIView *)pickerView:(UIPickerView *)pickerView
viewForRow:(NSInteger)row
forComponent:(NSInteger)component
reusingView:(UIView *)view{
if (component!=1) return nil;
UIImageView *imageView = nil;
UIImage *image = [UIImage imageNamed:[self.imagesArray objectAtIndex:row]];
if (view==nil) {
imageView = [[UIImageView alloc] initWithImage:image];
}
else {
imageView = (UIImageView *)view;
imageView.image = image;
}
view = imageView;
return view;
}
If it still does not work, please be more specific about what is not working.

UIDatePicker non-continuous minutes

I set my UIDatepicker timeInterval to 30 mins, so it shows it as 30,60 (repeating). I really don't want it to repeat, just show 2 options (like AM/PM). Is there a way to do this without creating it from scratch?
Thanks!
Why don't you use UIPicker instead?
UIPickerView *pick=[[UIPickerView alloc] initWithFrame:CGRectMake(300, datep.frame.origin.y, datep.frame.size.width, datep.frame.size.height)];
[pick setDelegate:self];
[pick setDataSource:self];
[pick setShowsSelectionIndicator:YES];
[self.view addSubview:pick];
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 3;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
if(component==0){
return 12;
}else{
return 2;
}
return 2;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
if(component==1){
if(row==1)
return #"30";
else
return #"00";
}
else if(component==0){
return [NSString stringWithFormat:#"%d", row+1];
}else{
if(row==1)
return #"AM";
else
return #"PM";
}
}
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
return 50;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
return 50;
}

UIPickerView only displays one component; should display two

Have a UIPicker which should display two components (columns in the picker view), but is only displaying one.
Can't find the error; it builds correctly. Second component is empty; no data displayed.
InstaEmailViewController.h
#import <UIKit/UIKit.h>
#interface InstaEmailViewController : UIViewController
<UIPickerViewDataSource, UIPickerViewDelegate> {
NSArray* activities_ ;
NSArray* feelings_ ;
}
InstaEmailViewController.m
#import "InstaEmailViewController.h"
#implementation InstaEmailViewController
- (void)dealloc
{
[activities_ release];
[feelings_ release];
[super dealloc];
}
- (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
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
activities_ = [[NSArray alloc] initWithObjects:#"sleeping", #"working", #"thinking", #"crying", #"begging", #"leaving", #"shopping", #"hello worlding", nil];
feelings_ = [[NSArray alloc] initWithObjects: #"awesome", #"sad", #"ambivalent", #"nauseous",#"psyched", #"confused", #"hopeful", #"anxious", nil];
[super viewDidLoad];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (component == 0) {
return [activities_ objectAtIndex:row];
} else {
[feelings_ objectAtIndex:row];
}
return nil;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component {
if (component ==0) {
return [activities_ count];
} else {
return [feelings_ count];
}
}
#end
You are not returning the value in the pickerView:titleForRow:inComponent: method for the second component.
if (component == 0) {
return [activities_ objectAtIndex:row];
} else {
[feelings_ objectAtIndex:row];
}
You should do
return [feelings_ objectAtIndex:row];

How to give this picker view a datasource

Here I am adding a pickerview programaticly
- (void)viewDidLoad {
[super viewDidLoad];
CGRect pickerFrame = CGRectMake(0,280,321,200);
UIPickerView *myPickerView = [[UIPickerView alloc] init]; //Not sure if this is the proper allocation/initialization procedure
//Set up the picker view as you need it
//Set up the display frame
myPickerView.frame = pickerFrame; //I recommend using IB just to get the proper width/height dimensions
//Add the picker to the view
[self.view addSubview:myPickerView];
}
But now I need to actually have it display content and somehow find out when it changes and what value it has changed to. How do I do this?
in .h file place this code
#interface RootVC : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>
assign the datasource and delegate to the picker
// this view controller is the data source and delegate
myPickerView.delegate = self;
myPickerView.dataSource = self;
use the following delegate and datasouce methods
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
return 200;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
return 50;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString *returnStr = #"";
if (pickerView == myPickerView)
{
returnStr = [[levelPickerViewArray objectAtIndex:row] objectForKey:#"nodeContent"];
}
return returnStr;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (pickerView == myPickerView)
{
return [levelPickerViewArray count];
}
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
You need to create a class that implements the UIPickerViewDataSource protocol and assign an instance of it to myPickerView.dataSource.