Trouble appending to array via segue - arrays

I'm trying to append to an array via segue:
#IBOutlet weak var addTextTextField: UITextField!
#IBAction func addTextButton(_ sender: UIButton)
{
performSegue(withIdentifier: "addTextSegue", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
let mainViewController = segue.destination as! MainViewController
mainViewController.array.append(addTextTextField.text!)
}
Its only appending to the array once, the next time I try, it overwrites the last entry. What is the reason for this?

Related

How can I get the Int for the value selected in the array using a table view?

I have a table view, and when I select an item I enter a screen to where I can edit the object, and then it will bring the item back into the ViewController where the array lives. I can't seem to get the index of the selected item in the array. I would imagine I the index lives in this function as I call the segue and then send over the object for editing.
Function:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.performSegue(withIdentifier: "goToEditArea", sender: self)
}
Here is where I get the array to send over.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let vc = segue.destination as! addArea
if let selectedIndexPath = areaTableView.indexPathForSelectedRow {
// Send over the area to update
vc.area = areaArray[selectedIndexPath.row]
}
}
Basically I just need the integer value for the object so I can save in the correct slot.
In your tableView(_:didSelectRowAt:) pass indexPath.row to the performSegue(withIdentifier:sender:):
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.performSegue(withIdentifier: "goToEditArea", sender: indexPath.row)
}
Then, you can get that value later in prepare(for:sender:):
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let vc = segue.destination as! addArea
if let row = sender as? Int {
vc.area = areaArray[row]
}
}

Why can't I assign data from the root view onto its embedded container view?

I have a DetailViewController that has a container view. The first container view that will appear is the DetailChildViewController.
The DetailChildViewController consists of a collection view which will display the child view's properties. However, even if I assign data onto the properties of the detailChildVC in the prepare(for segue:) function, the properties variable in the DetailChildViewController still returns nil. How do I fix this?
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Set the currentViewController to description child VC because that is the first one
// shown in the container view
if segue.identifier == "DetailChildSegue" {
let detailChildVC = DetailChildViewController()
// Set product of the childVC
let actionArray = [product?.action.keys.description] as? [String]
detailChildVC.properties = actionArray
}
// Set the currentVC
currentVC = segue.destination
}
You assign to a new var DetailChildViewController() not the destination
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "DetailChildSegue" {
let detailChildVC = segue.destination as! DetailChildViewController
let actionArray = [product?.action.keys.description] as? [String]
detailChildVC.properties = actionArray
}
// Set the currentVC
currentVC = segue.destination
}

Swift 3 triggering segue causes crash

Hi am trying to perform a segue by entering a specific word in a textfield.
So far my noob skills brought me to this.
I don't get an error, the simulator just crashes.
Any hints?
import UIKit
class CodewordVC: UIViewController, UITextFieldDelegate {
#IBOutlet weak var passWordInput: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
passWordInput.delegate = self
}
#IBAction func LosButton(_ sender: UIButton) {
//passwords
let realPassword1 = "rudi"
if passWordInput.text == realPassword1 {
self .performSegue(withIdentifier: "rudi", sender: self)
}
func dismissButton(_ sender: UIButton) {
dismiss(animated: true, completion: nil)
Got it to work!
Can't really explain, but that would be the answer:
import UIKit
class CodewordVC: UIViewController, UITextFieldDelegate {
#IBOutlet weak var passWordInput: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
let realPassword1 = "rudi"
#IBAction func LosButton(_ sender: UIButton) {
passWordInput.delegate = self
//passwords
if self.passWordInput.text == realPassword1 {
DispatchQueue.main.async(){
self.performSegue(withIdentifier: "rudi", sender: self)
}
}
}
func dismissButton(_ sender: UIButton) {
dismiss(animated: true, completion: nil)
}
}

Passing selected String in uiTableView from Array to uiLabel in new ViewController

I have my array, my tableview setup and the table shows the array of strings. 80+ strings in an array.
Now, I select a row, and obviously I want to pass the string in that row to a uiLabel in a secondary ViewController.
placeTypes is my declared array that is not in this posted code, just imagine an array with 84 strings! My segueIdentifier is A <- easy peasy. And passedString is the uiLabel that will receive the ..well...passed string data.
TableView code
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return placeTypes.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = placeTypes[indexPath.row]
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
NSLog("You selected cell number: \(indexPath.row)!")
performSegue(withIdentifier: "A", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "A" {
// let viewController = segue.destination as? ViewController
// SOMEHOW PASS THE SELECTED STRING IN THE SELECTED ROW TO THE VIEW CONTROLLER
}
}
Okay and now my ViewController code
#IBOutlet weak var passedString: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
EDIT: I am still getting errors. With the updated prepare for segue I get use of unresolved Identifier for placeTypes[indexPath.row] and viewController is telling me there is no sendString variable.
class TableViewController: UITableViewController {
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Row number: \(indexPath.row)")
performSegue(withIdentifier: "A", sender: indexPath)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "A" {
if let viewController = segue.destination as ViewController,
let indexPath = sender as IndexPath {
viewController.sendString = placeTypes[indexPath.row]
}
}
}
}
class ViewController : UIViewController {
#IBOutlet weak var passedString: UILabel!
// Put the string you need to send in a separate var
// You can't rely on the view controller being properly initalized
var sendString: String?
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let sendString = sendString {
passedString.text = sendString
}
}
}
You can also take a look at legendary-potato: https://github.com/ryantxr/legendary-potato
In did didSelectRowAt pass indexPath as sender and in prepare you can cast it to IndexPath. Try this.
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
NSLog("You selected cell number: \(indexPath.row)!")
performSegue(withIdentifier: "A", sender: indexPath)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "A" {
let indexPath = sender as? IndexPath {
let yourText = placeTypes[indexPath.row]
// let viewController = segue.destination as? ViewController
// SOMEHOW PASS THE SELECTED STRING IN THE SELECTED ROW TO THE VIEW CONTROLLER
}
}
}

Im getting the error = Cannot assign value of type 'String?' to type '[String]' Ive looked everywhere and i can't seem to find the answer

import UIKit
class FirstViewController: UIViewController
{
#IBOutlet var Dishname: UILabel!
#IBOutlet var TEXT: UITextField!
#IBOutlet var recipy: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let secondVC: SecondViewController = segue.destinationViewController as! SecondViewController
secondVC.items = TEXT.text! as? String //Error
// Cannot assign value of type 'String?' to type '[String]'
}
The items variable in SecondViewController is Array of String, [String] type and you try to assign a String to it rather than insert or append into it.

Resources