0

I have two buttons.one is @addIncomeTapped and the other one is @addExpenseTapped.what I want to do is to change the textcolor in my textfield once the user tapped (for example) the addIncomeTapped button. I'm trying to figure it out but have no luck :'( I'm hoping someone can help me? I'm still trying to learn swift more.thanks in advance.

import UIKit
class MySecondCell:UITableViewCell,UITextFieldDelegate
{    
    @IBOutlet weak var budgetTitle: UITextField!
    @IBOutlet weak var budgetAmount: UITextField!
}

class secondViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{

   var textfield: UITextField!    
   var SecondBudgetName = [String]()

   @IBOutlet weak var balanceView: UIView!
   @IBOutlet weak var mytableView: UITableView!

   override func viewDidLoad() {
    super.viewDidLoad()

    mytableView.delegate = self
    mytableView.dataSource = self

    view.addVerticalGradientLayer(topColor: primaryColor, bottomColor: secondaryColor)

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


@IBAction func addIncomeTapped(_ sender: Any) {
print("Add income tapped")
    //Add new row
    insert()
}

@IBAction func addExpenseTapped(_ sender: Any) {
print("Add expense tapped")
    //Add new row
    insert()

}

func insert(){
    SecondBudgetName.append("\(SecondBudgetName.count + 1)")
    let insertionIndexPath = IndexPath(row: SecondBudgetName.count - 1 , section: 0)
    mytableView.insertRows(at: [insertionIndexPath], with: .automatic)
}

//Configure TableView

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

    if editingStyle == UITableViewCellEditingStyle.delete {
        SecondBudgetName.remove(at: indexPath.row)
        tableView.beginUpdates()
        tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
        tableView.endUpdates()
    }
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return SecondBudgetName.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = mytableView.dequeueReusableCell(withIdentifier: "budgetCell") as! MySecondCell

    return cell

}

}

here is my tableviewcontroller

1 Answers1

0
  1. First class names start with capital.
  2. Second, about your code you will need to add some pieces of code:
  3. On secondViewController you will add two other declarations, var budgetTitleTextFieldColor = UIColor.black since black is default color for text or whatever color you're using var budgetAmountTextFieldColor = UIColor.black same as budgetTitleTextFielColor
  4. Add to

     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = mytableView.dequeueReusableCell(withIdentifier: "budgetCell") as! MySecondCell
    cell.budgetTitle.textColor = budgetTitleTextFieldColor
    cell.budgetAmount.textColor = budgetAmountTextFieldColor
    return cell
    }
    
  5. Then in button actions

    @IBAction func addIncomeTapped(_ sender: Any) {
        print("Add income tapped")
        //Add new row
        insert()
        // here you change the colors we declared in the beginning, it depends which one you want to change
        budgetTitleTextFieldColor = UIColor.whateverYourColorIs
        budgetAmountTextFieldColor = UIColor.whateverYourColorIs
        // don't forget this
        tableView.reloadData()}
     @IBAction func addExpenseTapped(_ sender: Any) {
           print("Add expense tapped")
           //Add new row
           insert()
           // here you change the colors we declared in the beginning
           budgetTitleTextFieldColor = UIColor.whateverYourColorIs
           budgetAmountTextFieldColor = UIColor.whateverYourColorIs
           // don't forget this
          tableView.reloadData()
    }
    
DionizB
  • 1,487
  • 2
  • 10
  • 20
  • thankyousomuch for the reply.i tried what you said but once I add "tableView.reloadData()"im getting this error "Ambiguous reference to member 'tableView(_:commit:forRowAt:)" .am i missing some code? –  Apr 17 '18 at 01:20
  • Your table view is named myTableView, so you have to change tableView.reloadData() to myTableView.reloadData() – DionizB Apr 17 '18 at 06:43
  • im sorry for asking so many questions but. why does the previous rows that I added also change.for example. i already have 5 inserted rows. and once I tapped the "add expense button" all the textcolors in rows becomes red, same as the "add income button" it all turns to green. :( maybe I should replace the reloadData. but I don't know what to use. –  Apr 17 '18 at 08:44
  • Since you reload table view data, every row will get reloaded and every row will get the colour as you set it. So which row colour do you want to change? – DionizB Apr 17 '18 at 08:49
  • what I want to do is. when I press "add income button" the text color of it will stay green. and when I press "add expense button" the new inserted rows text color will also stay red. –  Apr 17 '18 at 09:09
  • I don't know if I understood you properly, but try this in `tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)` add this `if indexPath.row == SecondBudgetName.count - 1 { // here set the color } else { // use default color }` – DionizB Apr 17 '18 at 09:14