Why does my Picker View show nothing?

Discussion in 'iOS Programming' started by mikezang, Oct 15, 2010.

  1. mikezang macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #1
    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));
    }
    
     

    Attached Files:

  2. vocaro macrumors regular

    Joined:
    Mar 5, 2004
    #2
    Did you forget to wire up the picker view delegate in Interface Builder?
     
  3. mikezang thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #3
    This picker view is created in programming, not in IB, how can I wire up them?
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    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?).
     
  5. mikezang thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #5
  6. ianray macrumors 6502

    Joined:
    Jun 22, 2010
    Location:
    @
    #6
    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.)
     
  7. mikezang thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #7
    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:(:)
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    I wonder why the OP is "hacking" UIActionSheet rather than just using a UIPopoverController.
     
  9. mikezang thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #9
    I think there is no UIPopoverController when that book was published.
    I just want to show pickerview and I found that sample.
     
  10. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    What book? First mention of some book.
     
  11. mikezang thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #11
    iPhone Developer's Cookbook 2nd edition
     
  12. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #12
    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.

    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.

     

Share This Page