3

以下代码在 iPhone 上运行良好,但在 iPad 上却不行。只得到一条细线而不是全视图。一直在研究,也许与选择器的矩形有关?所有尺寸数字似乎都是合理的。

    actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil
    cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
    [actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
    CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
    if ( isipad ) pickerFrame = CGRectMake(0, 345, 400, 216);
    pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
    pickerView.showsSelectionIndicator = YES;
    pickerView.dataSource = self;
    pickerView.delegate = self;
    [actionSheet addSubview:pickerView];
    [pickerView release];
    UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray         arrayWithObject:@"Select"]];
    closeButton.momentary = YES; 
    closeButton.frame = CGRectMake(10, 7.0f, 100.0f, 30.0f);
    closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
    closeButton.tintColor = [UIColor blackColor];
    [closeButton addTarget:self action:@selector(dismissActionSheet:)  forControlEvents:UIControlEventValueChanged];
    [actionSheet addSubview:closeButton];
    [closeButton release];
    UISegmentedControl *skipButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Skip Birth Year"]];
    skipButton.momentary = YES; 
    skipButton.frame = CGRectMake(200, 7.0f, 100.0f, 30.0f);
    skipButton.segmentedControlStyle = UISegmentedControlStyleBar;
    skipButton.tintColor = [UIColor blackColor];
    [skipButton addTarget:self action:@selector(skipActionSheet:)   forControlEvents:UIControlEventValueChanged];
    [actionSheet addSubview:skipButton];
    [skipButton release];
    [actionSheet setBounds:CGRectMake(0, 0, self.view.frame.size.width, 216)];
    [actionSheet showInView:self.view];
    [actionSheet setBounds:CGRectMake(0, 0, self.view.frame.size.width, 216)];
4

4 回答 4

4

好吧,通过把大部分代码放进去让它工作

    - (void)willPresentActionSheet:(UIActionSheet *)actionSheet

包含

    pickerFrame = CGRectMake(0, 0, 320, 214);

    initWithTitle:@"\n\n\n\n\n\n\n\n\n\n\n\n\n\n" 

为行动表。我猜是魔术。

于 2011-07-29T19:54:34.127 回答
2

它是否正确:

CGRect pickerFrame = CGRectMake(0, 40, 0, 0); //This line sets the frame height and width equal to 0.
if ( isipad ) pickerFrame = CGRectMake(0, 345, 400, 216); 

你的isipad布尔工作正常吗?

于 2011-07-28T21:08:28.023 回答
0

感谢您的帖子 ort11。我需要制作一个 iPad 应用程序,我从 iPhone 应用程序复制粘贴代码,在 iPad 上是一样的:一小行 :)

我的需求只是语言选择器,没有别的。

以下是基于您的答案的代码:

- (IBAction)btLanguageChooserPressed {
    NSLog(@"btLanguageChooserPressed");


    [self createLanguageSheet];

    // show the sheet: will be invoked the willPresentActionSheet method
    [sheet showInView:self.view];
}

- (void)createLanguageSheet {
    NSLog(@"createLanguageSheet()");

    // hoolly crap, need to initialize the title with: @"\n\n\n\n\n\n\n\n\n\n\n\n\n\n" to get working!!!
    sheet  = [[UIActionSheet alloc] initWithTitle:@"\n\n\n\n\n\n\n\n\n\n\n\n\n\n" 
                                         delegate:self 
                                cancelButtonTitle:nil
                           destructiveButtonTitle:nil
                                otherButtonTitles:nil];

    [sheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];    

}
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
    CGRect pickerFrame =  CGRectMake(0, 0, 320, 216);

    pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
    pickerView.showsSelectionIndicator = YES;
    pickerView.dataSource = self;
    pickerView.delegate = self;

    [sheet addSubview:pickerView];


    // add close button:       
    UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray         arrayWithObject:@"OK"]];
    closeButton.momentary = YES; 
    closeButton.frame = CGRectMake(120, 12, 100, 30);
    closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
    closeButton.tintColor = [UIColor blackColor];
    [closeButton addTarget:self action:@selector(dismissSheet:)  forControlEvents:UIControlEventValueChanged];
    [sheet addSubview:closeButton];

    [sheet setBounds:CGRectMake(0, 0, self.view.frame.size.width, 216)];



    //[sheet showInView:self.view];//already called, why need again?
    //[sheet setBounds:CGRectMake(0, 0, self.view.frame.size.width, 216)];

}

在运行时我在一个变量中做一个缓存:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    NSLog(@"Selected: %@", [arrayLanguages objectAtIndex:row])  ;
    selectedRow = row;
}

这是完成:

- (void)dismissSheet:(id)sender {
    [sheet dismissWithClickedButtonIndex:0 animated:YES];    
    NSString* selectedLanguage = [arrayLanguages objectAtIndex:selectedRow];
    NSLog(@"dismissSheet() selectedLanguage: %@",selectedLanguage);


    switch (selectedRow) {
        case 0://English
            imgCurrentLanguage.image = [UIImage imageNamed:@"english.png"];
            break;
        case 1:// Deutsch

            break;
        case 2://Français

            break;
        case 3://Italiano

            break;
        case 4://Español

            break;

        default:
            break;
    }
    // TODO: do the changes in GUI

}

我无法将此操作表从屏幕中心移动到左下角。但我正在努力...

于 2012-04-04T14:08:16.493 回答
0

我认为这将是最干净的解决方案:

@interface MyActionSheet : UIActionSheet

@property (nonatomic, strong) IBOutlet UIView * customView;

@end

@implementation MyActionSheet

-(CGRect)bounds {
    return self.customView.bounds;
}

@end

然后:

MyActionSheet * s = [[MyActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
s.customView = _auxView;
[s addSubview:_auxView];
于 2013-08-21T22:13:55.030 回答