2011-12-22 3 views
0

У меня есть код ниже в моем приложении. Я показываю UIPickerView с опциями, и у меня есть TextView, размещенный на том же UIViewController.Проблема UIPIckerView

Когда сборщик показан с опциями, остальная часть обзора кажется не в фокусе, и только когда я ухожу в отставку, весь взгляд получает фокус. В чем проблема?

-(void)viewWillAppear:(BOOL)animated { 
    myActionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil]; 
    UIPickerView *pickerViewCountry = [[UIPickerView alloc] init]; 
    pickerViewCountry.showsSelectionIndicator = YES; 
    pickerViewCountry.dataSource = self; 
    pickerViewCountry.delegate = self; 
    pickerViewCountry.frame = CGRectMake(0,35 , 320, 15); 
    [myActionSheet addSubview:pickerViewCountry]; 
    [pickerViewCountry release]; 

    closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Done"]]; 
    closeButton.momentary = YES; 
    closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 27.0f); 
    closeButton.segmentedControlStyle = UISegmentedControlStyleBar; 
    closeButton.tintColor = [UIColor blackColor]; 
    [closeButton addTarget:self action:@selector(hidePicker) forControlEvents:UIControlEventValueChanged]; 
    [myActionSheet addSubview:closeButton]; 
    [closeButton release]; 

    [myActionSheet showInView:self.view]; 
    [myActionSheet setBounds:CGRectMake(0, 0, 320, 250)]; 

    UITextView *myTextView = [[UITextView alloc] init]; 
    myTextView.frame = CGRectMake(100, 12, 210, 20); 
    myTextView.layer.borderWidth = 1.0f; 
    myTextView.layer.cornerRadius = 5; 
    myTextView.clipsToBounds = YES; 
    myTextView.layer.borderColor = [[UIColor grayColor] CGColor]; 
    myTextView.text = @"some text"; 
    [self.view addSubview:myTextView]; 
    [myTextView sizeToFit]; 
} 

enter image description here

ответ

0

Вы разместили свой UIPickerView внутри UIActionSheet. Это стандартное поведение для UIActionSheet. Попробуйте использовать keyboardView вместо так:

UITextField* dummyTextField = [[UITextField alloc]initWithFrame:CGRectMake(0, -20, 10, 10)];//the point is to put it outside visible view 
dummyTextField.inputView = pickerViewCountry; 

[dummyTextField becomeFirstResponder];//call to appear 
[dummyTextField resignFirstResponder];//call do disappear 

и кнопок вы можете использовать folloving:

dummyTextField.inputAccessoryView = <some toolbar with buttons or any other view> 

Днем кодирования :)

0

объявить переменную в файле заголовка

UIActionSheet *actionSheet; 
NSString *pickerType; 

Впишите свой код в файл реализации:

- (void)createActionSheet { 
if (actionSheet == nil) { 
    // setup actionsheet to contain the UIPicker 
    actionSheet = [[UIActionSheet alloc] initWithTitle:@"Select" 
               delegate:self 
            cancelButtonTitle:nil 
           destructiveButtonTitle:nil 
            otherButtonTitles:nil]; 

    UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; 
    pickerToolbar.barStyle = UIBarStyleBlackOpaque; 
    [pickerToolbar sizeToFit]; 

    NSMutableArray *barItems = [[NSMutableArray alloc] init]; 

    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]; 
    [barItems addObject:flexSpace]; 
    [flexSpace release]; 

    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(pickerDone:)]; 
    [barItems addObject:doneBtn]; 
    [doneBtn release]; 

    [pickerToolbar setItems:barItems animated:YES]; 
    [barItems release]; 

    [actionSheet addSubview:pickerToolbar]; 
    [pickerToolbar release]; 

    [actionSheet showInView:self.view]; 
    [actionSheet setBounds:CGRectMake(0,0,320, 464)]; 
} 
} 

-(IBAction)BtnPressed:(id)sender 
{ 
[self createActionSheet]; 
pickerType = @"picker"; 
select = NO; 
UIPickerView *chPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)]; 
chPicker.dataSource = self; 
chPicker.delegate = self; 
chPicker.showsSelectionIndicator = YES; 
[actionSheet addSubview:chPicker]; 
sessoTxt.text = [sessoArray objectAtIndex:0]; 
[chPicker release]; 
} 

методы объявить делегат

#pragma mark UIPickerViewDelegate Methods 

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

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 
{ 
    int count; 
    if ([pickerType isEqualToString:@"picker"]) 
     count = [array count]; 
return count; 
} 

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

    if ([pickerType isEqualToString:@"picker"]) 
     string = [array objectAtIndex:row]; 

return string; 
} 

// Set the width of the component inside the picker 
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component { 
    return 300; 
} 

// Item picked 
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { 
    select = YES; 
    if ([pickerType isEqualToString:@"picker"]) 
    { 
     Txt.text = [array objectAtIndex:row]; 
    } 
} 

- (void)pickerDone:(id)sender 
{ 
    if(select == NO) 
    { 
     if ([pickerType isEqualToString:@"picker"]) 
     { 
      Txt.text = [array objectAtIndex:0]; 
     } 
} 
    [actionSheet dismissWithClickedButtonIndex:0 animated:YES]; 
    [actionSheet release]; 
    actionSheet = nil; 

} 

} 
0

Я предлагаю вам одно решение с настраиваемыми View. datePickerContainer является UIView:

//datepicker  
    datePickerContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 440, 310, 300)]; 
    datePickerContainer.opaque = YES; 
    datePickerContainer.backgroundColor = [UIColor clearColor]; 
    UIImageView *pickerNavBar = [[UIImageView alloc] init]; 
    UIImage *barImage = [UIImage imageNamed:@"pickerbar.png"]; 
    pickerNavBar.image = barImage; 
    pickerNavBar.frame = CGRectMake(0, 7, barImage.size.width, barImage.size.height); 
    [datePickerContainer addSubview:pickerNavBar]; 
    [pickerNavBar release]; 

UIButton* blueButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [blueButton setTitle:@"Done" forState: UIControlStateNormal]; 
    blueButton.frame = CGRectMake(255, 15, 55, 30); 
    [blueButton addTarget:self action:@selector(hidePickerViewContainer) forControlEvents:UIControlEventTouchUpInside]; 
    [datePickerContainer addSubview:blueButton]; 
    [blueButton release]; 

    //UIPickerView Initialization code 
     UIPickerView *pickerViewCountry = [[UIPickerView alloc] init]; 
     pickerViewCountry.showsSelectionIndicator = YES; 
     pickerViewCountry.dataSource = self; 
     pickerViewCountry.delegate = self; 
     pickerViewCountry.frame = CGRectMake(0,35 , 320, 15); 
     [datePickerContainer addSubview:pickerViewCountry]; 
     [pickerViewCountry release]; 

    //set container and release 
    [self.view addSubview: datePickerContainer]; 
    [datePickerContainer release]; 

Чтобы показать datePickerContainer UIVie использовать этот код:

-(void)showPickerViewContainer{ 
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.4]; 
    CGRect frameContainer = datePickerContainer.frame; 
    frameContainer.origin.y = 150.0f; 
    datePickerContainer.frame = frameContainer; 
    [UIView commitAnimations]; 

} 

Для обработки кнопку Готово и скрыть pickerView использовать это:

-(void)hidePickerViewContainer{ 
    //here use you custom code to handle done action 

    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.4]; 
    CGRect frameContainer = datePickerContainer.frame; 
    frameContainer.origin.y = 440.0f; 
    datePickerContainer.frame = frameContainer; 
    [UIView commitAnimations]; 

} 

Это просто настраиваемое решение , Надеюсь, эта помощь.