0

我的下面的代码在 iOS 7 下运行良好 我试图在警报中添加选择器视图,在 ios7 版本下运行良好但在 ios 7 中它显示没有选择器视图的白色警报。

    -(void)showPinPickerAlert
    {
        numberarr = [[NSMutableArray alloc]initWithObjects:@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9", nil];


        UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"Enter Pin To Change Track me Option" message:@"\n\n\n\n\n\n\n" delegate:self cancelButtonTitle:@"Verify" otherButtonTitles:nil];

        UIPickerView *picker=[[UIPickerView alloc]initWithFrame:CGRectMake(25, 30, 230, 60) ];
        picker.dataSource=self;
        picker.delegate=self;
       // picker.backgroundColor=[UIColor blueColor];


        picker.showsSelectionIndicator = YES;
        //  picker.autoresizingMask = UIViewAutoresizingFlexibleHeight;
        picker.transform = CGAffineTransformMakeScale(0.6, 0.6);
        alert.tag=100;
        //  picker.transform = CGAffineTransformMakeScale(1, 0.2);
        [alert addSubview:picker];


        [alert show];

    }

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

{

    return 4;

}



- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

{
    strPin=[[NSString stringWithFormat:@"%i%i%i%i",[pickerView selectedRowInComponent:0],[pickerView selectedRowInComponent:1],[pickerView selectedRowInComponent:2],[pickerView selectedRowInComponent:3]]mutableCopy];

    NSLog(@"strPin=%@",strPin);
    // mlabel.text= [arrayNo objectAtIndex:row];

}



- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;

{

    return [numberarr count];

}

**//code try to change color of picker text and background thats work fine in below ios7 but in iOS 7 still shows white screen**


- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *label = (UILabel*) view;
    if (label == nil)
    {
        label = [[UILabel alloc] init];
    }

    //[label setText:@"Whatever"];


    // This part just colorizes everything, since you asked about that.

    [label setTextColor:[UIColor whiteColor]];
    [label setBackgroundColor:[UIColor blackColor]];
    CGSize rowSize = [pickerView rowSizeForComponent:component];
    CGRect labelRect = CGRectMake (0, 0, rowSize.width, rowSize.height);
    [label setFrame:labelRect];

    return label;
}


- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;

{

    pickerView.backgroundColor=[UIColor blackColor];
    return [numberarr objectAtIndex:row];

}

输出在下面 在此处输入图像描述

IOS 7 中的输出 在此处输入图像描述

如何解决?

4

2 回答 2

2

UIAlertView从 iOS7 开始,不再支持向 a 添加子视图。

您应该实现自己的视图并尝试与警报视图类似,或者像其他人说的那样,使用像这样的第 3 方警报视图:

https://github.com/wimagguc/ios-custom-alertview

于 2013-10-07T13:01:39.953 回答
0

以下大部分代码来自 ios7 之前的堆栈溢出答案。

PickerPrompt.h

#import <UIKit/UIKit.h>

@interface PickerPrompt : UIAlertView <UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate>
{
    UIPickerView *_pickerView;
    NSMutableArray *_options;
}

@property (readonly) NSString *enteredText;

- (id)initWithTitle:(NSString *)title message:(NSString *)message options:(NSMutableArray*)options delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle okButtonTitle:(NSString *)okButtonTitle;

@end

选择器提示.m

#import "PickerPrompt.h"

@implementation PickerPrompt
#define VIEW_TAG 49
#define SUB_LABEL_TAG 52
#define LABEL_TAG 53

#define COMPONENT_WIDTH 250
#define LABEL_WIDTH 10

@synthesize enteredText;

- (id)initWithTitle:(NSString *)title message:(NSString *)message options:(NSMutableArray*)options delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle okButtonTitle:(NSString *)okayButtonTitle
{

    if (self = [super initWithTitle:title message:message delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:okayButtonTitle, nil])
    {
        _options = options;

        _pickerView = [[UIPickerView alloc] init];
        [_pickerView sizeToFit];
        [_pickerView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
        [_pickerView setDelegate:self];
        [_pickerView setDataSource:self];
        [_pickerView setShowsSelectionIndicator:TRUE];

        // Change from pre iOS 7
        [self setAlertViewStyle:UIAlertViewStylePlainTextInput];
        [[self textFieldAtIndex:0] setDelegate:self];
        [[self textFieldAtIndex:0] setInputView:_pickerView];
        [[self textFieldAtIndex:0] becomeFirstResponder];
    }
    return self;
}

#pragma mark -
#pragma mark Picker delegate

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

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (pickerView == _pickerView) {
        return [_options count];
    }
    return [_options count];
}

- (UIView *)labelCellWithWidth:(CGFloat)width rightOffset:(CGFloat)offset {

    // Create a new view that contains a label offset from the right.
    CGRect frame = CGRectMake(0.0, 0.0, width, 32.0);
    UIView *view = [[[UIView alloc] initWithFrame:frame] autorelease];
    view.tag = VIEW_TAG;

    frame.size.width = width - offset;
    UILabel *subLabel = [[UILabel alloc] initWithFrame:frame];
    subLabel.textAlignment = UITextAlignmentRight;
    subLabel.backgroundColor = [UIColor clearColor];
    subLabel.font = [UIFont systemFontOfSize:24.0];
    subLabel.userInteractionEnabled = NO;

    subLabel.tag = SUB_LABEL_TAG;

    [view addSubview:subLabel];
    [subLabel release];
    return view;
}


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    NSString *fullString = [[textField text] stringByAppendingString:string];

    for (NSString* object in _options) {
        if ([object isEqualToString:fullString]) {
            return YES;
        }
    }

    return NO;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {

    UIView *returnView = nil;

    if ((view.tag == VIEW_TAG) || (view.tag == LABEL_TAG)) {
        returnView = view;
    }
    else {
        returnView = [self labelCellWithWidth:COMPONENT_WIDTH rightOffset:LABEL_WIDTH];
    }

    // The text shown in the component is just the number of the component.
    NSString *text = [_options objectAtIndex:row];

    // Where to set the text in depends on what sort of view it is.
    UILabel *theLabel = nil;
    if (returnView.tag == VIEW_TAG) {
        theLabel = (UILabel *)[returnView viewWithTag:SUB_LABEL_TAG];
    }
    else {
        theLabel = (UILabel *)returnView;
    }

    theLabel.text = text;
    return returnView;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    return COMPONENT_WIDTH;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    [[self textFieldAtIndex:0] setText:[_options objectAtIndex:row]];
}

- (NSString *)enteredText
{
    return [[self textFieldAtIndex:0] text];
}

@end

如何使用(从警报视图委托调用):

PickerPrompt *prompt = [PickerPrompt alloc];

NSMutableArray *options = [[NSMutableArray alloc] initWithObjects:@"option 1", @"option 2", nil];

prompt = [prompt initWithTitle:@"Select Option" message:@"Select Option" options:options delegate:self cancelButtonTitle:@"Cancel" okButtonTitle:@"Okay"];

[prompt show];
[prompt release]; 
于 2014-02-27T22:49:02.027 回答