I have a UIViewController that when a button is pressed I open another View from a segue. When it opens everything is fine and all the functionality works. However, if I close the modal and then try to re-open it, self is nil.
This is the code I'm using to open the Modal:
self.performSegueWithIdentifier("Search", sender: self)
My modal has a segue defined as:
Present Modally
Over Full Screen
I then have a close button within the modal that does the following:
dismissViewControllerAnimated(true, completion: nil)
Finally, when I try to open the modal again with the "Search" segue self within the modal's UIViewController is nil .
Therefore, when I try to use it, I get this message:
fatal error: unexpectedly found nil while unwrapping an Optional value
I assume I'm keeping a reference to the UIViewController somewhere, I just don't know how.
Any advice is appreciated.
Okay,
So it turns out the code that I omitted was causing the issue. My apologies, I just did not think it would be caused by it, as it was working in all other use cases.
I am using Socket.IO to communicate with a NodeJS server in real time.
Within a socket.on:
var user = User.sharedInstance
user.socket!.on("search") {[weak self]data, ack in
var json = JSON(data!)
self.songs.removeAll() // <- Caused the error.
...
So it turns out that the [weak self]
causes the self to be nil if it was already set before. So the first time I would open the Modal, the user.socket!.on('search') {[weak self] data, ack in ...
worked fine, although the second time, when I referred to self
within the socket call again, it caused it to be null.
Thank you!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.