저는 Xcode/Swift로 프로그래밍하기가 매우 어려워서 작은 문제에 직면하고 있습니다.대리인은 항상 섹터없이 2x ViewControllers간에 정보를 전달하지 않습니다.
저는 대리자와 segue를 사용하지 않고 ViewController에서 두 번째 ViewController로 정보를 보내려고합니다. 나는 그것에 대해 많은 것을 읽었고 가장 일반적인 해결책이 ViewDidLoad에서 "인스턴스".delegate = self를 사용하고 있음을 발견했다. 그러나 그것은 단지 나를 위해 작동하지 않는다.
- 앱의 정의 -
꽤 쉽습니다. 첫 번째 ViewController에는 단추와 레이블이 있습니다. 버튼은 두 번째 ViewController를 엽니 다.이 두 번째 ViewController에는 textField 및 Button이 있습니다. Button는 textField에있는 내용을 첫 번째 ViewController로 보내 레이블을 업데이트합니다.
- 코드 -이 첫 번째의 ViewController 코드입니다
:
protocol clickedButtonDelegate {
func didClickedButton(text: String)
}
class SecondViewController: UIViewController {
@IBOutlet weak var introducedText: UITextField!
var delegate : clickedButtonDelegate!
@IBAction func sendData(_ sender: Any) {
if delegate != nil {
let information:String = introducedText.text!
delegate!.didClickedButton(text: information)
dismiss(animated: true, completion: nil)
self.navigationController?.popViewController(animated: true)
}
}
}
이 코드 아무것도됩니까 사용하지 :
class ViewController: UIViewController, clickedButtonDelegate {
@IBOutlet weak var Label: UILabel!
func didClickedButton(text: String) {
Label.text = text
}
var secondView = SecondViewController()
override func viewDidLoad() {
super.viewDidLoad()
secondView.delegate = self
}
}
이 두 번째의 ViewController에 대한 코드입니다 왜냐하면 대리자는 SecondViewController에서 항상 nil이기 때문입니다.
도와 주시겠습니까?
미리 감사드립니다.
언제 어떻게 SecondViewController를 푸시합니까? 코드 조각도 제공해 주실 수 있습니까? –
다음 행 :'var secondView = SecondViewController()'는'SecondViewController'의 인스턴스를 생성하고 있습니다. **이 ** 인스턴스에 연결되어 있습니까? 아니면 스토리 보드 단편을 통해'SecondViewController'에 도달 했습니까?그렇다면 ** 인스턴스 **에 대한 포인터를 만들어서'var secondView = SecondViewController()'를 피해야합니다. – Honey
[이 답변보기] (https://stackoverflow.com/a/31934786/) 5175709) 및 링크 된 비디오 – Honey