简体   繁体   中英

Pickerview for TextField

I have this code, it's inside an action that it's triggered when the user selects the textfield to show the datepicker:

    pickerViewPopup = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];

    pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 0, 0)];
    pickerView.datePickerMode = UIDatePickerModeDateAndTime;
    pickerView.hidden = NO;
    pickerView.date = [NSDate date];

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

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

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

    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneButtonPressed:)];
    [barItems addObject:doneBtn];

    [pickerToolbar setItems:barItems animated:YES];

    [pickerViewPopup addSubview:pickerToolbar];
    [pickerViewPopup addSubview:pickerView];
    [pickerViewPopup showFromTabBar:self.tabBarController.tabBar];
    [pickerViewPopup setBounds:CGRectMake(0,0,320, 500)];

I was using this code in an app with a tab bar for navigation. Now I would like to use it in an app without it, but when the process reaches [pickerViewPopup showFromTabBar:self.tabBarController.tabBar]; it crashes.

Any idea on how I could fix it?

Use one of the following,

– showFromToolbar:
– showInView:
– showFromBarButtonItem:animated:
– showFromRect:inView:animated:

For eg:-

[pickerViewPopup showFromToolbar:pickerToolbar];

The problem was that you dont have a tab bar and you are trying to access the tab bar object to show the picker from it. Since tabbar is nil for you, it will crash.

For more details check the apple documentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM