Post

Replies

Boosts

Views

Activity

Reply to I get the error "Unexpectedly found nil while implicitly unwrapping an Optional value" when calling a protocol method
The user will click viewersTapped which will store the value of the senders current title into the protocol and then they will click the navigation bar back button which is viewWillDisappear to go to the base "ViewController". language protocol SourceForB {   func prepare(for segue: UIStoryboardSegue, from sender: Any?, toB vc: testStore) } protocol labelMaker {   func labelMaker(value: String) } class storeViewController: UIViewController {  var labelDelegate: labelMaker!  @IBAction func viewersTapped(_ sender: UIButton) {     labelDelegate.labelMaker(value: sender.currentTitle!) } override func viewWillDisappear(_ animated: Bool)   {                   }     super.viewWillDisappear(animated)     self.navigationController?.isNavigationBarHidden = false   } } extension testStore : SegueDestination{       func prepareAsDestination( segue: UIStoryboardSegue, sender: Any?){           let sourceForB = segue.source as! SourceForB     sourceForB.prepare(for: segue, from: sender, toB: self)   } } Once they are in the base "ViewController" they will click startLivePressed which will segue them to the LiveViewController language class ViewController: UIViewController, SourceForB, SourceForC {     @IBOutlet weak var startLiveButton: UIButton!    var startLiveButtonIsSelected = false @IBAction func startLivePressed(_ sender: UIButton) {            startLiveButtonIsSelected = !startLiveButtonIsSelected        performSegue(withIdentifier: "segueToCamera", sender: self)   }  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {      segue.prepare(sender:sender)    }  func prepare(for segue: UIStoryboardSegue, from sender: Any?, toC vc: liveViewController) {              } } Where the value that was stored in the protocol should be displayed within the onlineViewers label language protocol SourceForC{   func prepare(for segue: UIStoryboardSegue, from sender: Any?, toC vc: liveViewController) } class liveViewController: UIViewController { let storeView = storeViewController()    @IBOutlet var onlineViewers: UILabel! override func viewDidLoad() {  super.viewDidLoad()        storeView.labelDelegate = self } } extension liveViewController: labelMaker {   func labelMaker(value: String) {     onlineViewers.text = value } extension liveViewController : SegueDestination {      func prepareAsDestination( segue: UIStoryboardSegue, sender: Any?){           let sourceForC = segue.source as! SourceForC     sourceForC.prepare(for: segue, from: sender, toC: self)   } }
Topic: Programming Languages SubTopic: Swift Tags:
Feb ’21
Reply to I get the error "Unexpectedly found nil while implicitly unwrapping an Optional value" when calling a protocol method
My mistake, heres how everything works. The user will launch the app and will be met with the base "ViewController" From there they can either click startLivePressed or storeButtonPressed. If they click startLivePressed without ever going to the store there will not be a value in the onlineViewers label within the liveViewController. So they will click storeButtonPressed first. language class ViewController: UIViewController, SourceForB, SourceForC {    @IBOutlet weak var startLiveButton: UIButton!     var startLiveButtonIsSelected = false @IBAction func startLivePressed(_ sender: UIButton) {           startLiveButtonIsSelected = !startLiveButtonIsSelected       performSegue(withIdentifier: "segueToCamera", sender: self)    }    @IBAction func storeButtonPressed(_ sender: UIButton) {      storeButtonIsSelected = !storeButtonIsSelected     performSegue(withIdentifier: "segueToStore", sender: self)   } override func prepare(for segue: UIStoryboardSegue, sender: Any?) {      segue.prepare(sender:sender)    }  func prepare(for segue: UIStoryboardSegue, from sender: Any?, toC vc: liveViewController) {            }  func prepare(for segue: UIStoryboardSegue, from sender: Any?, toB vc: storeViewController) {           if (segue.identifier == "segueToStore") {       navigationController?.setNavigationBarHidden(navigationController?.isNavigationBarHidden == false, animated: true)          }    } @IBAction func unwindToViewController(unwindSegue: UIStoryboardSegue){     print("unwind")   } } Once the user is in the storeViewController they will click viewersTapped which should store the value of senders current title. After they click that they will click the navigation bar back button which is viewWillDisappear to go back to the viewController. language protocol SourceForB {    func prepare(for segue: UIStoryboardSegue, from sender: Any?, toB vc: storeViewController) } protocol labelMaker {   func labelMaker(value: String) } class storeViewController: UIViewController {  var labelDelegate: labelMaker!  @IBAction func viewersTapped(_ sender: UIButton) {     labelDelegate.labelMaker(value: sender.currentTitle!) } override func viewWillDisappear(_ animated: Bool)  {                 super.viewWillDisappear(animated)    self.navigationController?.isNavigationBarHidden = false   } } extension storeViewController : SegueDestination{       func prepareAsDestination( segue: UIStoryboardSegue, sender: Any?){           let sourceForB = segue.source as! SourceForB     sourceForB.prepare(for: segue, from: sender, toB: self)   } } Once they are back in the viewController they will now click startLivePressed language class ViewController: UIViewController, SourceForB, SourceForC {    @IBOutlet weak var stopButton: UIButton!  @IBOutlet weak var startLiveButton: UIButton!     var startLiveButtonIsSelected = false @IBAction func startLivePressed(_ sender: UIButton) {           startLiveButtonIsSelected = !startLiveButtonIsSelected       performSegue(withIdentifier: "segueToCamera", sender: self)    }    @IBAction func storeButtonPressed(_ sender: UIButton) {      storeButtonIsSelected = !storeButtonIsSelected     performSegue(withIdentifier: "segueToStore", sender: self)   } override func prepare(for segue: UIStoryboardSegue, sender: Any?) {      segue.prepare(sender:sender)    }  func prepare(for segue: UIStoryboardSegue, from sender: Any?, toC vc: liveViewController) {            }  func prepare(for segue: UIStoryboardSegue, from sender: Any?, toB vc: storeViewController) {           if (segue.identifier == "segueToStore") {       navigationController?.setNavigationBarHidden(navigationController?.isNavigationBarHidden == false, animated: true)          }    }   @IBAction func unwindToViewController(unwindSegue: UIStoryboardSegue){     print("unwind")   } } After they clicked startLivePressed they will be met with the LiveViewController where the value that was stored in viewersTapped should be showing within the onlineViewers label. And if they were to leave the app and go back in and click startLivePressed the value should still be showing within the label. stopButton is a unwind button. language protocol SourceForC {   func prepare(for segue: UIStoryboardSegue, from sender: Any?, toC vc: liveViewController) } class liveViewController: UIViewController { let storeView = storeViewController()     @IBOutlet weak var stopButton: UIButton! @IBOutlet var onlineViewers: UILabel! override func viewDidLoad() {  super.viewDidLoad()        storeView.labelDelegate = self } } extension liveViewController: labelMaker {   func labelMaker(value: String) {     onlineViewers.text = value } } extension liveViewController : SegueDestination {      func prepareAsDestination( segue: UIStoryboardSegue, sender: Any?){           let sourceForC = segue.source as! SourceForC     sourceForC.prepare(for: segue, from: sender, toC: self)   } } Segue code in separate folder. language extension UIStoryboardSegue {  func prepare(sender : Any?) {     guard let destination = self.destination as? SegueDestination else{ fatalError("Destination \(self.destination) does not conform to SegueDestination protocol")}     destination.prepareAsDestination(segue: self, sender: sender)   } } extension UINavigationController : SegueDestination {   func prepareAsDestination( segue: UIStoryboardSegue, sender: Any?){     guard let destination = self.topViewController as? SegueDestination       else{ fatalError("Top view contoroller must conform to protocol SegueDestination")}           destination.prepareAsDestination(segue: segue, sender: sender)   } } protocol SegueDestination {   func prepareAsDestination( segue: UIStoryboardSegue, sender: Any?) }
Topic: Programming Languages SubTopic: Swift Tags:
Feb ’21