Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using blocks to pass data back to view controller

I was looking at this question.

One of the answers shows how to use blocks to pass data backwards view the prepareForSegue method. My understanding is this method should really be used to pass data forward, not backwards.

I'm wanting to try blocks out for this purpose - passing data back to another viewController.

My question is: How do I do this, without using prepareForSegue method? I could use for instance in a UITableView - didselectRowAtIndexPath and dismiss the view - but then how does the receiving view get "notified" there is data to come back, without using a delegate?

like image 499
Robert J. Clegg Avatar asked Dec 02 '22 17:12

Robert J. Clegg


1 Answers

sent data backward

  1. declare block in your secondViewController.h file

    @property (nonatomic, copy)void(^myBlock)(NSString *);

  2. call block wherever you need to pass data from .m file of secondViewController

    myBlock(@"this will displayed in firstViewController");

3.import above .h file in your firstViewController .m file and define your block as

secondViewController *ref =[[secondViewController alloc ]init];
  
     ref.myBlock =^void(NSString *data)
    {
    self.labelOffirstviewcontroller=data;
    };
like image 85
Anurag Bhakuni Avatar answered Dec 21 '22 06:12

Anurag Bhakuni