assing object with prepareForSegue becomes nil in Swift 3 - ios

Sir,
I am trying to implement a form and pass the Data object below
import UIKit
import GRDB
class Staff: Record {
var id: Int64?
var compId: Int64 = 0
var chiName: String = ""
var engName: String = ""
to the table view controller loading the child record. when it comes to implementation, it seems getting null and does not make sense. Would you please tell me how to ensure the second view controller does not receive null objects under this case ?
Below is the
Log :
Here is my code:
First UIView Controller
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
print("view salary X ")
print(dummy)
print(dummy.id ?? "0")
if let secondController = segue.destination as? ViewSalaryTableViewController {
secondController.dummyStaff = dummy
}
}
Second UITableView Controller :
public var dummyStaff : Staff?
override func viewDidLoad() {
super.viewDidLoad()
..
print("arrive dummyStaff")
print(dummyStaff ?? "njull")
}
Storyboard partial draft :
Storyboard setting

Make sure the type casting for secondController is working. If you have multiple segues, use segue identifier to distinguish. Below code worked fine for me:
class MyBook {
var name:String!
}
ViewController 1
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "Vc1ToVc2" {
let book = MyBook()
book.name = "Harry"
if let destinationVc = segue.destination as? ViewController2 {
destinationVc.book = book
}
}
}
ViewController 2
var book:MyBook?
override func viewDidLoad() {
super.viewDidLoad()
print(book?.name ?? "No name")
}
Prints: Harry

Related

Can not pass Data To ContainerView

I have tried 2 methods to pass the data from ViewController to ContainerView, with and without segue
Here is without segue method
ViewController
class DetailPostBookReviewVC: UIViewController {
var postid: String!
#IBAction func Menubutton(_ sender: Any) {
print(postid!)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "MenuBookReviewVC") as! MenuBookReviewVC
vc.favpostid = postid
}
ContainerView
class MenuBookReviewVC: UIViewController {
var favpostid = String()
#IBAction func Deletepost(_ sender: Any) {
print(favpostid)
}
}
result: favposid has Nill Value
UPDATE this is with segue method
class DetailPostBookReviewVC: UIViewController {
var postid: String!
#IBAction func Menubutton(_ sender: Any) {
print(postid!)
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){
if (segue.identifier == "toMenuBookReviewVC") { //"toMenuBookReviewVC" is identifier
let vc = segue.destination as! MenuBookReviewVC
vc.favpostid = postid!
}
}
Pass your data like. User prepare(for:sender:)
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if
segue.identifier == "MyIdentifierInStorybiard", // Set that
let controller = segue.destination as? MenuBookReviewVC {
controller.favpostid = postid
}
}
I think you postid is not String type so print the null value
In this way, you can't pass data for the container view. if in this way without presenting controller and push controller you can use the global variable then direct pass data and use any controller you want to use.
Example
import UIKit
class ViewController: UIViewController {
var postid: String!
override func viewDidLoad() {
super.viewDidLoad()
postid = "23" // You change your post ID
}
#IBAction func Menubutton(_ sender: Any) {
favpostid = postid
}
}
var favpostid : String!
class MenuBookReviewVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
print(favpostid)
}
}
For Passing Data To Container View you can use this
UserDefaults.standard.set(value, forKey: "SomeKey")
after your data is used you can clear that default value.
UserDefaults.standard.set("", forKey: "SomeKey")

Fatal error when trying to pass data to another view controller

In order to practice my networking, I built an app with a text field where you can input something. I use the wikipedia API to fetch the definition of that term / name/ expression. My goal is to then display that definition into another view controller.
A button performs the segue to the new view controller, where a label displays that definition.
The get request works, but when tapping the button, I get a fatalError : "Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value".
I would like to add that the error is displayed in the "prepare for segue" function.
Here is the code for my first view controller
import UIKit
import Alamofire
import SwiftyJSON
class ViewController: UIViewController {
#IBOutlet weak var textEntryLabel: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
//MARK: - Relevant variables
let wikipediaURl = "https://en.wikipedia.org/w/api.php"
var termDefinitionInfo: String = ""
let segueName: String = "toDefinition"
#IBAction func buttonToDefinition(_ sender: UIButton) {
// on fait la requete ici
httpCall(termDefinition: textEntryLabel.text ?? "nothing to pass")
performSegue(withIdentifier: segueName , sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == segueName {
let secondVC = segue.destination as! DefinitionViewController
secondVC.definitionLabel.text = termDefinitionInfo
}
}
//MARK: - NETWORKING
func httpCall(termDefinition: String) {
let parameters : [String:String] = [
"format" : "json",
"action" : "query",
"prop" : "extracts",
"exintro" : "",
"explaintext" : "",
"titles" : termDefinition,
"indexpageids" : "",
"redirects" : "1",
]
//
request(wikipediaURl, method: .get, parameters: parameters).responseJSON { (response) in
if response.result.isSuccess {
//1. on affiche le tableau json initial
let definitionJSON: JSON = JSON(response.result.value)
print(definitionJSON)
// deux valeurs : pageID et definition
let pageId = definitionJSON["query"]["pageids"][0].stringValue
let pageDefinition = definitionJSON["query"]["pages"][pageId]["extract"].stringValue
self.termDefinitionInfo = pageDefinition
print(self.termDefinitionInfo)
} else {
print("Error! Could not fetch data!")
}
}
}
}
Here is the code for the second view controller
import SwiftyJSON
import Alamofire
class DefinitionViewController: UIViewController {
#IBOutlet weak var definitionLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}```
Tip: Try to avoid force down casting
In your case you are trying to assign a value to an IBOutlet when it's not wired to its parent view controller. You better do this:
class DefinitionViewController: UIViewController {
#IBOutlet weak var definitionLabel: UILabel!
var labelValue: String?
override func viewDidLoad() {
super.viewDidLoad()
definitionLabel.text = labelValue
}
}
And in your first view:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == segueName {
if let secondVC = segue.destination as? DefinitionViewController {
secondVC.labelValue = termDefinitionInfo
}
}
}

Can't get the value of a var in another class

I'm trying to get the value of a String var from an another class, but when i'm using it on the new class, the value is empty.
I've got the MainViewController.swift class with :
var movieIDSelected = String()
#IBAction func tapPosterButton(_ sender: UIButton) {
switch sender.tag
{
case 101: movieIDSelected = theaterMovieID[0]
print(movieIDSelected) //The value isn't empty
break
}
}
And the second MovieViewController.swift class with :
var HomeView = ViewPop()
override func viewDidLoad() {
super.viewDidLoad()
let movieID = HomeView.movieIDSelected
print(movieID) //The value is empty
}
With your current code try this in MainVC
if let home = UIApplication.shared.keyWindow?.rootViewController as? ViewPop {
print("home exists ",home.movieIDSelected)
}
//
but you should have only 1 segue to the destinationVC and link it to the VC not a segue for every button , then implement prepareForSegue and fire performSegue inside the button action to make the segue
//
#IBAction func tapPosterButton(_ sender: UIButton) {
switch sender.tag
{
case 101: movieIDSelected = theaterMovieID[0]
print(movieIDSelected) //The value isn't empty
self.performSegue(withIdentifier: "goToNext", sender:1)
break
}
}
//
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let des = segue.destination as! MovieViewController
des.sendedValue = self.movieIDSelected
des.buttonNumber = sender as! Int
}
//
class MovieViewController : UIViewController {
var sendedValue = ""
var buttonNumber = 0 // default value won't affect
}

How to access variables when preparing for segue?

I'm preparing for a Segue (just learning at the moment!) and want to pass an array, which is created in a function, through a segue to arrive at the new view controller.
It's working fine if I just put a text string in there, but when I try change to an array it is blank - I think because it can't access the array because it's outside the scope - but I'm stuck on how to do it
Here's the initial VC code :
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToWorkout" {
if let destVC = segue.destination as? WorkoutViewController {
destVC.workoutName = (testArray)
}
}
//Get the new view controller using segue.destinationViewController.
//Pass the selected object to the new view controller.
}
func testfunction() {
let testArray = ["blah","blah","ploop"]
}
and the 'receiving VC code'
class WorkoutViewController: UIViewController {
var workoutName = [String]()
override func viewDidLoad() {
super.viewDidLoad()
print(workoutName)
// Do any additional setup after loading the view.
}
I'm nearly there with it but think I must be missing something basic. How do you do this passing arrays/other variables created in functions?
If the function had more than 1 variable/array, would that change the approach? I.e. one function might produce the exercises in the workout AND the number of reps for example
You can send it in sender
self.performSegue(withIdentifier: "goToWorkout ", sender: testArray)
and in
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToWorkout" {
if let destVC = segue.destination as? WorkoutViewController {
destVC.workoutName = sender as! [String]
}
}
Yes, both occurrences of testArray are not in the same scope and the compiler complains.
Declare the function this way
func testfunction() -> [String] {
let testArray = ["blah","blah","ploop"]
return testArray
}
and assign the array by calling the function
destVC.workoutName = testfunction()
Your issue is caused by testArray being a local variable defined inside the testfunction function making it only accessible from inside the function. If you want to make a variable accessible from everywhere inside the class, you'll have to make it an instance property.
class InitialVC: UIViewController {
let testArray = ["blah","blah","ploop"]
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToWorkout" {
if let destVC = segue.destination as? WorkoutViewController {
destVC.workoutName = testArray
}
}
//Get the new view controller using segue.destinationViewController.
//Pass the selected object to the new view controller.
}
}
When saying:
destVC.workoutName = (testArray)
in the prepare(for segue: UIStoryboardSegue, sender: Any?) method, it definitely doesn't refers to testArray variable in testfunction(), since it is a local variable. If you are not getting a compile time error at the above line, then probably your declaring testArray somewhere in the view controller or maybe globally in the app.
So what you could do is to declare testfunction() as testfunction() -> [String]:
func testfunction() -> [String] {
return["blah","blah","ploop"]
}
thus:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToWorkout" {
if let destVC = segue.destination as? WorkoutViewController {
destVC.workoutName = testfunction()
}
}
}
func testfunction() -> [String] {
return ["blah","blah","ploop"]
}
I guess this is what you want. Functions / methods can return values. It can be achieved by the -> syntax.
Now you can use like:
destVC.workoutName = testfunction()
Few notes
Functions, methods and variables should be named through camel case notation. So, instead of testfunction you should write testFunction (maybe choose a better name also).
Do not forget to read Apple documentation on the subject: Functions.

swift - return and pass multiple arrays and an Int via Segue

I'm nearly there with a small basic program I'm writing (still learning) and I've hit a roadblock.
I can now pass 1 array between 2 view controllers and successfully print it when I hit a button in the 2nd one.
However, what I really want to do is pass 2 arrays and an Integer, created from a function on the first VC and have them accessible via the 2nd VC.
Code for 1st VC is here :
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToWorkout" {
if let destVC = segue.destination as? WorkoutViewController {
destVC.bothArrays = self.testFunction()
}
}
//Get the new view controller using segue.destinationViewController.
//Pass the selected object to the new view controller.
}
func testFunction() -> [String] {
let randomArray1 = ["blah","blah","ploop"]
let randomArray2 = ["alan", "john"]
let randomInt = 5
return BOTH ARRAYS AND INT TO SEND TO THE NEXT VIEW CONTROLLER?
}
#IBAction func goPressed(_ sender: UIButton) {
performSegue(withIdentifier: "goToNextVC", sender: self)
}
and 2nd VC here :
class WorkoutViewController: UIViewController {
var randomArray1 = [String]()
var randomArray2 = [String]()
var randomInt = 0
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func nowButtonPressed(_ sender: UIButton) {
print(randomArray1)
print(randomArray2)
print(randomInt)
}
}
I can get it working with just one array but I need more than one array and a value to be passed! I've tried playing around with it (e.g. trying '-> [String], [String], Int) but no luck
Any help much appreciated!
You can simply use a tuple to include several variables of different types in a single variable. You should pass the tuple including all 3 variables in your performSegue function as the sender argument, then assign them to the relevant instance properties in the prepare(for:) method.
If you want to keep the function for generating the variables, you should change the return type of the function to a tuple that can fit the 3 variables.
func testFunction() -> (arr1:[String],arr2:[String],randInt:Int) {
let randomArray1 = ["blah","blah","ploop"]
let randomArray2 = ["alan", "john"]
let randomInt = 5
return (randomArray1, randomArray2, randomInt)
}
Then assign the return value of testFunction to the sender input argument of performSegue:
performSegue(withIdentifier: "goToNextVC", sender: testFunction())
Assign the variables:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToWorkout" {
if let destVC = segue.destination as? WorkoutViewController, let variablesToBePassed = sender as? (arr1:[String],arr2:[String],randInt:Int) {
destVC.randomArray1 = variablesToBePassed.arr1
destVC.randomArray2 = variablesToBePassed.arr2
destVC.randomInt = variablesToBePassed.randInt
}
}
}
As others have suggested, you can refactor your function to return a tuple, and then use that to pass to your other view controller:
//This is the tuple data type we use to pass 2 string arrays and an Int
typealias parameterTuple = ([String], [String], Int)
func testFunction() -> parameterTuple {
let randomArray1 = ["blah","blah","ploop"]
let randomArray2 = ["alan", "john"]
let randomInt = 5
return (randomArray1, randomArray2, randomInt)
}
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToWorkout" {
if let destVC = segue.destination as? WorkoutViewController {
//refactor WorkoutViewController to have a parameters property
//of type parameterTuple, split out the tuple and pass each part to
//a different property in your WorkoutViewController
destVC.parameters = testFunction()
}
}
}

Resources