Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mikezang

macrumors 6502a
Original poster
May 22, 2010
939
41
Tokyo, Japan
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?
Code:
#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
Code:
#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
 
First, where do you set your UIPickerView's delegate?

Second, this delegate method has a typo in the name:
Code:
-(NSString *) [COLOR="Red"]pcikerView[/COLOR]:(UIPickerView *)[COLOR="red"]pcikerView[/COLOR] titleForRow:(NSInteger)row forComponent:(NSInteger)component {
	return [pickerData objectAtIndex:row];
}
 
Here is my settings
 

Attachments

  • SnapShot?2010-07-14 23.49.22?.jpg
    SnapShot?2010-07-14 23.49.22?.jpg
    27.4 KB · Views: 119
  • SnapShot?2010-07-14 23.56.02?.jpg
    SnapShot?2010-07-14 23.56.02?.jpg
    45 KB · Views: 58
It was shown in post #2. Here it is again: (pickerView is spelled wrong)

Code:
-(NSString *) [size=7]p[b]ci[/b]kerView:[/size](UIPickerView *)[size=7]p[b]ci[/b]kerView[/size] titleForRow:(NSInteger)row forComponent:(NSInteger)component {
	return [pickerData objectAtIndex:row];
}
 
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. :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.