0

Im a beginner, so I apologize for anything that seems overlooked or simple.

Basically, what I am trying to do is have a date picker on a screen update a UILabel, and then take that result of the update UILabel and push it back to another UILabel on the previous screen. I have the date picker working great and it updates the UILabel as expected, but I can't figure out how to code it so that I can update the first UILabel with the results from the DatePicker. Here is what i have for the DatePicker.

class StartDatePickerViewController: UIViewController {


    @IBOutlet weak var dateLabel: UILabel!
    @IBOutlet weak var datePicker: UIDatePicker!


    override func viewDidLoad() {
    super.viewDidLoad()

    datePicker.addTarget(self, action: Selector("datePickerChanged:"), forControlEvents: UIControlEvents.ValueChanged)

    // Do any additional setup after loading the view.
}

func datePickerChanged(datePicker:UIDatePicker) {
    var dateFormatter = NSDateFormatter()

    dateFormatter.dateStyle = NSDateFormatterStyle.ShortStyle

    var strDate = dateFormatter.stringFromDate(datePicker.date)
    dateLabel.text = strDate
}

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

I need StartDatePickerViewController to update my UILabel startDateLabel on the AddNewTradeViewController and I'm not sure how to approach this properly. Any advice would be greatly appreciated!

Dair
  • 15,910
  • 9
  • 62
  • 107
trever
  • 961
  • 2
  • 9
  • 28

3 Answers3

1

You can use NSNotification Add Observer or Delegation this will more better as compare to send data directly to that you should follow that pattern. When ever you are going to pop from second viewController to first-viewController on that attempt you can call Delegation pattern or NSNotification Observer(if you want to show your label data more than one place then you can use NSNotification). otherwise Delegation pattern will good for one to one message passing. Hope this will be helpful for you.

Vinod Pandey
  • 183
  • 4
0

You should use a pattern called delegation. This means, that you call a specified method on AddNewTradeViewController from datePickerChanged.

How to set up delegation in Swift can be found here.

Community
  • 1
  • 1
Tim Bodeit
  • 9,673
  • 3
  • 27
  • 57
0

What i understand from your question is you want to update the UILabel on First Screen when you push it back from SecondView.

if my understanding is right then please do as follows.

First define one NSString property in your First View Controller

@property (strong, nonatomic) NSString* lblString;

Now while popback to your firstviewContoller from your secondViewController just assign the updated value to its propery named as lblString.

UINavigationController maintain the list of all pushed controller in viewControllers and the root controller always be at 0.

Firstcontroller *firstcontroller = (Firstcontroller *)[self.navigationController.viewControllers objectAtIndex:0];
myController.lblString = @"Your Updated DatePickerValue" ;
[self.navigationController popToViewController:myController animated:YES];

now lblString have updated value when you popBack to firstViewcontroller and you can use wherever you want to use in firstViewController.

BhavikKama
  • 8,566
  • 12
  • 94
  • 164
  • That definitely sounds about right, all good answers. My issue now is, I have no idea how to transcribe into Swift just yet.. Haha any help there? – trever Jan 13 '15 at 21:12