TextField的Pickerview

时间:2012-11-29 19:45:49

标签: ios datepicker tabbar

我有这个代码,它是在用户选择显示日期选择器的文本字段时触发的操作内部:

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

我在带有标签栏的应用中使用此代码进行导航。现在我想在没有它的应用程序中使用它,但当进程到达[pickerViewPopup showFromTabBar:self.tabBarController.tabBar];时它会崩溃。

我对如何修复它的想法?

1 个答案:

答案 0 :(得分:2)

使用以下其中一项

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

例如: -

[pickerViewPopup showFromToolbar:pickerToolbar];

问题是你没有标签栏而你正试图访问标签栏对象以显示它的选择器。由于tabbar对你来说是零,它会崩溃。

有关详细信息check the apple documentation.