DropDown列表而不是DataPicker和PickerView

时间:2014-04-17 11:33:28

标签: ios objective-c

我想创建一个下拉列表,而不是使用DataPicker和Picker视图,所以我创建了自己的,没有错误,但它无法正常工作:(如果你能搞清楚,请看一下让我知道。 注意我正在使用Storyboard。我想要一个(性别,位置)和日期的下拉列表。

我的标题

IBOutlet UIPickerView *PickerviewS;
IBOutlet UIToolbar *mypickerToolbar;
UIDatePicker *mydatePicker;
IBOutlet UITextField *DOBS;
IBOutlet UITextField *genderS;
IBOutlet UITextField *LocationS;
NSArray *gender;
NSArray *Location;

我的档案:

    - (void)viewDidLoad : (BOOL)animated
    {

        UIToolbar* keyboardDoneButtonView = [[UIToolbar alloc] init];
        keyboardDoneButtonView.barStyle = UIBarStyleDefault;
        keyboardDoneButtonView.translucent = YES;
        keyboardDoneButtonView.tintColor = nil;
        [keyboardDoneButtonView sizeToFit];
        UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc]        initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                                       style:UIBarButtonItemStyleBordered target:self
                                                                      action:@selector(Doneaction:)];
        [keyboardDoneButtonView setItems:[NSArray arrayWithObjects:flexibleSpaceLeft, doneButton, nil]];

        DOBS.inputAccessoryView = keyboardDoneButtonView;
        [mydatePicker removeFromSuperview];
        [PickerviewS removeFromSuperview];


        [super viewDidLoad];

        gender = [[NSArray alloc] initWithObjects:@"", @"Male",@"Female", nil];
            Location = [[NSArray alloc] initWithObjects:@"", @"NYC",@"LA",@"Paris",@"London", @"Rome",nil];


        //this code is for setting the date picker
        mydatePicker = [[UIDatePicker alloc]init];
        mydatePicker.datePickerMode = UIDatePickerModeDateAndTime;
        [mydatePicker setMinimumDate: [NSDate date]];
        [mydatePicker addTarget:self action:@selector(updateTextField:) forControlEvents:UIControlEventValueChanged];
        DOBS.InputView = mydatePicker;




        //make sure you assign the textfields delegate to self
        LocationS.delegate =self;
        genderS.delegate=self;

     }



    -(void)updateTextField:(id)sender
    {UIDatePicker *picker = (UIDatePicker*)DOBS.inputView;
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateStyle:NSDateFormatterLongStyle];
        [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
        DOBS.text = [dateFormatter stringFromDate:[picker date]];
        //self.dateandtime.text = [NSString stringWithFormat:@"%@",picker.date];
        DOBS.textColor = [UIColor blackColor];


        - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
        {self showPicker:textField];
            if(textField == LocationS ){
                [PickerviewS selectRow:0 inComponent:0 animated:NO];}
            else if(textField== genderS){
                [PickerviewS selectRow:0 inComponent:0 animated:NO];}
          return YES;}
    //show a picker view when any of these text field is active
    - (IBAction)showPicker:(id)sender {
        UITextField *textField=(UITextField *)sender;
        PickerviewS = [[UIPickerView alloc] init];
        PickerviewS.showsSelectionIndicator = YES;
        PickerviewS.dataSource = self;
        PickerviewS.delegate = self;
        mypickerToolbar = [[UIToolbar alloc] init];
        mypickerToolbar.barStyle = UIBarStyleDefault;
        [mypickerToolbar sizeToFit];
        //to make the done button aligned to the right
        UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
    style:UIBarButtonItemStyleDone target:self
    action:@selector(Doneaction:)];
    [mypickerToolbar setItems:[NSArray arrayWithObjects:flexibleSpaceLeft, doneButton, nil]];
        if(textField== LocationS)
            {PickerviewS.tag=1;
            LocationS.inputView = PickerviewS;
            LocationS.inputAccessoryView = mypickerToolbar;}
        else if(textField== genderS)
        {PickerviewS.tag=2;
            genderS.inputView = PickerviewS;
            genderS.inputAccessoryView = mypickerToolbar;}
        }


//assign arrays for the textfield picker view
- (void)pickerView:(UIPickerView *)mypickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{if(mypickerView.tag==1){
        LocationS.text = [Location objectAtIndex:row];
        LocationS.textColor = [UIColor blackColor];}

else if(mypickerView.tag==2){
        genderS.text = [ gender objectAtIndex:row];
        genderS.textColor = [UIColor blackColor];
    }
}



//assign number of rows in a picker view for each textfield
- (NSInteger)pickerView:(UIPickerView *)mypickerView numberOfRowsInComponent:(NSInteger)component;
{
    if(mypickerView.tag==1)
    {return [Location count];}
    else if (mypickerView.tag==2)
        {return [gender count];}

  else
      return [ gender count];
}



//to return the chosen value from the picker view
- (NSString *)pickerView:(UIPickerView *)mypickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
    if(mypickerView.tag==1){
        return [Location objectAtIndex:row];}

    else if(mypickerView.tag==2){
        return [gender objectAtIndex:row];}
    else
        return [gender objectAtIndex:row];}

0 个答案:

没有答案