2017-09-26 9 views
1

내 선택기보기에서 기본 행을 0으로 지정합니다. 이 행의 값은 1입니다. 버튼을 제외한 뷰 컨트롤러에서 아무 것도 터치하지 못하도록하고 싶습니다. 나는 비슷한 질문이 있다는 것을 알고 있지만 그들은 나를 위해 일하지 않았다.PickerView 기본 행을 선택했지만 선택기보기를 이동하지 않은 경우 0을 반환합니다.

override func viewWillAppear(_ animated: Bool) { 

    self.pickerView.delegate = self 
    self.pickerView.dataSource = self 


    self.pickerView.selectRow(0, inComponent: 0, animated: true) 

     } 


func numberOfComponents(in pickerView: UIPickerView) -> Int { 
    return 1 
} 

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { 
    return String(numbers[row]) 
} 

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { 
    return numbers.count 

} 

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { 

    therow = pickerView.selectedRow(inComponent: 0) + 1   
} 

다음

@IBAction의 FUNC의 submitTapped (_ 보낸 사람 : 모두) { 인쇄 (therow) }

내가 제출 및 행 0의 값을 인쇄를 누릅니다 그것을 0이지만, 피커보기를 흔들어 다시 0 행에 놓으면 1을 인쇄합니다. 피커보기에서 아무 것도 건드리지 않고 기본 행의 적절한 값을 반환 할 수 있어야합니다.

+0

난 당신이 didSelectRow 방법 –

+0

theRowIndex에서 "theRowIndex = 행"을 사용한다고 생각하는 것은 알 수없는 @SaurabhPrajapati –

+0

이'didSelectRow'입니다 단추 만 누를 때 전화가 왔어? – Sweeper

답변

1

당신은 사용해야 row 그 다음과 같이 코드를 수정해야하므로 방법, 당신을 제공하는 pickerview 위임 : numbers 배열 번호 = [1, 2, 3

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { 

    therow = numbers[row] 
    //theRowIndex = row //this is the index of row that you selected 


} 

예를 들어 경우, 4 ]에서 위의 첫 번째 행을 클릭하면 therow이 1로 설정되고 두 번째 행을 클릭하면 therow이 2로 설정됩니다. 당신은 당신이 다음 다음과 같이 사용할 수 있습니다 쓴 코드를 사용하려면

는 :

therow = numbers[pickerView.selectedRow(inComponent: 0)] 

이렇게하면 선택한 행의 수를 줄 것이다,하지만 난 당신이 위의 방법 안에 그것을 필요가없는 것 같아요. 당신이 선택기를 터치합니다 해달라고하면

지금 나는 당신이 할 필요가 있다고 생각 :

@IBAction func submitTapped(_ sender: Any) { 
     therow = numbers[self.pickerView.selectedRow(inComponent: 0)] 
     print(therow) 

} 
+0

내 프로그램에서 "theRowIndex"는 어디에서 얻었습니까? @ 3stud1ant3 –

+0

@MichaelR.내 appologies, 난 단지 예를 들어 그것을 언급, 답변을 업데이 트, 당신을 도울 경우 참조하십시오 – 3stud1ant3

1

사용이 문을 사용하면 데이터와 피커보기를로드하면.

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

selectRow의 첫 번째 매개 변수를 변경하여 기본 선택 값을 변경할 수 있습니다.

1

이 이유는 프로그래밍 방식으로 행을 선택한 경우 didSelectRow이 어떻게 든 호출되지 않는다고 생각합니다. docs 따라 다음 사용자가 컴포넌트에서 행을 선택하는 선택기 뷰에 의해 호출

.

그래서 당신은 당신이 selectRow 전화를 프로그래밍 한 후 therow 속성을 설정해야합니다

self.pickerView.selectRow(0, inComponent: 0, animated: true) 
therow = 1 // <--- this line