2014-09-12 78 views
0

我發現以下site代碼應該顯示一個按鈕,一旦點擊,它會顯示一個下拉菜單。沒有顯示按鈕,因此無法測試功能。ios按鈕上的下拉菜單點擊

我錯過了什麼嗎?我是否必須在UI中創建一個元素並將其鏈接起來?

這裏是我的代碼:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 

    self.countriesArray = [[NSArray alloc] initWithObjects:@"Saudi Arabia", @"United Arab Emirates", @"Bahrain", nil]; 
    self.countriesPicker = [[UIPickerView alloc] initWithFrame:CGRectZero]; 
    [self attachPickerToTextField:self.countriesTextfield :self.countriesPicker]; 

} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (void)attachPickerToTextField: (UITextField*) textField :(UIPickerView*) picker{ 
    picker.delegate = self; 
    picker.dataSource = self; 
    textField.delegate = self; 
    textField.inputView = picker; 
} 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    [self.countriesTextfield resignFirstResponder]; 
} 

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


- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 
{ 
    if (pickerView == self.countriesPicker){ 
     return self.countriesArray.count; 
    } 
     return 0; 
} 

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{ 
    if (pickerView == self.countriesPicker){ 
     return [self.countriesArray objectAtIndex:row]; 
    } 

    return @"???"; 
} 

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
{ 
    if (pickerView == self.countriesPicker){ 
     self.countriesTextfield.text = [self.countriesArray objectAtIndex:row]; 
    } 
} 
+0

這個視圖有UIPickerView委託方法,但它是否正確地連接在故事板?如果沒有,那些設置pickerview的委託方法都不會被捕獲。 沒有關於按鈕的東西 - 只是從硬編碼選項陣列中創建一個pickerView,並加載國名。當一個選擇器行被選中時,它將該國家名稱放入textField中。 它設置'textField.inputView = picker'的線應該是設置選取器而不是鍵盤顯示的線。當你點擊textView時它不會出現嗎? – mc01 2014-09-12 19:02:10

回答

0

您發佈的鏈接是隻有一個NSViewController子類代碼下載頁面,所以沒有背景,我不能徹底的答案。此外,鏈接代碼與問題中的引用代碼不匹配(這必須是簡化版本?),所以我的答案在下面提到的鏈接代碼中,我至少可以看到IBOutlet屬性實例化的位置。

該代碼將兩個UIPickerViews連接到兩個UITextFields。沒有按鈕,也沒有下拉菜單。從鏈接上的代碼,它看起來像是在Interface Builder中設置並連接了選擇器和文本框。我這樣說是因爲IBOutlet屬性是在代碼的內部設置的,它們不會在頭文件的外部接口中公開。

儘管如此,爲了使UIPickerViews在加載時不可見,我認爲有必要在代碼中實例化它們。看到這個答案:instantiating UIPickerView for UITextField

+0

你在哪裏看到2?只有國家Picker和國家Textfield。 – mc01 2014-09-12 18:51:51

+0

謝謝你指出這一點。將編輯修復。我給出的答案是基於鏈接的文件,它似乎與引用的代碼不匹配。 – RishiG 2014-09-12 19:28:08