Passing array through segue in swift
Your code looks fine, use the following prepareforsegue as it helps figure out where the problem could be
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "segueIdInStoryboard" {
if let DVC = segue.destinationViewController as? FirstViewController{
DVC.mySeguedArray = myIncomeArray
} else {
print("Data NOT Passed! destination vc is not set to firstVC")
}
} else { print("Id doesnt match with Storyboard segue Id") }
}
class FirstViewController: UIViewController
{
@IBOutlet weak var textView: UITextView!
var myIncomeArray: [Income]!
var mySeguedArray: [Income]!{
didSet{
myIncomeArray = mySeguedArray //no need to call viewDidLoad
}
}
}
Passing an array with prepareforsegue
Why don't you just allocate the view controller from the storyboard and pass the array in as a property of that view controller before you add it to the stack? i.e. avoid using prepareForSegue
-(void) buttonPressed:(UIButton*) sender
{
UIStoryBoard *story = [UIStoryboard storyboardWithName:@"Storyboard name"];
YourViewController *vc = [story instantiateViewControllerWithIdentifier:@"identifier"];
vc.array = <you array>
[self.navigationController pushViewController:vc animated:YES];
}
Not able to pass array to second view controller using segue
var model = [News]()
creates an empty array. You pass that to a new controller and then try to access the first element of this empty array. That's your issue.
Seems like there are no issues in passing it between controllers, you just try to access a nonexistent element. Plain old "index out of bounds" error, just like the error message told you. Add some objects and you will see that it works. If you want to check if it was passed at all (but that's a guarantee, no reason why it shouldn't), just do print(self.model)
, print(self.model.count)
or print(self.model.first?.title)
(this will print nil
since the first element doesn't exist, again.)
how can I pass a swift array by reference between viewControllers in prepare(for segue:?
Create wrapper class for your array and then pass that class reference wherever you want:
class ArrayWrapper {
let array: [YourType]
init(array: [YourType]) {
self.array = array
}
}
Pass Array of URLs to another View Controller via Segue
You're nearly there, your code just needs some tweaks for MacOS.
Your prepare for segue method would look like this:
override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
if (segue.identifier?.rawValue == "YourSegueName") {
//get a reference to the destination view controller
let destinationVC:MyViewController = segue.destinationController as! MyViewController
//set properties on the destination view controller
destinationVC.urls = [] //pass in urls here
}
}
You can see there are a few adjustments for MacOS, UIStoryboardSegue
->NSStoryboardSegue
, AnyObject
->Any
(This should be Any
now in iOS too), you'll need the rawValue
of the identifier
, destination
->destinationController
.
The ViewControllerClass
mentioned in your code refers to a class that you create that subclasses NSViewController
. I've created an example called MyViewController
, just to show you what it could look like:
class MyViewController: NSViewController {
var urls:[URL] = [] {
didSet(newValue) {
//do something here
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
In the storyboard to make all of this work, you would need to do a few things:
- Give the segue an identifier eg. "YourSegueName"
- Give the second view controller a class identifier eg. MyViewController
I recommend you read a tutorial on this, for example, Ray Wenderlich's MacOS View Controllers Tutorial.
Passing object with prepareForSegue Swift
You have to give the segue an identifier in the storyboard.(say mySegue
)
Using Xcode 10 swift 4.x(Also works with Xcode 9 & 8 , swift 3.x)
override func prepare(for segue: UIStoryboardSegue, sender: Any?){}
Is called for all segues being called from your current UIViewController
. So the identifier
is to differentiate the different segues
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "mySegue" ,
let nextScene = segue.destination as? VehicleDetailsTableViewController ,
let indexPath = self.tableView.indexPathForSelectedRow {
let selectedVehicle = vehicles[indexPath.row]
nextScene.currentVehicle = selectedVehicle
}
}
If you are using Using Xcode 7, swift 2.x
Then use this code:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "mySegue" {
var nextScene = segue.destinationViewController as! VehicleDetailsTableViewController
// Pass the selected object to the new view controller.
if let indexPath = self.tableView.indexPathForSelectedRow {
let selectedVehicle = vehicles[indexPath.row]
nextScene.currentVehicle = selectedVehicle
}
}
}
Place a breakpoint after nextScene
and see if it is being triggered by clicking any cell in the TableView
. If it isn't then the identifier name u provided in the storyboard must be different then the one given here.
PrepareForSegue with UITableViewController - Pass data
As pointed out by Arun, you need to use -prepareForSegue
. Here's what I'd do to pass something to your detail view controller (say meal.name
)
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Use this to determine the selected index path. You can also store the selected index path in a variable using -didSelectRowAtIndexPath
let cell = sender as! UITableViewCell
let indexPath = self.tableView.indexPathForCell(cell)
// Get the relevant detail
let meal = meals[indexPath.row]
// Create reference and pass it
let detailViewController = segue.destinationViewController as! MyDetailViewController
detailViewController.mealName = meal.name
}
And eventually, just hook up the UITableViewCell
with MyDetailViewController
using your Storyboard
(and select whatever presentation and transition style you prefer)
Edited for Swift 4
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let cell = sender as! UITableViewCell
let indexPath = self.tableView.indexPath(for: cell)
// Get the relevant detail
let meal = meals[indexPath!.row]
// Create reference and pass it
let detailViewController = segue.destination as! MyDetailViewController
detailViewController.mealName = meal.name
}
Related Topics
Adding Data to a Specific UId in Firebase
Avoid Equatable and Hashable Boilerplate, Swift 4.2
Sklabelnode Text with Two Different Fonts and Colour. How Is This Possible
Sandbox Entitlement to Script Itunes via Nsapplescript
Call Function on App Termination in Swift
Protocol Variable Implementing Another Protocol
Swift-Animate Cashapelayer Stroke Color
Swift: Draw a Semi-Sphere in Mkmapview
Automatic Select a Date in Datepicker in Swift Language
Turn Off Touch for Whole Screen, Spritekit, How
How to Bind My Array Controller to My Core Data Model
Allow Line Editing When Reading Input from The Command Line
iOS Coredata Compatible with Both iOS 9 and iOS 10
Swift 3/iOS 10/Todayextension - Userdefaults Always Returns Nil