0

在我开始之前只有几点:我刚刚接管了这段代码我对 iOS 代码几乎没有经验认为我是一个菜鸟,所以对我来说放轻松:P

我在下面有一些代码,用于显示UIActionSheet. 在 iOS 8 中 UIActionSheet 已被删除(感谢苹果!)所以我开始改变它来使用UIAlertController......我遇到了很多我已经解决的问题,现在我完全困惑了,需要你们有经验的帮助......!

这是一个截图 在此处输入图像描述

现在您可以看到UIAlertController正在显示,但 UIView 的位置不正确,因此无法使用控件!您可以按预期拖动数字,但这并不容易......

如您所见,共有三个控制器,一个用于选择日期时间,一个用于乘客数量,另一个用于选择车辆类型。

我只需要一个可以在所有 iOS8 手机上运行的简单的修复程序,它可能有点狡猾,因为我们正要雇人重新编写整个代码,因为代码总体上很糟糕,我们想从自下而上。

如果您想了解更多信息,请告诉我,我的 iOS 编码经验非常少(我确实想学习,因为我喜欢它,尽管不如 Android)

else if (selectedTableField == travelTimeText || selectedTableField == noPassengers || selectedTableField == carType) {

            // setup actionsheet to contain the UIPicker

            if (version < 8)
            {
            actionSheet = [[UIActionSheet alloc] initWithTitle:@"            "
                                                      delegate:self
                                             cancelButtonTitle:nil
                                        destructiveButtonTitle:nil
                                             otherButtonTitles:nil];

            [actionSheet showInView:self.view];
            }

            if (version >= 8)
            {
                alertController = [UIAlertController alertControllerWithTitle:@"title"
                                                                    message:@"messages"
                                                             preferredStyle:UIAlertControllerStyleAlert];
            }

            //CGRect actionSheetBounds = CGRectMake(0,0,320 + offset, 570);

            CGRect actionSheetBounds;

            if (selectedTableField == travelTimeText)
               // actionSheetBounds = CGRectMake(0,0,320 + offset, 655);

            if (version >= 8)
            {
                [alertController.view setBounds:actionSheetBounds];
            }
            else
            {
                [actionSheet setBounds:actionSheetBounds];
            }

            UIView *bgView = [[UIView alloc] initWithFrame:actionSheetBounds];

            if (version < 7)
                bgView.backgroundColor = [[[ConfigManager sharedInstance].skin valueForKey:@"popupBackgroundColour"] toUIColor];

            UIButton *doneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            // swap asap and select around if on travel time picker
            if (selectedTableField == travelTimeText) {
                doneBtn.frame = CGRectMake(10+bOffset, 305, 300, 40);
            } else {
                doneBtn.frame = CGRectMake(10+bOffset, 250, 300, 40);
            }
            [doneBtn addTarget:self action:@selector(pickerDone:) forControlEvents:UIControlEventTouchUpInside];
            [doneBtn setBackgroundImage:[UIImage imageNamed:@"button_large.png"] forState:UIControlStateNormal];
            [doneBtn setTitle:@"Select" forState:UIControlStateNormal];
            doneBtn.titleLabel.font = [UIFont boldSystemFontOfSize:19];
            doneBtn.titleLabel.textColor = [[[ConfigManager sharedInstance].skin valueForKey:@"popupButtonFontColour"] toUIColor];
            [bgView addSubview:doneBtn];

            UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320 + offset, 20)];
            title.font = [UIFont boldSystemFontOfSize:12];
            title.backgroundColor = [UIColor clearColor];
            title.textAlignment = UITextAlignmentCenter;
            title.textColor = [[[ConfigManager sharedInstance].skin valueForKey:@"popupLabelFontColour"] toUIColor];

            if (selectedTableField == travelTimeText) {
                title.text = @"Booking Time";

                UIButton *asapBtn = [UIButton buttonWithType:UIButtonTypeCustom];
                asapBtn.frame = CGRectMake(10 +bOffset, 250, 300, 40);
                [asapBtn addTarget:self action:@selector(pickerAsap:) forControlEvents:UIControlEventTouchUpInside];
                [asapBtn setTitle:@"As soon as possible" forState:UIControlStateNormal];
                [asapBtn setBackgroundImage:[UIImage imageNamed:@"button_large.png"] forState:UIControlStateNormal];
                asapBtn.titleLabel.font = [UIFont boldSystemFontOfSize:19];
                asapBtn.titleLabel.textColor = [[[ConfigManager sharedInstance].skin valueForKey:@"popupButtonFontColour"] toUIColor];
                [bgView addSubview:asapBtn];
            } else if (selectedTableField == noPassengers)
                title.text = @"Passengers";
            else
                title.text = @"Vehicle";

            [bgView addSubview:title];

            [title release];
            if (version < 8)
            {
            [actionSheet addSubview:bgView];
            }
            else
            {
                [alertController.view addSubview:bgView];
            }
            [bgView release];

            // travel time uses a different picker
            if (selectedTableField == travelTimeText) {
                UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0 + bOffset, 20, 0.0, 0.0)];

                [datePicker addTarget:self action:@selector(travelTimeChanged:) forControlEvents:UIControlEventValueChanged];
                datePicker.datePickerMode = UIDatePickerModeDateAndTime;
                datePicker.minuteInterval = 5;
                if (travelTime == nil)
                    datePicker.date = [[NSDate date] addTimeInterval:15*60];
                else
                    datePicker.date = travelTime;

                if (version < 8)
                {
                [actionSheet addSubview:datePicker];
                }
                else
                {
                    [alertController.view addSubview:datePicker];
                }

                // update table field to show now
                [self travelTimeChanged:datePicker];

                [datePicker release];

                if (version >= 8)
                {
                    [self presentViewController:alertController animated:NO completion:nil];
                }

            } else {
                UIPickerView *picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0 + bOffset, 20, 0.0, 0.0)];
                picker.dataSource = self;
                picker.delegate = self;
                if (selectedTableField == noPassengers) {
                    picker.showsSelectionIndicator = YES;
                    [picker selectRow:[passengerOptions indexOfObject:noPassengers.text] inComponent:0 animated:NO];
                } else if (selectedVehicle != nil) {
                    [picker selectRow:[filteredCarOptions indexOfObject:selectedVehicle] inComponent:0 animated:NO];
                }
                if (version < 8)
                {
                [actionSheet addSubview:picker];
                }
                else
                {
                    [alertController.view addSubview:picker];
                }

                [picker release];

                if (version >= 8)
                {
                    [self presentViewController:alertController animated:NO completion:nil];
                }
            }

        } else if (selectedTableField == bookButton) {
            // validate the form
            [self validateForm];
        }
4

1 回答 1

0

我也在努力解决同样的问题。我需要在 UIAlertController 中有一个 UIPickerView。经过几个小时的调查,我发现无法自定义 UIAlertController 的视图。事实上,它是明确写在苹果文档上的。

UIAlertController 类旨在按原样使用,不支持子类化。

此类的视图层次结构是私有的,不得修改。

顺便说一句,您可以尝试替代解决方案 - uipickerview-programatic-example

于 2017-08-24T09:20:11.603 回答