Using blocks to pass data back to view controller

后端 未结 4 514
没有蜡笔的小新
没有蜡笔的小新 2021-01-06 11:20

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 t

相关标签:
4条回答
  • 2021-01-06 11:44

    I know that you asked specifically for a solution that did not involve prepareForSegue but this appears to be based on the assumption that prepareForSegue is for passing data forward only.

    There is something called an unwind segue which might be helpful in your situation. There is a detailed discussion on SO here.

    If you specifically want to use blocks for this, you can simply add a block property to your child controller and have the parent controller set the block. The child controller would have to invoke the block when it is being dismissed. Beware of retain loops if you do this. It doesn't sound to me like blocks are the best solution in this context but it's difficult to say something like that with authority without having a lot more context.

    0 讨论(0)
  • 2021-01-06 11:50

    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;
        };
    
    0 讨论(0)
  • 2021-01-06 11:52

    Yes methods can send data forward as well as backwards for that you can use Blocks or Delegates For more info about blocks in ios use this link CLICK HERE

    Hope it will help you, thnx

    0 讨论(0)
  • 2021-01-06 11:54

    In your view controller 1:

    MyViewControllerClass2* vc2 = [[MyViewControllerClass2 alloc] initWithNibName:@"UIViewController" bundle:[NSBundle mainBundle] completion:^{
        NSLog(@"view2 is closed");
    }]];
    [self.navigationController pushViewController:vc2 animated:YES];
    

    In MyViewControllerClass2.m:

    @interface MarryViewController ()
    
    @property (nonatomic, copy) void(^completion)();
    
    @end
    
    @implementation MarryViewController
    
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self)
        {
    
        }
        return self;
    }
    
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil completion:(void(^)())completion
    {
        self = [self initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    
        if( self )
        {
            //store completion block
            _completion = completion;
        }
    
        return self;
    }
    
    -(void)viewDidDisappear:(BOOL)animated
    {
        [super viewDidDisappear:animated];
    
        //call completion block
        _completion();
    }
    

    In MyViewControllerClass2.h:

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil completion:(void(^)())completion;
    

    Just a few notes on how awesome block are:

    1. MyViewControllerClass2 has no idea what is defined in _completion() which is the main point, as this is not of his concern

    2. You could also call _completion() in -dealloc or even on some place where MyViewControllerClass2 will continue to run

    3. You can pass arguments to block functions

    4. You can pass arguments from block functions

    5. Many more :)

    I'm really encouraging people to get good overview on blocks and stat using them as they are pretty cool.

    IMPORTANT!

    While using block you do not declare delegate, the main idea and abstract result of delegate method and using block are the same. Moreover delegate pattern has it's advantages such as better documenting and more strictly usage. Still blocks are way more flexible and(when get used to) easier to use.

    Regards,

    hris.to

    0 讨论(0)
提交回复
热议问题