2

This is UIView file's owner of XIB

import UIKit

class sideBarContent: UIView  {

@IBOutlet weak var userName: UILabel!
@IBOutlet var sideBarContentView: UIView!
let mapviewcontroller = MapViewController()

@IBAction func userProfile(_ sender: UIButton) {

    mapviewcontroller.performSegueFromView(stringFromView: "userprofile")
}

Whereas inside my UIviewcontroller

class MapViewController: UIViewController
{
    func performSegueFromView(stringFromView:String){
    performSegue(withIdentifier: "\(stringFromView)", sender: nil)
    }
}

It says that my segue identifier does not exist, please help, i'm quite lost. And i couldn't perform segue in UIView as well, and i tried to use storyboard ID method too, but the present(vc,animated:true,completion:nil) not showing up.

Yuan
  • 107
  • 2
  • 13
  • `let mapviewcontroller = MapViewController()` That a NEW object, not the one you think of that is in the Storyboard with the segue. If the `sideBarContent` instance is an object of `MapViewController` you can use delegate pattern or closure to tell the parent instance of type `MapViewController` to perform the segue. – Larme Oct 12 '18 at 15:58
  • @Larme I tried to use the delegate to tell parent , however it say my delegate found nil, and it always nil. even though i put in something like this: delegate.dataSentBackToParent(dataString : "userprofile"). Please help.. – Yuan Oct 13 '18 at 01:05
  • Did you set the `delegate`? Edit your question with your current code. – Larme Oct 15 '18 at 06:00

1 Answers1

0

First things first, I think you should not access the view controller from your view. It is better to model the controller logic in the view controller rather than in the view.

That said, you can use a delegate pattern to implement your functionality.

import UIKit

class SideBarContent: UIView  {

    @IBOutlet weak var userName: UILabel!
    @IBOutlet var sideBarContentView: UIView!

    let delegate: SideBarContentDelegate? = nil

    @IBAction func userProfile(_ sender: UIButton) {
        self.delegate?.performSegueFromView(withIdentifier: "userprofile")
    }
}

protocol SideBarContentDelegate {
    func performSegueFromView(withIdentifier identifier: String)
}

class MapViewController: UIViewController, SideBarContentDelegate {

    @IBOutlet private weak var sidebarView: SideBarContent!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.sidebarView.delegate = self
    }

    func performSegueFromView(withIdentifier identifier: String) {
        self.performSegue(withIdentifier: identifier, sender: nil)
    }
}

More info : https://stackoverflow.com/a/1340470/2603900

Sudara
  • 4,769
  • 3
  • 34
  • 39