Performing a specific action (like setting a UIDatePicker's date) based on being segued from a specific Table View

StackOverflow https://stackoverflow.com/questions/22344638

I have what appears to be a simple problem.

I have a table view controller (part of a 2 tabbed tab bar) which is populated by the user tapping the plus button in the navigation bar and filling in some information. That takes the user to an "Add Entry" view controller.

I have a second table view (second tab of the tab bar) which also has a plus button in the navigation bar which also calls the Add Entry. However, with this table view, I am already populating a textField and the datePicker to be related to the information it came from.

In the prepareForSegue, I'm setting the date and the text field. That works. However, I'm not entirely sure where to place the code in the Add Entry to say "If you're called from tab 1, leave everything blank and if you're called from tab 2, set the date picker".

In the prepareForSegue:

if ([segue.identifier isEqualToString:@"Create New Entry From Event"])
{
    AddEntryViewController *addEntryViewController = (addEntryViewController *)segue.destinationViewController;
    [addEntryViewController setSelectedEvent:self.occasion.title];

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MMMM d, yyyy"];
    NSDate *dateFromString = [[NSDate alloc] init];
    dateFromString = [dateFormatter dateFromString:sectionTitle];
    [addEntryViewController setSelectedDate:dateFromString];
}

The setSelectedDate is:

- (void)setSelectedDate:(NSDate *)selectedDate
{
    _selectedDate = selectedDate;
}

If I set the viewWillAppear to:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.occasionTextField.text = self.selectedEvent;
    [self.datePicker setDate:self.selectedDate animated:YES];
}

I get a crash when calling the Add Entry from any other screen but this one which of course isn't desirable.

So I need a way to leave all text fields and the date picker as blank when called from anywhere in the app (which works without the self.datePicker line) and to only SET the datePicker when being called from THAT particular table view.

Any thoughts on this would be really great!

有帮助吗?

解决方案

in interface define a BOOL like this.

@property (assign, nonatomic) BOOL fromTabOne;

and add

@synthesize fromTabOne;

in viewWillAppear

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (!self.fromTabOne) {
      self.occasionTextField.text = self.selectedEvent;
      [self.datePicker setDate:self.selectedDate animated:YES];
    }
}

in prepareForSegue

if ([segue.identifier isEqualToString:@"Create New Entry From Event"])
{
    AddEntryViewController *addEntryViewController = (addEntryViewController *)segue.destinationViewController;
        [addEntryViewController setSelectedEvent:self.occasion.title];

    if (viewOne) { //if you're on first tab
       [addEntryViewController setFromTabOne:YES];
    } else {
       [addEntryViewController setFromTabOne:NO];
       NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
       [dateFormatter setDateFormat:@"MMMM d, yyyy"];
       NSDate *dateFromString = [[NSDate alloc] init];
       dateFromString = [dateFormatter dateFromString:sectionTitle];
       [addEntryViewController setSelectedDate:dateFromString];
    }
}

The error you mentioned in comment probably caused by a nil NSDate or wrong locale settings. Make a nil check before setting. I guess the NSDateFormatter couldn't format your string.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top