0

I have two ViewController. First ViewController has a TableView. I present second ViewController programmatically modally. In second ViewController I download an image. Now I dismiss the second ViewController after completion of the download. And in the mean time I would like to update the TableView with the image and make that specific cell selected.

I have used delegate to do this process. But it could not update the TableView.

I used viewDidAppear, viewWillAppear method to reload the TableView. But no effect.

Can anybody suggest me with this specific task to complete?

I have presented my second ViewController by this way:

ImageSelectionViewController *viewController= [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"modal"];
viewController.modalPresentationStyle = UIModalPresentationCustom;
viewController.carDownloadName = [self.listOfItem objectAtIndex:sender.tag];

[self presentViewController:viewController animated:YES completion:nil];

and dismiss ViewController by this way :

[self dismissViewControllerAnimated:YES completion:nil];
EDUsta
  • 1,932
  • 2
  • 21
  • 30
Tanvir Nayem
  • 702
  • 10
  • 25

2 Answers2

0

you can use dismiss block

[self dismissViewControllerAnimated:YES completion:^{

        <do something on completion here>

    }];
Rajesh Dharani
  • 285
  • 4
  • 20
0

For sending a UIImage back do the following in ViewControllerA:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let viewControllerB = segue.destinationViewController as! ViewControllerB
    viewControllerB.callback = { message in
        //Do what you want in here!
    }
}

In ViewControllerB:

var callback : (String -> Void)?
@IBAction func done(sender: AnyObject) {
    callback?("Hi")
    self.dismissViewControllerAnimated(true, completion: nil)
}
Eric
  • 893
  • 10
  • 25
  • I can pass any data through delegate method of second view controller. After that I call [_tableView reloadData] method. But my tableview is not reloaded. It does not update the tableview at all. What should I do ? – Tanvir Nayem Mar 02 '17 at 17:47
  • Where is `reloadData` called? – Eric Mar 02 '17 at 19:23
  • i call reload data in first viewcontroller by implementing second view controller delegate method – Tanvir Nayem Mar 03 '17 at 11:53
  • A better approach would be to let the `UIViewController` be dismissed through an Unwind Segue declared in the `TableView`'s controller with `prepare(for: segue:)` programmed to pass the reference of UIImageView to another in the Unwind Segue. Delegate is avoided. – Eric Mar 08 '17 at 03:03
  • yes i figure it out in couple of ways. Just use the delegate to get the image value and set the delegate in where it was create the second class instance and presented modally. Use there classinstance.delegate = self. and then present the viewcontroller. – Tanvir Nayem Jul 03 '17 at 06:59