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 want to make a Picker View and it will be displayed when I click a button, my code as below, but the picker view show nothing, what can I do?
Code:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 3;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return 20;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return [NSString stringWithFormat:@"%@-%d", component == 1 ? @"R" : @"L", row];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    UIPickerView *pickerView = (UIPickerView *)[actionSheet viewWithTag:101];
    self.title = [NSString stringWithFormat:@"L%d-R%d-L%d", [pickerView selectedRowInComponent:0], [pickerView selectedRowInComponent:1], [pickerView selectedRowInComponent:2]];
    [actionSheet release];
}

- (void)action {
    NSString *title = UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation) ? @"\n\n\n\n\n\n\n\n\n" : @"\n\n\n\n\n\n\n\n\n\n\n\n" ;
	
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:title delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"Set Combo", nil];
    [actionSheet showInView:self.view];
	
    UIPickerView *pickerView = [[[UIPickerView alloc] init] autorelease];
    pickerView.tag = 101;
    pickerView.delegate = self;
    pickerView.dataSource = self;
    pickerView.showsSelectionIndicator = YES;
	
    [actionSheet addSubview:pickerView];
	
    // Peek at dimensions
    CFShow(NSStringFromCGRect(pickerView.frame));
}
 

Attachments

  • SnapShot 2010-10-15 at 22.12.51.jpg
    SnapShot 2010-10-15 at 22.12.51.jpg
    57.6 KB · Views: 164
For anyone finding this thread later, then the code posted by the OP:
  • Fails in iPad (iOS SDK 3.2)
  • Works in iPhone (iOS SDK 4.1)
BTW, making space in a UIActionSheet by including newline's in the title is pretty crazy :D
(Where "crazy" means device-dependent -- I like writing resolution-independent code.)
 
For anyone finding this thread later, then the code posted by the OP:
  • Fails in iPad (iOS SDK 3.2)
  • Works in iPhone (iOS SDK 4.1)
BTW, making space in a UIActionSheet by including newline's in the title is pretty crazy :D
(Where "crazy" means device-dependent -- I like writing resolution-independent code.)
Thanks for you comment. Like you said, it is very stupid way, but I just copied from a book and try it before I can reform it:(:)
 
I wonder why the OP is "hacking" UIActionSheet rather than just using a UIPopoverController.
I think there is no UIPopoverController when that book was published.
I just want to show pickerview and I found that sample.
 
iPhone Developer's Cookbook 2nd edition
Well, then, part of the problem might be that you are running code on a platform that didn't exist when the book was written.

Look at the documentation for an obviously named property (hint you want to set the delegate, what do you expect the property to be called?).
And sorry robbieduncan but the OP actually seems to have wired their delegate correctly, as seen in this line of code from the first post.

Code:
    pickerView.delegate = self;
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.