我已經創建了兩個獨立的類,ViewController.h和PopupViewController.h處理程序
請看代碼:
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
我希望你已經得到你的答案,如果你有任何疑問,請隨時問我.. :)
來源
2017-03-16 11:11:57
SAM
我想這樣感謝您的答覆。 – Jaff