2013-10-07 57 views
0

我下面的代碼不可見做工精細下面的iOS 7 我想添加選擇器視圖的警報,在下面ios7版本工作正常,但在IOS 7它呈現白色警報沒有選擇器視圖。選擇器在AlertView在IOS7

-(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]; 

} 

輸出下面 enter image description here

輸出在IOS 7 enter image description here

如何解決呢?

+2

查找第三方警報查看和使用那。你永遠不應該把子視圖添加到'UIAlertView' - 它從來沒有官方的支持。 – Wain

回答

2

從iOS7開始,不再支持將子視圖添加到UIAlertView

你應該實現自己的看法,並嘗試做類似警報視圖,或者像其他人說,使用這樣一個第三方的警報視圖:

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

0

下面的大多數代碼是來自pre 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 

pickerPrompt.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];