NotMe
NotMe

Reputation: 77

How to transfer button sender to another class

So I am building a simple messenger in swift. I am using a view state enum for different UIs upon viewWillAppear i.e .Offline, .Default. I have a convoVC and an InboxVC

From my InboxVC, I am transitioning to a convoVC which displays messages between the user and another user that is either selected from the inbox (in which case I want .Default state for conversationVC), or selected from a query using a searchbar when I transition from a "compose new message" button (in which case I want ".Compose" state for conversationVC).

I am having trouble setting the state on convoVC given the sender of my function which calls the segue from InboxVC to convoVC

sender of type UIBarbuttonItem if the compose button is pressed to make the transition, or sender of type myAppName.InboxVC if tableview cell is pressed to make the transition.

Code for what happens in each state is trivial, for lack of a better word.

This is my action function in InboxVC called from either my tableView's didSelectRowAtIndexPath method, or from barButtonItem selector:

func compose(sender: AnyObject) {
    self.performSegueWithIdentifier("toConversation", sender: sender)
}

I have tried capturing sender (within compose function, and within ViewWillDisappear but not both) in a variable declared within InboxVC and transferring it to another variable in convoVC to then check its type to set state in convoVC.

My problem is that it always comes out to nil. How do I transfer the sender? pls help I am new to swift. Thanks! (tagged obj-c because translating is not hard)

the variable is called sender in both VCs and is declared as type AnyObject?

I tried

self.sender = sender
convoVC().sender = sender

I also tried

self.sender = sender

and then setting sender (in convoVC) = InboxVC().sender

Upvotes: 1

Views: 177

Answers (2)

Mobile Apps Expert
Mobile Apps Expert

Reputation: 1048

func compose(sender: AnyObject) { self.performSegueWithIdentifier("toConversation", sender: sender) }

Here in compose(sender: AnyObject) sender is your button object and in self.performSegueWithIdentifier("toConversation", sender: sender) sender is your view controller.

So you havre to call
self.performSegueWithIdentifier("toConversation", sender: self) with self.

And to pass button object you can create a reference in your another class and in func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) method you should create object of that class and assign you button sender.

Upvotes: 0

Mobile Apps Expert
Mobile Apps Expert

Reputation: 1048

func compose(sender: AnyObject) {
    self.performSegueWithIdentifier("toConversation", sender: sender)
}

Here in compose(sender: AnyObject) sender is your button object and in self.performSegueWithIdentifier("toConversation", sender: sender) sender is your view controller.

So you have to call self.performSegueWithIdentifier("toConversation", sender: self) with self.

And to pass button object you can create a reference in your another class and in func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) method you should create object of that class and assign you button sender.

Upvotes: 1

Related Questions