PDA

View Full Version : My PcikerView only show "?"




mikezang
Jul 14, 2010, 09:21 AM
I made a PickerView as below, but when I run it, I can only view "?" in my PickerView, I am not sure why, can you help me?

#import <UIKit/UIKit.h>
@interface SingleComponentPickerViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> {
UIPickerView *singlePicker;
NSArray *pickerData;
}
@property (nonatomic, retain) IBOutlet UIPickerView *singlePicker;
@property (nonatomic, retain) NSArray *pickerData;
-(IBAction) buttonPressed;
@end


#import "SingleComponentPickerViewController.h"
@implementation SingleComponentPickerViewController
@synthesize singlePicker;
@synthesize pickerData;
-(IBAction) buttonPressed {
NSInteger row = [singlePicker selectedRowInComponent:0];
NSString *selected = [pickerData objectAtIndex:row];
NSString *title = [[NSString alloc] initWithFormat:@"You selected %@!", selected];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:@"Thank you for choosing." delegate:nil cancelButtonTitle:@"You are Welcome" otherButtonTitles:nil];
[alert show];
[alert release];
[title release];
}
typically from a nib.
- (void)viewDidLoad {
NSArray *array = [[NSArray alloc] initWithObjects:@"Luke", @"Leia", @"Han", @"Chewbacca", @"Artoo", @"Threepio", @"Lando", nil];
self.pickerData = array;
[array release];
}
-(NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return [pickerData count];
}
-(NSString *) pcikerView:(UIPickerView *)pcikerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [pickerData objectAtIndex:row];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
self.singlePicker = nil;
self.pickerData = nil;
[super viewDidUnload];
}
- (void)dealloc {
[singlePicker release];
[pickerData release];
[super dealloc];
}
@end



dejo
Jul 14, 2010, 09:32 AM
First, where do you set your UIPickerView's delegate?

Second, this delegate method has a typo in the name:

-(NSString *) pcikerView:(UIPickerView *)pcikerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [pickerData objectAtIndex:row];
}

mikezang
Jul 14, 2010, 09:41 AM
What do you mean "where to set delegate"?
Do you mean in xib file?

dejo
Jul 14, 2010, 09:43 AM
What do you mean "where to set delegate"?
Do you understand what setting a delegate means?

mikezang
Jul 14, 2010, 09:45 AM
I set both delegate and DataSource to UIPickerView in my nib file

dejo
Jul 14, 2010, 09:47 AM
I set both delegate and DataSource to UIPickerView in my nib file
Good. That answers my question then.

mikezang
Jul 14, 2010, 09:51 AM
Here is my settings

dejo
Jul 14, 2010, 10:00 AM
Did you fix your delegate method name typo yet?

mikezang
Jul 14, 2010, 10:27 AM
Did you fix your delegate method name typo yet?

Why do you mean? I am not sure what do you talk....

Can you tell me which name I should modify?

dejo
Jul 14, 2010, 10:30 AM
Why do you mean? I am not sure what do you talk....

Can you tell me which name I should modify?
If my first post in this thread isn't enough info for you, I'm afraid I can't help you. I'm out.

PBG4 Dude
Jul 14, 2010, 10:34 AM
It was shown in post #2. Here it is again: (pickerView is spelled wrong)


-(NSString *) pcikerView:(UIPickerView *)pcikerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [pickerData objectAtIndex:row];
}

mikezang
Jul 14, 2010, 11:19 AM
OH, My god?

Sorry for my miss, now I got what I need.

Thanks for your patient:)

dejo
Jul 14, 2010, 11:51 AM
Thanks for your patient:)
You're welcome. Just remember that precision is an important skill to hone in programming. Also, I try to copy delegate method syntax from right out of the documentation. This helps reduce typos to a large extent. If there is a typo, it's Apple's fault, not mine. :)