I know using custom delegate
is an option for data traveling, but I want to know what's the best practice, also I have tried many other methods but the array in Destination ViewController
always shows nil
Asked
Active
Viewed 77 times
0

Gavin Miller
- 43,168
- 21
- 122
- 188
-
send the data via segue – Praveen Kumar May 16 '17 at 12:39
2 Answers
0
Basically, you need to bind the data with the destination viewcontroller.
You have to pass data like this:-
let otherUserInstance = self.storyboard?.instantiateViewControllerWithIdentifier("OtherUserProfileVC") as! OtherUserProfileVC
otherUserInstance.userID = self.data["UserId"]as! String //You can use array or dictionay instead of string
self.navigationController?.pushViewController(otherUserInstance, animated: true)
class OtherUserProfileVC: UIViewController {
var userID = "" //declare as property of class
}

Salman Ghumsani
- 3,647
- 2
- 21
- 34
-
actually, I am not performing any navigation, I have 2 view controllers appearing in the same view... – May 16 '17 at 13:24
-
Here we are giving solution on the basis of normal scenario if you are not cleared your question. So basically this answer only tells us that we need to `bind up` the data with the view controller instance. – Salman Ghumsani May 16 '17 at 13:30
-
-1
You may pass data by define array public like this way
var array: NSMutableArray!
class FirstViewController: UIViewController {
}
and then access it in other class like this way
class SecondViewController: UIViewController
{
override func viewDidLoad()
{
super.viewDidLoad()
array = [1, 2, 3]
}
}

Jignesh Mayani
- 6,937
- 1
- 20
- 36
-
-
Using NSMutableArray instead of a Swift array is bad practice. Using it as implicitly unwrapped optional is even worse. And using a global variable like this is also very, very bad practice. So... three errors in six lines. The downvote is not really surprising, in this case, I guess. ;) – Eric Aya May 16 '17 at 13:16
-
yeah using global variables is a very very bad practice.... but thnx for your response – May 16 '17 at 13:22