1

I have a add button that takes you to a new view with text inputs to add info. once you hit add button it takes you back to the tableview and adds all inputs to the labels. I am having trouble making the current time pull into the dateStamp label I have made. can anyone help?

Main Controller

 var dateStamp = Date()
 var clientName = [""]
 var projecDescript = [""]

// Custom cell to make all input fields custom
class CustomCell: UITableViewCell {
    //Make your outlets here, connect the outlets from cell in your storyboard

    @IBOutlet var clientNameLabel: UILabel!
    @IBOutlet var descriptionLabel: UILabel!
    @IBOutlet var dateStamp: UILabel!

}

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet var clientTableList: UITableView!

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return (clientName.count)
        return (projecDescript.count)
    }

    // This is the new items added into the inputs
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: "clientCell", for: indexPath) as! CustomCell

        // Adds Clients Name
        let companyName = clientName[indexPath.row]
        cell.clientNameLabel?.text = companyName

        // Adds Clients Description
        let descriptionName = projecDescript[indexPath.row]
        cell.descriptionLabel?.text = descriptionName



        return cell
    }

    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }

    override func viewDidAppear(_ animated: Bool) {
        clientTableList.reloadData()
    }

Second Controller

import UIKit

class AddInvoice: UIViewController {

    @IBOutlet var clientNameInput: UITextField!
    @IBOutlet var descriptionNameInput: UITextView!



    @IBAction func addInvoice(_ sender: Any) {

        if clientNameInput.text != "" && descriptionNameInput.text != "" 
        {
            clientName.append(clientNameInput.text!)
            //clientInput.text = ""

            projecDescript.append(descriptionNameInput.text!)
            //descriptionFieldInput.text = ""


            _ = navigationController?.popViewController(animated: true)
        }  
    }
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Blue Moose
  • 125
  • 1
  • 15

2 Answers2

5
let timeStamp = "\(DateFormatter.localizedString(from: Date(), dateStyle: .long, timeStyle: .long))"

if you want to convert Format to as of Today

just pass the date to that function and it will return a string saying like 3 weeks ago

func relativePast(for date : Date) -> String {

    let units = Set<Calendar.Component>([.year, .month, .day, .hour, .minute, .second, .weekOfYear])
    let components = Calendar.current.dateComponents(units, from: date, to: Date())

    if components.year! > 0 {
        return "\(components.year!) " + (components.year! > 1 ? "years ago" : "year ago")

    } else if components.month! > 0 {
        return "\(components.month!) " + (components.month! > 1 ? "months ago" : "month ago")

    } else if components.weekOfYear! > 0 {
        return "\(components.weekOfYear!) " + (components.weekOfYear! > 1 ? "weeks ago" : "week ago")

    } else if (components.day! > 0) {
        return (components.day! > 1 ? "\(components.day!) days ago" : "Yesterday")

    } else if components.hour! > 0 {
        return "\(components.hour!) " + (components.hour! > 1 ? "hours ago" : "hour ago")

    } else if components.minute! > 0 {
        return "\(components.minute!) " + (components.minute! > 1 ? "minutes ago" : "minute ago")

    } else {
        return "\(components.second!) " + (components.second! > 1 ? "seconds ago" : "second ago")
    }
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Abdul Waheed
  • 863
  • 8
  • 14
  • Thank you this was what I was looking for! I can't up vote it till I have a reputation of 15, sorry about that! Is there a short hand way of making this say... 1 min ago. 1 day ago. 2 weeks ago, etc. or will I need to build a function for this – Blue Moose Aug 22 '17 at 01:41
  • Thats awesome! makes sense and is easier than what I was going to do! – Blue Moose Aug 24 '17 at 02:33
0

In order to do this you can use a delegate! At least thats what I learned.

Passing Data using the Delegate Method

Siyavash
  • 970
  • 9
  • 23
  • no I get that part. Its adding a time stamp that I am struggling with. – Blue Moose Aug 21 '17 at 02:26
  • hmm are you having trouble getting the timestamp? or you have the timestamp but don't know how to add it to the label? if its the second case then as It mentioned in the video when the user has clicked on the add button you get the current time stamp and send it back and then add it to label – Siyavash Aug 21 '17 at 02:37
  • no I think I'm struggling with getting a timestamp in the first place. I've looked online at several things, and everything I try it seems to not work. I made a var dateStamp = Date(). Now I'm trying to get that when I hit the add Invoice button – Blue Moose Aug 21 '17 at 03:32