带有 UIActionSheet IOS 8 的 UIPickerView 停止工作

Posted

技术标签:

【中文标题】带有 UIActionSheet IOS 8 的 UIPickerView 停止工作【英文标题】:UIPickerView with UIActionSheet IOS 8 stop working 【发布时间】:2014-09-19 13:10:45 【问题描述】:

ios8 中显示带有 UIActionSheet 的 UIPickerView 不显示

-(void)showPicker

    /************************  FIXED please contact me on nfsarmento@hotmail.com if you need help to fix******//





   actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
   [actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];



    CGRect pickerFrame = CGRectMake(0, 40, 0, 0);

    UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
    pickerView.showsSelectionIndicator = YES;

    switch((uint)currentDelegate)
        case 0:
            pickerView.dataSource = propertyDelegate;
            pickerView.delegate = propertyDelegate;
            [pickerView selectRow:[propertyDelegate index] inComponent:0 animated:NO];
            break;
        
        case 1:
            pickerView.dataSource = regionDelegate;
            pickerView.delegate = regionDelegate;
            [pickerView selectRow:[regionDelegate index] inComponent:0 animated:NO];
            break;
        
        case 2:
            pickerView.dataSource = townDelegate;
            pickerView.delegate = townDelegate;
            [pickerView selectRow:[townDelegate index] inComponent:0 animated:NO];
            break;
        
        case 3:
            pickerView.dataSource = bedDelegate;
            pickerView.delegate = bedDelegate;
            [pickerView selectRow:[bedDelegate index] inComponent:0 animated:NO];
            break;
        
        case 4:
            pickerView.dataSource = bathDelegate;
            pickerView.delegate = bathDelegate;
            [pickerView selectRow:[bathDelegate index] inComponent:0 animated:NO];
            break;
        
        case 5:
            pickerView.dataSource = priceDelegate;
            pickerView.delegate = priceDelegate;
            [pickerView selectRow:[priceDelegate index1] inComponent:0 animated:NO];
            [pickerView selectRow:[priceDelegate index2] inComponent:1 animated:NO];
            break;
        
        case 6:
            pickerView.dataSource = currencyDelegate;
            pickerView.delegate = currencyDelegate;
            [pickerView selectRow:[currencyDelegate index] inComponent:0 animated:NO];
            break;
        

    







    [actionSheet addSubview:pickerView];

    UISegmentedControl *previousButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:NSLocalizedString(@"Previous", nil)]];
    previousButton.momentary = YES; 
    previousButton.frame = CGRectMake(5.0f, 7.0f, 70.0f, 30.0f);
    previousButton.segmentedControlStyle = UISegmentedControlStyleBar;
    previousButton.tintColor = [UIColor blackColor];

    UISegmentedControl *nextButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:NSLocalizedString(@"Next", nil)]];
    nextButton.momentary = YES; 
    nextButton.frame = CGRectMake(80.0f, 7.0f, 70.0f, 30.0f);
    nextButton.segmentedControlStyle = UISegmentedControlStyleBar;
    nextButton.tintColor = [UIColor blackColor];


    UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:NSLocalizedString(@"Done", nil)]];
    closeButton.momentary = YES; 
    closeButton.frame = CGRectMake(240, 7.0f, 70.0f, 30.0f);
    closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
    closeButton.tintColor = [UIColor colorWithRed:0.35f green:0.55f blue:1.5f alpha:1.0f];

    [closeButton addTarget:self action:@selector(hidePicker) forControlEvents:UIControlEventValueChanged];

    [previousButton addTarget:self action:@selector(previousPicker) forControlEvents:UIControlEventValueChanged];

    [nextButton addTarget:self action:@selector(nextPicker) forControlEvents:UIControlEventValueChanged];

    if(currentDelegate > 0)[actionSheet addSubview:previousButton];
    if(currentDelegate < 6)[actionSheet addSubview:nextButton];

    [actionSheet addSubview:closeButton];

    [actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];

    [actionSheet setBounds:CGRectMake(0, 0, 320, 485)];


-(void) nextPicker

    [self hidePicker];

    currentDelegate++;

    [self showPicker];


-(void) previousPicker

    [self hidePicker];
    currentDelegate--;

    [self showPicker];


-(void)hidePicker


    switch ((uint)currentDelegate) 
        case 0:
            [tf_type setText: [propertyDelegate.values objectAtIndex: [propertyDelegate index]]];
            _appDelegate.int_typeV = [propertyDelegate index];
            break;
        
        case 1:
            [tf_region setText: [regionDelegate.values objectAtIndex: [regionDelegate index]]];
            _appDelegate.int_regionV = [regionDelegate index];

            //Reset Town dropdown when a region is picked
            _appDelegate.int_townV = 0;
            townDelegate = [[TownDelegate alloc] init];
            [self.tf_town setText: [townDelegate.values objectAtIndex: [_appDelegate int_townV]]];

            break;
        
        case 2:
            [tf_town setText: [townDelegate.values objectAtIndex: [townDelegate index]]];
            _appDelegate.int_townV = [townDelegate index];
            break;
        
        case 3:
            [tf_numBed setText: [bedDelegate.values objectAtIndex: [bedDelegate index]]];
            _appDelegate.int_numBedV = [bedDelegate index];
            break;
        
        case 4:
            [tf_numBath setText: [bathDelegate.values objectAtIndex: [bathDelegate index]]];
            _appDelegate.int_numBathV = [bathDelegate index];
            break;
        
        case 5:
            NSString * priceString = [[NSString alloc] initWithFormat:@"%@ - %@", 
                                      [priceDelegate.values objectAtIndex: [priceDelegate index1]], 
                                      [priceDelegate.values2 objectAtIndex: [priceDelegate index2]]];
            [tf_price setText:priceString];

            _appDelegate.int_minPriceV = [priceDelegate index1];
            _appDelegate.int_maxPriceV = [priceDelegate index2];
            break;
        
        case 6:
            [tf_currency setText: [currencyDelegate.values objectAtIndex: [currencyDelegate index]]];
            _appDelegate.int_currencyV = [currencyDelegate index];
            break;
        
    

    [popoverController dismissPopoverAnimated:YES];
    popoverController = nil;

    [self dismissActionSheet];

我的选择器视图

#import "PropertyTypeDelegate.h"

@implementation PropertyTypeDelegate
@synthesize values;


-(id)init

    self = [super init];
    [self loadData];

    return self;


-(int)index return index;
-(void) loadData



    NSArray* array = [[NSArray alloc] initWithObjects: 
                      NSLocalizedString(@"No Preference", nil),
                      NSLocalizedString(@"Villa", nil),
                      NSLocalizedString(@"Town House", nil),
                      NSLocalizedString(@"Apartment", nil),
                      NSLocalizedString(@"Retail", nil),
                      NSLocalizedString(@"Labour Camp", nil),
                      NSLocalizedString(@"Office", nil),
                       NSLocalizedString(@"Warehouse", nil),
                      NSLocalizedString(@"Land Residential", nil),
                      NSLocalizedString(@"Hotel apartment", nil),
                      NSLocalizedString(@"Residential Building", nil),

                      nil];

    self.values = array;

    index = 0;






    -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
    return 1;
     

    -  (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
    return [values count];





    -(NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent: (NSInteger)component return [values objectAtIndex:row];


    -(void)pickerView:(UIPickerView *) thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component  
    index  = (int) (NSInteger)row;




    @end 

【问题讨论】:

请参考此链接-***.com/questions/24366437/… 嗨 VijayKumar 为什么我需要这样做?这是一篇不同代码的新帖子 嗨 VijayKumar,你能帮我解决这个问题吗? 【参考方案1】:

上面提供的链接实际上是指Apple Doc,它已删除将子视图添加到UIActionSheet。在您的代码中,您通过将 UIPickerView 添加到 UIActionSheet 来做类似的事情。所以在 iOS8 以后,即使视图被添加到 UIActionSheet,在显示时返回的视图实际上是 nil。

为此,您可以使用 ActionSheetPicker-3.0。 实际上,它不再是UIActionSheet。但看起来完全一样,这就是它在 iOS8 上工作的原因。 如果这回答了您的问题,请告诉我!

【讨论】:

非常感谢您的快速响应,我已经下载了 ActionSheetPicker,这是一个非常聪明的解决方案,但我仍然有点困惑和迷茫,我该如何分配我的意见?谢谢 请 VijayKunar 告诉我如何锻炼这个“pickerView.dataSource = propertyDelegate;pickerView.delegate = propertyDelegate;[pickerView selectRow:[propertyDelegate index] inComponent:0 animated:NO];” on这个 ActionSheetPicker 我已经设法在我的项目中添加了 ActionSheetPicker,我不明白如何在这个例子中添加数据源,请你帮帮我 其实你什么都不用做。使用 ActionSheetStringPicker 执行完成块和取消块要简单得多。要指定数据源,您需要将 NSString 的 dataArray 对象传递给您的上述方法 我该怎么做,请 VijayKumar 帮我解决这个问题

以上是关于带有 UIActionSheet IOS 8 的 UIPickerView 停止工作的主要内容,如果未能解决你的问题,请参考以下文章

更改 UIActionSheet 中项目的文本颜色 - iOS 8

UIActionSheet iOS 8 错误

iOS 8 之后,还能继续使用 UIActionSheet 和 UIAlertView 吗?

UIActionSheet 未在 iOS 8 中显示

iOS 8 在 UIActionSheet 中破坏了 UIPickerView

iOS 8 中 UIAlertView 和 UIActionSheet 河里去了?