2012-06-25 18 views
7

Tôi đang cố gắng đặt lại UIPickerView khi nhấp vào nút. Id pickerview của tôi được tạo khi chạy, tôi đã thiết lập các đại biểu. Sau khi googling, tôi thấyCách đặt lại UIPickerView thành chỉ mục: 0, iPhone

[pickerView reloadAllComponents]; 

Nhưng điều này làm cho mọi ứng dụng vụ tai nạn của tôi khi nó đạt đến ở đây.

Đối tượng tại chỉ mục 0 là "Chọn từ danh sách" và sau đó là các mục. Khi nhấn nút gửi, tôi muốn rằng "Chọn từ danh sách" sẽ vẫn ở đầu nhãn của tôi (chỉ mục đã chọn: 0).

Đây là mã của tôi

ViewDidload 

pickerView = [[UIPickerView alloc] init]; 
    pickerView.delegate = self; 
    pickerView.dataSource = self; 

    -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ 
     return 1; 
    } 


// Total rows in our component. 
    -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ 
     return [nameArray count]; 
    } 

// Display each row's data. 
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ 
    return [nameArray objectAtIndex: row]; 
    } 

// Do something with the selected row. 

    -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ 
     dlbl.hidden=YES; 
     NSLog(@"You selected this: %@", [nameArray objectAtIndex: row]); 
     [btnSelectDesigner setTitle:[nameArray objectAtIndex: row] forState:UIControlStateNormal]; 

    } 

và, trên nút bấm:

-(IBAction)btnSubmitClicked:(id)sender{ 
[pickerView reloadAllComponents]; 
} 

Bất kỳ ý tưởng những gì tôi đang làm sai?

Cảm ơn

+3

Một số mã có thể giúp gỡ lỗi tốt hơn. –

Trả lời

25
[picker reloadAllComponents]; 
[picker selectRow:0 inComponent:0 animated:YES]; 
5
//use this line for going at the top of the label index 0: 

[picker selectRow:0 inComponent:0 animated:YES]; 
+0

Khi nhấp vào nút? cho tôi tai nạn – FirstTimer

+1

Bạn đã phân bổ mảng của mình chưa ?? là lần đầu tiên nó làm việc tốt ?? .. bạn có thể hiển thị đăng nhập tai nạn của bạn ở đây .. những gì đang xảy ra chính xác .. – Abhishek

3

phiên bản Swift:

picker.selectRow(0, inComponent: 0, animated: true)