How to set UIView delegate?
Asked Answered
S

5

6

I have an UIViewController

class WelcomeViewController: UIViewController

and an UIView

class SignUpView: UIView

Now I want to set in my WelcomeViewController delegate of SignUpView:

protocol SegueDelegate {
    func runSegue(identifier: String)
}

class SignUpView: UIView { ... }

and connect it in

class WelcomeViewController: UIViewController, SegueDelegate {

how can I set in my WelcomeViiewController those delegate? When I'm trying to set:

override func viewDidLoad() {
    SignUpView.delegate = self
}

it returns me

Instance member 'delegate' cannot be used on type 'SignUpView'

how can I find a solution?

Slacken answered 3/3, 2016 at 9:4 Comment(1)
I assume the delegate property is not on the class-level but on the instance-level; am I right?Bridewell
M
4

You are trying to set delegate to a class. It should be an instance of the class i.e

let signUpView = SignUpView()
signUpView.delegate = self
Monoxide answered 3/3, 2016 at 9:13 Comment(14)
Yes, it works, but why my delegate function does not call? Can you please look at my code? It's short - pastebin.com/7J5bSTzdSlacken
Everything seems fine, can you check if signUp is really called and you have a segue with id "goToMainPage"Karajan
it does not even call my delegate function. Yes, I have such a segueSlacken
is signUp function called properly?Karajan
yes, those function calls successfully, but the delegate part - notSlacken
Oh I guess the problem is, signUpView should be an outlet. It means that you should create an outlet for this view by dragging from storyboard to view controller. it should be like @IBOutlet weak var signUpView: SignUpView! in the definition. Then set this inside viewDidLoad as signUpView.delegate = selfKarajan
the problem is that those SignUpView is a UIView XIB. So, I cannot create an outlet. Or I can?)Slacken
then you have to load the nib via loadNibNamed methodMulticolor
like this: WelcomeViewController(nibName: "SignUpView", bundle: nil)? It does not work, tooSlacken
Currently you are already using this SignUpView with your view controller. How did you make it?Karajan
I have a WelcomeViewController and the UIView XIB. When I click on button on the WelcomeVC I run a function with self.view.addSubview(xib). So my view becomes a xib. Later, when I click on a button on my UIView XIB, I want to segue from the WelcomeController to the MainViewControllerSlacken
Can you share all welcomeviewcontroller class, pieces of code doesn't make senseKarajan
Remove variable a from the class and just add signUpView.delegate = self in viewDidLoad that would be enoughKarajan
Let us continue this discussion in chat.Slacken
A
0

What would be the point in doing that? If you want to navigate from one View to another, just add that Segue in Storyboard with an Identifier, so you can call self.performSegueWithIdentifier("IdentifierOfSegue", sender: self)

Arrestment answered 3/3, 2016 at 9:8 Comment(0)
M
0

Create a weak property in SignUpView of that delegate(protocol) and name it other than delegate

then you can set and use it.

Multicolor answered 3/3, 2016 at 9:10 Comment(8)
Yes, it works, but why my delegate function does not call? Can you please look at my code? It's short - pastebin.com/7J5bSTzdSlacken
Please rename it segueDelegate and then checkMulticolor
this var delegate: SegueDelegate? variable name or where? Sorry I did not catch itSlacken
var segueDelegate : SegueDelegate?Multicolor
then set SignUpView. segueDelegate = selfMulticolor
is your view's name SignUpView in ViewController?Multicolor
then set SignUpView. segueDelegate = selfMulticolor
yes, I know about that :) No, it does not, unfortunatelySlacken
G
0

I agree with the developers saying "you can just do that via segue" but the problem is you didn't declare a delegate var in the SignUpView class so you can implement it in the signIn , if you declared it please write the line of code for me in a comment to check it for now ... I can suggest that you make a subview to be a parent class then override which method you want to call and you need to declare the delegate var as an optional (so you won't have a memory cycle) like the following line ...

var delegate: SegueDelegate?
Gentile answered 17/9, 2017 at 14:41 Comment(0)
G
0

Let's solve this for people in need whom could need a solution when reading this issue:

In your UIView:

class SignUpView: UIView

you need to add:

var delegate : SegueDelegate?

Now, still in your class SignUpView, you need to add the function you want to delegate, just like this:

func runSegue(identifier: String) {
    delegate?.runSegue(identifier)
}

This will call your delegate:

protocol SegueDelegate {
    func runSegue(identifier: String)
}

Now, in your ViewController, you should have your SignUpView somewhere (created programmatically or linked through Storyboard / XIB).

In your viewDidLoadfunction, add: signUpView.delegate = self.

Don't forget to add SegueDelegatein your class heritage.

Gretna answered 28/3, 2018 at 10:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.