A
A
Artem_AK2015-04-10 19:55:06
Objective-C
Artem_AK, 2015-04-10 19:55:06

How to update ViewController using viewDidLoad?

As planned, the entered data after clicking on the "Save" button should be displayed in the table.
The logic is this - by pressing the "Save" button, the entered data is transferred to the database, then it executes viewDidLoad in which the current contents of the array + the entered data are loaded from the database.
At the moment, after pressing the "Save" button, a log is displayed with the contents of the array, in which the added element is present, and the controller table remains unchanged. The new element is displayed only after switching to the previous controller in the hierarchy and returning back.
What is the problem?
Below is the code and screenshot.
Thank you.
649a6b6e27ac461184ed95cd35cb7602.png

@interface SetVC ()

@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;

@property (weak, nonatomic) IBOutlet UITextField *kgTextField;
@property (weak, nonatomic) IBOutlet UITextField *repsTextField;
@property (weak, nonatomic) IBOutlet UITextField *noticeTextField;
@property (weak, nonatomic) IBOutlet UILabel *exerciseNameTextLabel;

@property (strong, nonatomic) NSArray *listOfSets;


@end

@implementation SetVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    AppDelegate *appDeligate = [[UIApplication sharedApplication] delegate];
    self.managedObjectContext = [appDeligate managedObjectContext];

    self.exerciseNameTextLabel.text = self.exerciseName;

    self.listOfSets = [DataManager retrieveListOfSetsByExerciseName:self.exerciseName withContext:self.managedObjectContext];
    NSLog(@"%@", self.listOfSets);
}



- (IBAction)saveButton:(UIButton *)sender {

    [DataManager addSetToExercise:self.kgTextField.text withReps:self.repsTextField.text withNotice:self.noticeTextField.text withExerciseName:self.exerciseName withContext:self.managedObjectContext];
    
    [self viewDidLoad];
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.listOfSets.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdentifier"];
    
    cell.textLabel.text = [[self.listOfSets objectAtIndex:indexPath.row] stringValue];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellIdentifier"];
    }
    
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
M
ManWithBear, 2015-04-10
@Artem_AK

You have porridge in your head. viedDidLoad is called on the controller when its view is fully loaded. Often, this happens generally once during the life of the controller.
In your case, you need a separate method that will load data from the database, and send the table [_table reloadData];
UPD I advise you to carefully read and understand this article ---> CLICK!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question