Reputation: 311
I am attempting to pass an object from one view controller to another and the object seems to be empty when the next view controller is called. I found other forums that recommended I use something similar to this to pass a my User
object.
var user: User!
override func viewDidLoad() {
super.viewDidLoad()
oauthswift = OAuth1Swift(
consumerKey: CONSUMER_KEY,
consumerSecret: CONSUMER_SECRET,
requestTokenUrl: "https://api.twitter.com/oauth/request_token",
authorizeUrl: "https://api.twitter.com/oauth/authorize",
accessTokenUrl: "https://api.twitter.com/oauth/access_token"
)
handle = oauthswift.authorize(
withCallbackURL: URL(string: "oauth-swift://oauth-callback/twitter")!,
success: { credential, response, parameters in
print("OAuthToken: \(credential.oauthToken)")
print("OAuthSecret: \(credential.oauthTokenSecret)")
print("User ID: \(parameters["user_id"]!)")
self.user = User(oauthswift: self.oauthswift, consumerKey: CONSUMER_KEY, consumerSecret: CONSUMER_SECRET, oAuthToken: credential.oauthToken, oAuthSecret: credential.oauthTokenSecret)
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let feedViewController = segue.destination as? FeedViewController {
feedViewController.user = self.user
}
}
self.performSegue(withIdentifier: "goToFeed", sender: self)
// Do your request
},
failure: { error in
print(error.localizedDescription)
print(self.handle)
}
)
// Do any additional setup after loading the view, typically from a nib.
}
When goToFeed
is executed, the User object is empty. I am performing the initialization within the closure so it would seem to me that the User should be fully populated. I assume the way I am using the segue function is incorrect? Any thoughts are appreciated.
Upvotes: 0
Views: 39
Reputation: 131398
You misunderstand how to use prepare(for:)
. It must be a top-level instance method, not a nested function. It will get called after you call performSegue(withIdentifier:)
:
var user: User!
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let feedViewController = segue.destination as? FeedViewController {
feedViewController.user = self.user
}
}
override func viewDidLoad() {
super.viewDidLoad()
oauthswift = OAuth1Swift(
consumerKey: CONSUMER_KEY,
consumerSecret: CONSUMER_SECRET,
requestTokenUrl: "https://api.twitter.com/oauth/request_token",
authorizeUrl: "https://api.twitter.com/oauth/authorize",
accessTokenUrl: "https://api.twitter.com/oauth/access_token"
)
handle = oauthswift.authorize(
withCallbackURL: URL(string: "oauth-swift://oauth-callback/twitter")!,
success: { credential, response, parameters in
print("OAuthToken: \(credential.oauthToken)")
print("OAuthSecret: \(credential.oauthTokenSecret)")
print("User ID: \(parameters["user_id"]!)")
self.user = User(oauthswift: self.oauthswift, consumerKey: CONSUMER_KEY, consumerSecret: CONSUMER_SECRET, oAuthToken: credential.oauthToken, oAuthSecret: credential.oauthTokenSecret)
self.performSegue(withIdentifier: "goToFeed", sender: self)
// Do your request
},
failure: { error in
print(error.localizedDescription)
print(self.handle)
}
)
// Do any additional setup after loading the view, typically from a nib.
}
Upvotes: 2