Substract navigation bar subtitle's value with cell's detail value - ios

I am building a simple budget app, which will allow me to add specified budget and then considering my added expenses, calculate my actual outcome.
In Details Controller: 1, I want to be able to grab subtitle value from navigation bar, subtract it with value from cell's detailLabel and finally present in footer's label Wallet Balance. So far, I have been struggling with retrieving subtitle's value and doing calculation.
Here is my Main Controller: 2, which is used to send data forward to Details Controller.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destinationViewController = segue.destination as? AddBudgetViewController {
destinationViewController.delegate = self
}
// pass cell's label to detailVC
let destinationVC = segue.destination as? BudgetDetailsViewController
let cell = sender as? BudgetCell
destinationVC?.navigationItem.setTitle(title:(cell?.budgetNameLabel.text?.capitalized)!, subtitle:(cell?.detailTextLabel?.text)!)
}
AddBudgetViewController : using delegation to pass data back to main controller
protocol BudgetDelegate: class {
func enteredBudgetData(info: String, info2: String)
}
class AddBudgetViewController: UIViewController {
weak var delegate: BudgetDelegate? = nil
#IBOutlet weak var budgetName: UITextField!
#IBOutlet weak var budgetAmount: UITextField!
//
#IBAction func saveContent(_ sender: UIButton) {
if ((budgetName.text?.isEmpty)! && (budgetAmount.text?.isEmpty)!) {
_ = self.navigationController?.popViewController(animated: true)
} else {
delegate?.enteredBudgetData(info: budgetName.text!, info2: budgetAmount.text!)
_ = self.navigationController?.popViewController(animated: true)
}
}
Using didSelectRowAt instead segue method above
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//
let budget = self.budget[indexPath.row]
let viewController = UIStoryboard(name:"BudgetList", bundle: nil).instantiateViewController(withIdentifier: "DetailsController") as! BudgetDetailsViewController
viewController.budgets = budget
navigationController?.pushViewController(viewController, animated: true)
}
DetailsController
class BudgetDetailsViewController: UITableViewController, ExpenseDelegate {
#IBOutlet weak var walletBalance: UILabel!
var expense = [Expense]()
var budgets: Budget! {
didSet {
if isViewLoaded {
calculateWalletBudget(budget:, expense:)
}
}
}

Related

Why getting unexpected nil value passing data already loaded in tableview by a segue

well my problem is that idk how to pass the info values from the uitableviewcell to the anotherviewcontroller by the segue, could you please help, im unwraped the value, cause the data is already loaded idk what to do to pass the info to the popup controller without crashed
Here is my class model where i set the values
class MovieCell: UITableViewCell {
//
// MARK: - Class Constants
//
static let identifier = "MovieCell"
let urlImage = "https://image.tmdb.org/t/p/w500"
//
// MARK: - IBOutlets
//
#IBOutlet weak var title: UILabel!
#IBOutlet weak var rating: RatingView!
#IBOutlet weak var releaseDate: UILabel!
#IBOutlet weak var poster: UIImageView!
var titlePopUp: String = ""
func configure(movieDictionary: [String: Any]) {
title.text = (movieDictionary["title"] as! String)
titlePopUp = movieDictionary["title"] as! String
releaseDate.text = (movieDictionary["release_date"] as! String)
do {
let url = URL(string: "\(self.urlImage)" + "\(movieDictionary["backdrop_path"]!)")
let data = try Data(contentsOf: url!)
self.poster!.image = UIImage(data: data)
}
catch{
print(error)
}
}
}
heres is the viewcontroller where i get the error in the line 53
class ViewController: UIViewController,UITableViewDataSource, UICollectionViewDataSource, UITableViewDelegate {
var jsonArray: [Any] = []
let movieService = MovieService()
let popUpVC = popUpViewController()
#IBOutlet weak var moviesTableView: UITableView!
#IBOutlet weak var postersView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
movieService.fetchMovies { jsonArray in
if let jsonArray = jsonArray {
self.jsonArray = jsonArray
self.moviesTableView.reloadData()
self.postersView.reloadData()
}
}
self.moviesTableView.delegate = self
self.moviesTableView.dataSource = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
self.jsonArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:MovieCell = tableView.dequeueReusableCell(withIdentifier: "MovieCell", for: indexPath) as! MovieCell
cell.configure(movieDictionary: jsonArray[indexPath.row] as! [String: Any])
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
present(popUpVC, animated: true, completion: nil)
moviesTableView.deselectRow(at: indexPath, animated: true)
self.performSegue(withIdentifier: "popUp", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "popUp" {
let destinationVC = segue.destination as! popUpViewController
let data = MovieCell()
destinationVC.movieN = data.title.text!
}
}
}
and the popup controller is this one
class popUpViewController: UIViewController {
#IBOutlet weak var poster: UIImageView!
#IBOutlet weak var movieName: UILabel!
#IBOutlet weak var releaseDate: UILabel!
#IBOutlet weak var descriptionMovie: UILabel!
var movieN = String()
override func viewDidLoad() {
super.viewDidLoad()
movieName.text = movieN
// Do any additional setup after loading the view.
}
#IBAction func closePop(_ sender: UIButton) {
dismiss(animated: true, completion: nil)
}
}
im getting crash passing the label and image, the clasical error, unexpected nil value, but idk why the data is already loaded in the tableview main screen
You're not getting the data from the cell is because in the prepare method, you're creating a new instance of the cell.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "popUp" {
let destinationVC = segue.destination as! popUpViewController
let data = MovieCell() // This is where the issue is
destinationVC.movieN = data.title.text!
}
}
The newly created cell instance has no relationship with the one that's already displaying the data. In the new cell, all the properties are either empty or nil. That's why you're getting the 'unexpected nil value' error.
To get that cell's values, you should get a reference to that cell.
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let cell = tableView.cellForRow(at: indexPath) as? MovieCell else { return }
performSegue(withIdentifier: "popUp", sender: cell.titlePopUp)
}
Here you get the cell the user taps on and casts it to the MovieCell to access its titlePopUp property.
The you pass it to the prepare method in the sender parameter.
Finally in the prepare method, you cast it back to String (because the sender parameter is of type Any) and pass it to the popup view.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "popUp" {
let destinationVC = segue.destination as! popUpViewController
destinationVC.movieN = sender as? String
}
}
A few final notes: This way of passing around JSON as dictionaries will get the job done but will cause you massive headaches for you down the line if you have to change/maintain your code. What if later you have to pass another value of a movie along with the title to the popup? It's going to become harder and harder to scale this.
Do look in to object oriented way of programming. For example, create a class/struct called Movie to hold all the data (title, release date, rating etc). In the movie service, parse your JSON and create instances of that object and pass them up to the view controller.
i already solved sender was: cell not self
let cell = tableView.cellForRow(at: indexPath) as! MovieCell
self.performSegue(withIdentifier: "popUp", sender: cell)
and the data passed in the segue
if segue.identifier == "popUp" {
if let cell = sender as? MovieCell{
let destinationVC = segue.destination as! popUpViewController
destinationVC.movieN = cell.title.text!
let url = cell.urlImage + cell.posterPath
destinationVC.posterUrl = url
destinationVC.descriptionText = cell.descriptionMovie
destinationVC.releaseDateText = cell.releaseDate.text!
}
}

Manipulating from grandchild view controller - Swift Xcode

This is my first time using swift or Xcode.
I'm trying to make a simple transaction register app
The first view has a table, in which each row represents an account and it's balance.
When you click on a row, it opens up a second view, via segue, which contains a table of all transactions for that account. At the top of this view there is an 'Add Transaction' button, which opens up a third view, that has a form and an 'Add' button. When the 'Add' button is pressed, I use the .reloadData() on the table in the second view and the third view is dismissed.
But, the table, visually, does not have an additional row in it. Which is because after the 3rd view closes, the newly added transaction is no longer in the transactions array.
Am I doing something wrong? My attempt and images are below.
First view
import UIKit
class AccountsViewController: UIViewController {
#IBOutlet weak var newAccountNameUITextField: UITextField!
#IBOutlet weak var newAccountBalanceUITextField: UITextField!
#IBOutlet weak var addNewAccountUIButton: UIButton!
#IBOutlet weak var accountsUITableView: UITableView!
var selectedAccount: Account = Account(name: "", balance: "")
var accounts = [Account(name: "PNC", balance: "45.93")]
override func viewDidLoad() {
super.viewDidLoad()
accountsUITableView.delegate = self
accountsUITableView.dataSource = self
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
super.prepare(for: segue, sender: sender)
if let transactionsViewController = segue.destination as? TransactionsViewController {
transactionsViewController.modalPresentationStyle = .fullScreen
transactionsViewController.account = selectedAccount
}
}
}
extension AccountsViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedAccount = accounts[indexPath.row]
performSegue(withIdentifier: "trasactionsSegue", sender: self)
}
}
extension AccountsViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return accounts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "account", for: indexPath) as! AccountCell
cell.selectionStyle = .none
cell.nameUILabel?.text = accounts[indexPath.row].name
cell.balanceUILabel?.text = accounts[indexPath.row].balance
return cell
}
}
Second View
import UIKit
class TransactionsViewController: UIViewController {
#IBOutlet weak var nameUILabel: UILabel!
#IBOutlet weak var TransactionsUITableView: UITableView!
#IBOutlet weak var balanceUILabel: UILabel!
var account: Account = Account(name: "", balance: "", transactions: [])
override func viewDidLoad() {
super.viewDidLoad()
TransactionsUITableView.dataSource = self
nameUILabel.text = account.name
balanceUILabel.text = account.balance
}
//Pass data to newTransactionViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
super.prepare(for: segue, sender: sender)
if let newTransactionViewController = segue.destination as? NewTransactionViewController {
newTransactionViewController.account = account
}
}
//Dismiss this view when Accounts button is pressed
#IBAction func backToAccountsTouchUpInside(_ sender: UIButton) {
self.dismiss(animated: true, completion: {
self.presentingViewController?.dismiss(animated: true, completion: nil)
})
}
#IBAction func addTransactionTouchUpInside(_ sender: UIButton) {
performSegue(withIdentifier: "addTransactionSegue", sender: self)
}
#IBAction func unwindToViewControllerA(segue: UIStoryboardSegue) {
DispatchQueue.global(qos: .userInitiated).async {
DispatchQueue.main.async {
//At this point the newly added transaction is missing
self.TransactionsUITableView.reloadData()
}
}
}
}
extension TransactionsViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return account.transactions.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "transaction", for: indexPath) as! TransactionCell
cell.selectionStyle = .none
cell.descriptionUILabel.text = account.transactions[indexPath.row].description
cell.amountUILabel.text = account.transactions[indexPath.row].amount
cell.balanceUILabel.text = account.transactions[indexPath.row].balanceAfterAmount
return cell
}
}
Third View
import UIKit
class NewTransactionViewController: UIViewController {
#IBOutlet weak var clearedUISegmentedControl: UISegmentedControl!
#IBOutlet weak var depositingUISegmentedControl: UISegmentedControl!
#IBOutlet weak var descriptionUITextField: UITextField!
#IBOutlet weak var amountUITextField: UITextField!
#IBOutlet weak var addTransactionUIButton: UIButton!
var account: Account? = nil
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func addTransactionTouchUpInside(_ sender: UIButton) {
let depositing = depositingUISegmentedControl.selectedSegmentIndex == 0 ? true : false
let cleared = clearedUISegmentedControl.selectedSegmentIndex == 0 ? true : false
let description = descriptionUITextField.text
let amount = amountUITextField.text
let balanceAfterAmount = operationOnCurrency(depositing: depositing, amount: amount!, balance: account!.balance)
let newTransaction = Transaction(depositing: depositing, amount: amount!, balanceAfterAmount: balanceAfterAmount, description: description!, cleared: cleared)
account?.transactions.append(newTransaction)
self.performSegue(withIdentifier: "backToTransactions", sender: self)
}
}
func operationOnCurrency (depositing: Bool, amount: String, balance: String) -> String {
//Return empty string for now
return ""
}
The problem is that you are appending a new Transaction in the Account instance that was created in your NewTransactionViewController, rather than updating the data in the instance held by the TransactionsViewController or the root data source in the AccountsViewController (assuming that is the root data source). You need to pass the updated data backwards when the add button is pressed. You can create a delegate protocol to take care of this. Using your transition from NewTransactionViewController to TransactionsViewController example, first create the protocol:
protocol NewTransactionDelegate {
func transactionAddedToAccount(account: Account)
}
Then inside of your NewTransactionViewController you will want to create a delegate property:
class NewTransactionViewController: UIViewController {
#IBOutlet weak var clearedUISegmentedControl: UISegmentedControl!
#IBOutlet weak var depositingUISegmentedControl: UISegmentedControl!
#IBOutlet weak var descriptionUITextField: UITextField!
#IBOutlet weak var amountUITextField: UITextField!
#IBOutlet weak var addTransactionUIButton: UIButton!
var account: Account? = nil
**var delegate: NewTransactionDelegate?**
override func viewDidLoad() {
super.viewDidLoad()
}
And inside of your addTransactionTouchUpInside method call the delegate method:
#IBAction func addTransactionTouchUpInside(_ sender: UIButton) {
let depositing = depositingUISegmentedControl.selectedSegmentIndex == 0 ? true : false
let cleared = clearedUISegmentedControl.selectedSegmentIndex == 0 ? true : false
let description = descriptionUITextField.text
let amount = amountUITextField.text
let balanceAfterAmount = operationOnCurrency(depositing: depositing, amount: amount!, balance: account!.balance)
let newTransaction = Transaction(depositing: depositing, amount: amount!, balanceAfterAmount: balanceAfterAmount, description: description!, cleared: cleared)
account?.transactions.append(newTransaction)
**delegate?.transactionAddedToAccount(account: account)**
self.performSegue(withIdentifier: "backToTransactions", sender: self)
}
Now back in your TransactionsViewController you will want to conform to the NewTransactionDelegate protocol and implement the required method declared in the protocol:
class TransactionsViewController: UIViewController, NewTransactionDelegate {
func transactionAddedToAccount(account: Account) {
self.account = account
tableView.reloadData()
}
Then when you perform the segue to transition from TransactionsViewController to NewTransactionViewController you will want to set the destination view controller's delegate property to self:
//Pass data to newTransactionViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
super.prepare(for: segue, sender: sender)
if let newTransactionViewController = segue.destination as? NewTransactionViewController {
**newTransactionViewController.delegate = self**
newTransactionViewController.account = account
}
}
Now when the add button is tapped the delegate method is called and passed the new instance of account, which is then passed back to the previous view controller and updated.
Note that this will only update in account instance in the TransactionsViewController and you will also need to update the data for this account at the source or it will be lost when TransactionsViewController is deallocated. Pass the new account back to AccountsViewController, save to device, update database, etc.

Problem performing segue from tableview to viewcontroller

I am trying to perform a segue from a UITableView with news. If you push one of the news, it performs a segue to the specific news you selected.
It is easy and I have done it a few times... but I don't know what am I doing wrong this time.
The NewsDetailViewController is like this:
class NewsDetailViewController: UIViewController {
#IBOutlet weak var newsImage: UIImageView!
#IBOutlet weak var newsTitle: UILabel!
#IBOutlet weak var newsDate: UILabel!
#IBOutlet weak var newsText: UILabel!
var newsLink: String?
override func viewDidLoad() {
super.viewDidLoad()
// Hides the navigation bar.
self.navigationController?.setNavigationBarHidden(true, animated: false)
}
#IBAction func closeNews(_ sender: UIButton) {
navigationController?.popViewController(animated: true)
}
}
And the segue in the NewsTableViewController is like this:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("you selected the row: \(indexPath.row)")
tableView.deselectRow(at: indexPath, animated: true)
self.performSegue(withIdentifier: "goToNewsDetail", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToNewsDetail" {
if let destination = segue.destination as? NewsDetailViewController {
destination.newsLink = "whateverlink"
destination.newsTitle.text = "whatevertitle"
}
}
}
And the line: destination.newsLink = "whateverlink"
Works perfectly.
But the line: destination.newsTitle.text = "whatevertitle"
Throws a
fatal error: Unexpectedly found nil while implicitly unwrapping an
Optional value.
And I have no idea of what if going on. The same problem happens when trying to initialise the rest of the labels in the destination.
This line is the problem
destination.newsTitle.text = "whatevertitle"
don't access outlets of the destination vc as they not yet loaded send a string and assign it to the label in the destination vc
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToNewsDetail" {
if let destination = segue.destination as? NewsDetailViewController {
destination.newsLink = "whateverlink"
destination.toSend = "whatevertitle"
}
}
}
class NewsDetailViewController: UIViewController {
#IBOutlet weak var newsTitle:UILabel!
var toSend = ""
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.newsTitle.text = toSend
}
}
In the prepareForSegue method, the newsTitle label is still not initialised, so it is nil.
Generally, you shouldn't set the target VC's view's properties in prepareForSegue. You should declare a newsTitleText property in NewsDetailViewController:
var newsTitleText: String!
And set this property instead:
destination.newsTitleText = "whatevertitle"
Then set the newsTitle.text in viewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
// Hides the navigation bar.
self.navigationController?.setNavigationBarHidden(true, animated: false)
newsTitle.text = newsTitleText
}
When the prepare(for:sender:) method is called, NewsDetailViewController has not loaded the view yet so you can't set the text on a label. What you want to do is create another property on NewsDetailViewController such as var newsTitleText: String?. Then in viewDidLoad you can call newsTitle.text = newsTitleText.

Swift didSelectRowAt passing array values to second view

I am looking for ideas how to fix a problem I'm having with transposing data from my first view controller to the second view controller. The second view controller is being called when the user selects a table cell.
Code that populates the first tableview
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = mtgRates.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ViewControllerTableViewCell
cell.fiName.text = fetchedFiName[indexPath.row].fiName
cell.oneYear.text = fetchedFiName[indexPath.row].oneYear
cell.twoYear.text = fetchedFiName[indexPath.row].twoYear
cell.threeYear.text = fetchedFiName[indexPath.row].threeYear
cell.fourYear.text = fetchedFiName[indexPath.row].fourYear
cell.fiveYear.text = fetchedFiName[indexPath.row].fiveYear
return (cell)
}
I've watched many youtube videos but they all take a simple approach when setting up the data using an array set globally.
Code that I have been working but does nothing at this point.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
oneYearFound = self.fetchedFiName[indexPath.row].oneYear
twoYearFound = self.fetchedFiName[indexPath.row].twoYear
threeYearFound = self.fetchedFiName[indexPath.row].threeYear
fourYearFound = self.fetchedFiName[indexPath.row].fourYear
fiveYearFound = self.fetchedFiName[indexPath.row].fiveYear
performSegue(withIdentifier: "segue", sender: self)
}
I am thinking my issues is sending the fetched results to the second view controller
Thank you for any help!
More info based on the reply. You are correct I do have two view controllers on the storyboard. The code I have this far my UIViewController is
class SegueViewController: UIViewController {
#IBOutlet weak var V2TwoYear: UILabel!
#IBOutlet weak var V2FiveYear: UILabel!
#IBOutlet weak var V2FourYear: UILabel!
#IBOutlet weak var V2ThreeYear: UILabel!
#IBOutlet weak var V2OneYear: UILabel!
#IBOutlet weak var V2FiName: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
V2FiName.text = foundFi[myIndex].fiName
V2TwoYear.text = foundFi[myIndex].twoYear
V2OneYear.text = foundFi[myIndex].oneYear
V2ThreeYear.text = foundFi[myIndex].threeYear
V2FourYear.text = foundFi[myIndex].fourYear
V2FiName.text = foundFi[myIndex].fiveYear
}
Why don't you pass an instance of your fetchedFiName?
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedFiName = self.fetchedFiName[indexPath.row]
performSegue(withIdentifier: "segue", sender: selectedFiName)
}
Then, cast your sender as YOUR_FETCHED_FI_NAME_CLASS and pass it to your destination view controller in prepareForSegue:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let selectedFiName = sender as? YOUR_FETCHED_FI_NAME_CLASS,
destVC = segue.destination as? SegueViewController {
destVC.passedFiName = selectedFiName
}
}
Then, in your destination viewController and after ViewDidLoad (since your labels will not be loaded before that) you may use your passedFiName to populate your labels.
override func viewDidLoad() {
super.viewDidLoad()
updateLabels()
}
func updateLabels() {
V2FiName.text = passedFiName.fiName
V2TwoYear.text = passedFiName.twoYear
V2OneYear.text = passedFiName.oneYear
V2ThreeYear.text = passedFiName.threeYear
V2FourYear.text = passedFiName.fourYear
V2FiName.text = passedFiName.fiveYear
}
Update:
I continue to have problems with this. I think I am getting closer
Controller one code
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedFiName = self.fetchedFiName[indexPath.row].fiName
let selectedOneYear = self.fetchedFiName[indexPath.row].oneYear
let selectedTwoYear = self.fetchedFiName[indexPath.row].twoYear
let selectedThreeYear = self.fetchedFiName[indexPath.row].threeYear
let selectedFourYear = self.fetchedFiName[indexPath.row].fourYear
let selectedFiveYear = self.fetchedFiName[indexPath.row].fiveYear
passData = [SecondTable(passedFIName: selectedFiName, passedOneYear: selectedOneYear, passedTwoYear: selectedTwoYear, passedThreeYear: selectedThreeYear, passedFourYear: selectedFourYear, passedFiveYear: selectedFiveYear)]
performSegue(withIdentifier: "SecondViewController", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let selectedFiName = sender as! ,
let destVC = segue.destination as? SecondViewController {
destVC.fiName = selectedFiName
}
}
Second View Controller Code`
struct SecondTable {
var passedFIName: String = ""
var passedOneYear: String = ""
var passedTwoYear: String = ""
var passedThreeYear: String = ""
var passedFourYear: String = ""
var passedFiveYear: String = ""
}
class SecondViewController: UIViewController {
#IBOutlet weak var fiName: UILabel!
#IBOutlet weak var sometext: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let fiDetails = SecondTable()
fiName.text = .passedFIName
sometext.text = "Some Text"
}
}
I am getting error messages at` override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
I am nit sure what to put after "Sender as "Missing value"
I have been searching for hours. One I solve this problem. my project will start to move along. Thank you for all the great help!`
I need to make a few assumptions:
You have a storyboard with a seque with the identifier "seque"
You have a view controller at the end of that seque.
You need to send oneYearFound.... all 5 of those values to the SecondViewController. Lets just say that view controller has 5 UILabels that display the five values you want to send.
Sounds like SecondViewController needs some sort of object to hold those values. Define one on that view controller, and then create a prepareForSeque method on your FirstViewController. Source
In prepareForSeque, get the destination view controller off the seque and then pass it the object.
Then use the object to populate the values for the labels in ViewDidAppear or ViewDidLoad.
Edit:
It looks like you're really close. Make sure you dont forget to include the PrepareForSeque method in the first view controller. Inside that method override you can access the second view controllers instance and set those array values.
Here is some code that should help get you thinking.
class LabelsClass {
var str1:String;
var str2:String;
init(firstName:String, secondName:String) {
str1 = firstName;
str2 = secondName;
}
}
class SegueViewController: UIViewController {
var firstLabelString:String = ""
var secondLabelString:String = ""
func setValues(labels:LabelsClass) {
self.firstLabelString = labels.str1;
self.secondLabelString = labels.str2;
}
}
class MessageListViewController: UIViewController {
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
var first = "Game"
var second = "maniac"
var localLabelsVar = LabelsClass.init(firstName: first,secondName: second);
var destVC = segue.destination as? SegueViewController
destVC?.setValues(labels: localLobalsVar)
}
}
Sources:
How do I use prepareForSegue in swift?
How to pass prepareForSegue: an object

Error passing data between view controllers

I am trying to pass data from one view controller to another in Swift.
View Controller 1:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedFiName = self.fetchedFiName[indexPath.row].fiName
let selectedOneYear = self.fetchedFiName[indexPath.row].oneYear
let selectedTwoYear = self.fetchedFiName[indexPath.row].twoYear
let selectedThreeYear = self.fetchedFiName[indexPath.row].threeYear
let selectedFourYear = self.fetchedFiName[indexPath.row].fourYear
let selectedFiveYear = self.fetchedFiName[indexPath.row].fiveYear
passData = [SecondTable(passedFIName: selectedFiName, passedOneYear: selectedOneYear, passedTwoYear: selectedTwoYear, passedThreeYear: selectedThreeYear, passedFourYear: selectedFourYear, passedFiveYear: selectedFiveYear)]
performSegue(withIdentifier: "SecondViewController", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let selectedFiName = sender as! ,
let destVC = segue.destination as? SecondViewController {
destVC.fiName = selectedFiName
}
}
The error shows in "prepare for segue" in view controller 2 (receieving):
struct SecondTable {
var passedFIName: String = ""
var passedOneYear: String = ""
var passedTwoYear: String = ""
var passedThreeYear: String = ""
var passedFourYear: String = ""
var passedFiveYear: String = ""
}
class SecondViewController: UIViewController {
#IBOutlet weak var fiName: UILabel!
#IBOutlet weak var sometext: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let fiDetails = SecondTable()
fiName.text = SecondTable.passedFIName
sometext.text = "Some Text"
This bit:
if let selectedFiName = sender as! ,
Should not even compile.
You need a type, like
if let selectedFiName = sender as? String,
let destVC = segue.destination as? SecondViewController {
//Your code here
}
View1:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// create your struct object but in this example you're not passing an array so get rid of the [ ]
presentNextView(passing: passData)
}
func presentNextView(passing: SecondTable) {
let next = self.storyboard?.instantiateViewController(withIdentifier: "identifierHere") as! View2
next.fiDetails = passing
self.present(next, animated: true)
}
identifierHere is set in your main.storyboard, in View2 (the one you want to present) attribute inspector, under viewControllers class. I usually keep it the same as the ViewController name for simplicity but string must match perfectly or you will get a crash.
In View2 you simply need to declare your variables like this:
var fiDetails = SecondTable()
Do it at the start of the view controller class, not inside the viewDidLoad
I know it's not a segue but I find it a lot more convenient as you also don't need to make any connections in your storyboards, next view gets presented from the function rather then a segue.
If you want to pass the PassData struct to the second view controller, pass it as sender parameter in performSegue...
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let fetchedItem = self.fetchedFiName[indexPath.row]
let selectedFiName = fetchedItem.fiName
let selectedOneYear = fetchedItem.oneYear
let selectedTwoYear = fetchedItem.twoYear
let selectedThreeYear = fetchedItem.threeYear
let selectedFourYear = fetchedItem.fourYear
let selectedFiveYear = fetchedItem.fiveYear
passData = [SecondTable(passedFIName: selectedFiName, passedOneYear: selectedOneYear, passedTwoYear: selectedTwoYear, passedThreeYear: selectedThreeYear, passedFourYear: selectedFourYear, passedFiveYear: selectedFiveYear)]
performSegue(withIdentifier: "SecondViewController", sender: passData)
}
In the destination view controller create a property fiDetails rather than a local variable. Then assign the text to the text property of the label.
class SecondViewController: UIViewController {
#IBOutlet weak var fiName: UILabel!
#IBOutlet weak var sometext: UILabel!
var fiDetails = SecondTable()
override func viewDidLoad() {
super.viewDidLoad()
fiName.text = fiDetails.passedFIName
sometext.text = "Some Text"
In the source view controller in prepare(for get the struct from the sender parameter and assign it to the fiDetails property:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "SecondViewController" {
let destVC = segue.destination as! SecondViewController
let fiDetails = sender as! SecondTable
destVC.fiDetails = fiDetails
}
}

Resources