を定義popViewあなたが表示されたとき、私は2別々のクラス、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