2014-12-01 79 views
1

帶工具欄的選取器視圖,創建完成按鈕。點擊完成按鈕不起作用。UIPickerView與完成按鈕不起作用

選取器視圖向上滾動。點擊完成按鈕。

-(void)createPicker:(id)sender{ 

    pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,100,0,0)]; 
    [pickerView setDataSource: self]; 
    [pickerView setDelegate: self]; 
    pickerView.showsSelectionIndicator = YES; 
    [pickerView setBackgroundColor:[UIColor whiteColor]]; 

    UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)]; 
    toolBar.barStyle = UIBarStyleBlackOpaque; 
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneTouched:)]; 
    [toolBar setItems:[NSArray arrayWithObjects:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], doneButton, nil]]; 
    [pickerView addSubview:toolBar]; 
} 

在點擊完成按鈕關閉該pickerView

-(void)doneTouched:(id)sender{ 
    [pickerview removeFromSuperview]; 
} 

我不知道我做錯了什麼在這裏。任何人都可以建議我如何調用在uipickerview工具欄按鈕上添加的完成按鈕方法。

在單擊完成選擇器視圖是向上滾動,而不是調用方法doneTouched:

@All 在此先感謝。

+0

您的選擇器視圖沒有框架:CGRectMake(0,100,0,0) – 2014-12-01 18:55:08

+0

我在指出這一點,因爲它可能是您無法與工具欄進行交互的原因,因爲工具欄並非技術上的在UIPickerView框架內。 – 2014-12-01 19:02:27

+0

@LyndseyScott我無法弄清楚。我做錯了什麼。 – KkMIW 2014-12-01 19:20:10

回答

4

我已經解決了不知道的問題是否正確實施的問題,但它對我有用。 下面完成按鈕

-(void)createPickerView{ 

    pickerToolBarView = [[UIView alloc]initWithFrame:CGRectMake(0,self.view.frame.size.height/2, self.view.frame.size.width,400)]; 
    [pickerToolBarView setBackgroundColor:[UIColor whiteColor]]; 

    UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,pickerToolBarView.frame.size.width,42)]; 
    toolBar.barStyle = UIBarStyleBlackOpaque; 
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneTouched:)]; 
    [toolBar setItems:[NSArray arrayWithObjects:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], doneButton, nil]]; 

    pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,toolBar.frame.size.height,toolBar.frame.size.width,100)]; 
    [pickerView setDataSource: self]; 
    [pickerView setDelegate: self]; 
    pickerView.showsSelectionIndicator = YES; 
    [pickerView setBackgroundColor:[UIColor whiteColor]]; 

    [pickerToolBarView addSubview:toolBar]; 
    [pickerToolBarView addSubview:pickerView]; 
    [self.view addSubview:pickerToolBarView]; 
    [self.view bringSubviewToFront:pickerToolBarView]; 
    [pickerToolBarView setHidden:YES]; 
} 

/* Done Touched */ 
- (void)doneTouched:(UIBarButtonItem *)sender{ 
    // hide the view 
    NSLog(@"Done Touched"); 
    [pickerToolBarView setHidden:YES]; 
} 
1

而不是添加工具欄作爲子視圖的選擇器視圖代碼做:

yourTextField.inputAccessoryView = toolBar; 
0

我有同樣的問題。我的問題是我沒有直接將選取器綁定到文本字段,而是從一個按鈕調出選取器。我還想要一個「完成」按鈕,在我完成時擺脫選擇器。我想,問題在於我將工具欄作爲選取器視圖的一部分,出於某種原因,我無法使按鈕工作。我所做的是使工具欄成爲視圖的子視圖,並將其y值設置爲選取器的角落--44。這將其移動到選取器的頂部並允許按鈕工作。 這裏有很多關於這個問題的問題,他們都沒有爲我工作。我從他的網站上的Gabriel Theodoropoulos的例子開始:http://gabriel-tips.blogspot.com/2011/04/uipickerview-add-it-programmatically_04.html 並做了一些修改。以下是我想出了,它似乎爲我工作:

在.h文件:

` 
    @interface Detail() <UIPickerViewDataSource, UIPickerViewDelegate> 
    @property (nonatomic, retain) IBOutlet UIPickerView *picker; 
    @property (nonatomic, retain) NSArray *pickerData; 
    @property (assign, nonatomic) NSInteger pickerRowSelected; 
    @property (nonatomic, retain) UIBarButtonItem *barButtonDone; 
    @property (nonatomic, retain) UIToolbar *toolBar; 
    @end 

` 

在.m文件:`

-(void)showPicker{ 
    // Calculate the screen's width. 
    float screenWidth = [UIScreen mainScreen].bounds.size.width; 
    float pickerWidth = screenWidth * 3/4; 

    // Calculate the starting x coordinate. 
    float xPoint = screenWidth/2 - pickerWidth/2; 

    // Init the picker view. 
    picker = [[UIPickerView alloc] init]; 

    // Set the delegate and datasource. Don't expect picker view to work 
    // correctly if you don't set it. 
    [picker setDataSource: self]; 
    [picker setDelegate: self]; 

    // Set the picker's frame. We set the y coordinate to 50px. 
    [picker setFrame: CGRectMake(xPoint, 150.0f, pickerWidth, 200.0f)]; 

    // Before we add the picker view to our view, let's do a couple more 
    // things. First, let the selection indicator (that line inside the 
    // picker view that highlights your selection) to be shown. 
    picker.showsSelectionIndicator = YES; 

    // Allow us to pre-select the first option in the pickerView. 
    [picker selectRow:0 inComponent:0 animated:YES]; 

    [picker setBackgroundColor:[UIColor grayColor]]; 

    toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,150-44,pickerWidth,44)]; 

    barButtonDone = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(itemWasSelected:)]; 
    [toolBar setItems:[NSArray arrayWithObjects: barButtonDone, nil]]; 

    // OK, we are ready. Add the picker in our view. 
    [self.view addSubview: picker]; 

    // Add the done button to the picker view 
    [self.view addSubview:toolBar]; 
    [toolBar becomeFirstResponder]; 
} 


// The number of columns of data 
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView 
{ 
    return 1; 
} 

// The number of rows of data 
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 
{ 
    return pickerData.count; 
} 

// The data to return for the row and component (column) that's being passed in 
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{ 
    return pickerData[row]; 
} 

// Catpure the picker view selection 
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
{ 
    // This method is triggered whenever the user makes a change to the picker selection. 
    // The parameter named row and component represents what was selected. 
    self.pickerRowSelected = row; 
} 

-(IBAction)itemWasSelected:(id)sender{ 

    NSLog(@"Picker selected = %@",[pickerData objectAtIndex: self.pickerRowSelected]); 
    //NSLog(@"ID = %ld",(long)[self.thisData getIDForEntry:@"company" column:@"name" entry:[pickerData objectAtIndex: self.pickerRowSelected]]); 
    [picker setHidden:YES]; 
    [toolBar setHidden:YES]; 

} 


- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component 
{ 

    NSAttributedString *attString = 
    [[NSAttributedString alloc] initWithString:[pickerData objectAtIndex:row] attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}]; 

    return attString; 
} 

//bring up the size Picker 
//This is tied to a button from IB and when pressed will bring up 
//the picker. The index returned is stored in self.pickerRowSelected 
- (IBAction)sizeButtonPressed:(id)sender { 

    self.pickerData = [self.thisData getSizePickerData]; 
    [self showPicker]; 

} 

` 我確定有更好的方法來實現這一點,但這對我有用,我現在正在使用它。