UIPIckerView проблема - PullRequest
       1

UIPIckerView проблема

0 голосов
/ 22 декабря 2011

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

когда средство выбора показывается с опциями, остальная часть вида кажется не в фокусе, и только когда я переставляю uipicker, весь вид получает фокус. в чем здесь проблема?

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

Ответы [ 3 ]

0 голосов
/ 22 декабря 2011

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

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 голосов
/ 22 декабря 2011

Я предложу вам одно решение с пользовательским видом.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];

}

Для обработки кнопки «Готово» и скрытия средства выбораПросмотрите это:это просто индивидуальное решение.Надеюсь, что это поможет.

0 голосов
/ 22 декабря 2011

Вы разместили свой 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

и для кнопок вы можете использовать следующие:

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

Удачного кодирования:)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...