简体   繁体   中英

Call a method completion block after button is tapped

I've created a popupView with a button in a separate Class. When the button is tapped in popup view, it should go inside a completion handler.

Example. UIAlertAction actionWithTitle:...:handler^{ }; This opens an alertView with ok button. When the button is tapped i get to work inside completion handler.

Is it possible to create like that, if so how can i create this in Objective-C.

I know how to create with using @protocol and set the protocol for the class and call a method. I need to use this class for many view controller so I dont want to call protocol method where ever I implement the class.

You can try this with block(Objective C)

Create a property with block, write below code where you have created the pop view.

@property (nonatomic, strong) void (^actionHandler)(void);

In the button Action -

- (IBAction)doneButtonAction:(id)sender {
     _actionHandler();
}

// When you show popView you define the handler

[popView setActionHandler:^{
        // DO Whatever you want, you just need to write this whenever you show pop view, rest code only once. 
}];

To create a function with a completion handler

-- Swift 3 --

func action(completion:@escaping (_:Bool) -> Void) {

}

action { (success) in

}

-- Objective C --

- (void)action:(void (^)(bool success))completionHandler {

}

[self action:^(bool success) {

}];

I have created two seperate class, ViewController.h and PopupViewController.h

Please have look in the code:

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController


@end

ViewController.m

#import "ViewController.h"
#import "PopupViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(IBAction)btnOkPressed:(id)sender
{
    PopupViewController *master=[PopupViewController sharedInstance];
    [self presentViewController:master animated:YES completion:^{

    }];
    [master callCompletion:^(NSString *str) {
        NSLog(@"%@",str);
        [master dismissViewControllerAnimated:YES completion:^{

        }];
    }];
}


@end

PopupViewController.h

#import <UIKit/UIKit.h>

typedef void(^Completionhandler)(NSString* str);

@interface PopupViewController : UIViewController

+(PopupViewController*)sharedInstance;
-(void)callCompletion:(Completionhandler)handler;

@end

PopupViewController.m

#import "PopupViewController.h"

@interface PopupViewController ()
{
    @private
    Completionhandler _myHandler;
}

@end

@implementation PopupViewController

+(PopupViewController*)sharedInstance
{
    static PopupViewController *popup=nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        popup = [[PopupViewController alloc] initWithNibName:@"PopupViewController" bundle:nil];
    });
    return popup;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(IBAction)btnOkPressed:(id)sender
{
    _myHandler(@"test msg");
}

-(void)callCompletion:(Completionhandler)handler
{
    _myHandler = handler;
}

@end

I hope you have got your answer, if you have any doubt, feel free to ask me.. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM