Question

I'm writing a simple inventory app. It uses a navigation controller and the rootViewController is a UITableView. When you click on one of the rows in the tableView, it takes you to a detailsViewController, where it displays details of the item that was selected. At the bottom of this detailsViewController is a UILabel that displays the date that the time was created, along with a button that allows you to change the date. When this button is clicked, it pulls up another view controller with a datePicker.

How do I get the date label in the detailsView to display the new date that the user has picked?

Here's my attempt below. I'm relatively new to iOS programming so this will probably be a simple fix.

@implementation MPDetailViewController

- (IBAction)changeDate:(id)sender {
    MPDateViewController *dated = [[MPDateViewController alloc] init];
    [self.navigationController pushViewController:dated animated:YES];
}

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];

MPItem *item = self.item;
self.nameField.text = item.itemName;
self.serialNumberField.text = item.serialNumber;
self.valueField.text = [NSString stringWithFormat:@"%d", item.valueInDollars];

//turns a date into a simple string
if (!self.dateFormatter) {
    self.dateFormatter = [[NSDateFormatter alloc] init];
    self.dateFormatter.dateStyle = NSDateFormatterMediumStyle;
    self.dateFormatter.timeStyle = NSDateFormatterMediumStyle;
    }
self.dateLabel.text = [self.dateFormatter stringFromDate:item.dateCreated];
}

And this is the date view controller

@implementation MPDateViewController

- (IBAction)dateWasChanged:(id)sender {
    self.nDate = [self.changeDate date];
    NSLog(@"%@", self.nDate);
}

- (void) viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    [self.view endEditing:YES];
    MPItem *item = self.item;
    item.dateCreated = self.nDate;
    super.dateLabel.text = [super.dateFormatter stringFromDate:self.nDate];
}

I'd appreciate any help or comments that you are willing to provide.

Was it helpful?

Solution

You have to implement the Protocol for it. As Protocol is best practice to use in this kind of situation.

Here you can find the document for Protocol.

@protocol MPDateSelectedDelegate

- (void) MDPDateChanged: (NSDate*) date;

@end
@property (nonatomic, assign) id <MPDateSelectedDelegate> delegate;

You can call above delegate method when you change the date:

[self.delegate MDPDateChanged: newDate];

And You have to implement the same method in parent controller like this:

- (void) MDPDateChanged: (NSDate*) date
{
    // Do you stuff at here.
    // date will hold the new changed date from picker view.
    NSLog(@"%@",date.description);
}

OTHER TIPS

It seems that you are using IBActions to try and get User Interface events which occur in two different view controllers. Unfortunately this does not work. The easiest way (to me at least) to get information between two view controllers is to use delegates.

Here is a tutorial on how to use delegates.

What you could do is:

Change: @implementation MPDetailViewController To: @implementation MPDetailViewController <MPDateViewControllerDelegate>

Add the following lines to your MPDateViewController.h

@protocol MPDateViewControllerDelegate

- (void) MPDateViewControllerDidChangeDate: (NSString*) date; //I don't know your datatype here, but you can use it as an argument. I'm using NSString as an example

@end
@property (nonatomic, assign) id <MPDateViewControllerDelegate> delegate;

Finally, add this line once you do change your date in the DateViewController

[self.delegate MPDateViewControllerDidChangeDate: newDate];

I hope this helps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top