2012-09-03 22 views
0

嗨誰能幫我這個,我一直都在對這個問題以前的問題很多,他們似乎有點複雜。我想設置一個選擇器,通過觸摸一個名爲locationField的IBOutlet的文本字段進行動畫處理。動畫效果選擇器在+完成按鈕+文本字段更新

我不希望添加的選擇器或工具欄和按鈕在我的故事板,我寧願做編程。

到目前爲止,我有:

- (void)viewDidLoad 
{ 
//populate picker arrays 
locationPickerArray = [[NSArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5", nil]; 


    actionSheet = [[UIActionSheet alloc] initWithTitle:@"" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil]; 

    [actionSheet showInView:self.view]; 

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

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

    UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonSystemItemCancel target:self action:@selector(cancel_clicked:)]; 
    [barItems addObject:cancelBtn]; 

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

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

    [barItems addObject:doneBtn]; 
    [pickerToolbar setItems:barItems animated:YES]; 
    [actionSheet addSubview:pickerToolbar]; 

    UIPickerView *picker = [[UIPickerView alloc] init]; 
    picker.frame = CGRectMake(0, 44, 320, 216); 
    picker.delegate = self; 
    picker.dataSource = self; 
    picker.showsSelectionIndicator = YES; 
    [actionSheet addSubview:picker]; 
} 

-(void)done_clicked:(id)sender 
{ 
    [actionSheet dismissWithClickedButtonIndex:0 animated:YES]; 
} 

-(void)cancel_clicked:(id)sender 
{ 
    [actionSheet dismissWithClickedButtonIndex:0 animated:YES]; 
} 

我有這幾個問題,它崩潰上:

*終止應用程序由於未捕獲的異常 'NSInvalidArgumentException' 的,究其原因: ' - [DetailsS​​creenViewController numberOfComponentsInPickerView:]:無法識別的選擇發送到 實例0xb505be0'

這是什麼意思?

我的最後一個問題是我怎麼能填充這個選擇器,並分配它,當我在前面創建的IBOutlet中點擊(位置字段)彈出。

謝謝

回答

1

原因您墜毀是您分配的DetailsScreenViewController一個實例是一個UIPickerViewdataSourcedelegate沒有實際執行這些協議的方法。分配發生在這裏:

picker.delegate = self; 
picker.dataSource = self; 

具體的崩潰是告訴你,你還沒有實現numberOfComponentsInPickerView:,該UIPickerViewDataSource協議的一部分。實際上,你應該已經得到了編譯器警告。學會使用這些警告可能會非常有幫助!無論如何,要解決它,提供兩種協議所需的所有方法的實現。實現的細節將取決於您的應用程序的需求。如果你仍然需要幫助,請告訴我詳情。

關於你提到的第二個問題,我想放一個UIPickerView一個UIActionSheet裏面是現在的事情。我從來沒有必要這樣做,但這裏有一個SO question,有一些背景的示例代碼,不知道你是否已經閱讀過這樣的代碼。但我認爲你只需要等待的時間,你居然要顯示的選擇器調用

[actionSheet showInView:self.view]; 

我認爲你可以在一個 UITextFieldDelegate方法做到這一點,有可能是其他方式。我將盡力在後面做一些實驗並更新這篇文章。

編輯: 如果您分配您的視圖控制器的任何文本字段,你感興趣的代表,那麼你就可以實現這個UITextFieldDelegate方法:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField 
{ 
    [actionSheet showInView:self.view]; 
    return NO; 
} 

編輯完

希望這有助於,並讓我知道如果您有任何問題。

+0

非常感謝您的幫助,它在設置這些方法後不再崩潰。無論如何,我想說如果說正在編輯某個文本字段,然後顯示某個選擇器? (我有兩個每個)我猜我可以用標籤做到這一點?你是否可以檢查我在那裏的號碼,只有一半的工具欄顯示,當我運行它時沒有選擇器在屏幕上。 – dev6546

+0

您的視圖控制器是否具有對任一文本字段的引用?然後做'if(textField == self.whateverField){//顯示錶單,返回NO} else {//做其他任何事情}' –

+0

非常感謝你,然後我會使用標籤作爲拾取器。你如何看待選取框和標籤欄的矩形?我必須弄錯數字,因爲它只在屏幕底部顯示一半標籤欄。 – dev6546