问题描述
我有viewController1-主视图控制器和viewController2-这是我的customPopUp。我想在单击button1或button2时从viewController2创建回调。
viewController1代码中的
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
CustomPopUpViewController *vc = (CustomPopUpViewController *)[storyboard instantiateViewControllerWithIdentifier:@"CustomPopUpViewControllerID"];
vc.textAlert = text;
vc.btn1TxtAlert = btn1Txt;
vc.isAlertOneBtn = YES;
我想做这样的事情:
vc.button1Callback {
}
vc.button2Callback {
}
-
viewController2
viewController2.h
@interface CustomPopUpViewController : UIViewController
@property (nonatomic,assign) Nsstring* titleAlert;
@property (nonatomic,assign) Nsstring* textAlert;
@property (nonatomic,assign) Nsstring* btn1TxtAlert;
@property (nonatomic,assign) Nsstring* btn2TxtAlert;
@property (nonatomic,assign) BOOL isAlertOneBtn;
viewController2.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
_UIbtn2Txt.layer.borderWidth = 2.0f;
_UIbtn2Txt.layer.borderColor = [UIColor colorWithRed:0.09 green:0.53 blue:0.00 alpha:1.0].CGColor;
_textLbl.text = _textAlert;
[_UIbtn1Txt setTitle:_btn1TxtAlert forState:UIControlStatenormal];
[_UIbtn2Txt setTitle:_btn2TxtAlert forState:UIControlStatenormal];
if(_isAlertOneBtn) {
_UIbtn2Txt.hidden = YES;
}
}
- (IBAction)btnAction1:(id)sender {
[self dismissViewControllerAnimated:true completion:nil];
// how i can do callback from this place
}
- (IBAction)btnAction2:(id)sender {
[self dismissViewControllerAnimated:true completion:nil];
// how i can do callback from this place
}
我考虑使用viewController2.h
delegete方法创建,就像这样:
@protocol CustomPopUpDelegate;
@interface CustomPopUpViewController : UIViewController
@property (nonatomic,assign) BOOL isAlertOneBtn;
@property (weak)id <CustomPopUpDelegate> delegate;
@end
@protocol CustomPopUpDelegate <NSObject >
- (id) btn1Action;
- (id) btn2Action;
@end
在此地点更改
- (IBAction)btnAction1:(id)sender {
[self dismissViewControllerAnimated:true completion:nil];
[self.delegate btn1Action];
}
- (IBAction)btnAction2:(id)sender {
[self dismissViewControllerAnimated:true completion:nil];
[self.delegate btn2Action];
}
然后我无法理解,如何在btn1Action
中没有全局实现btn2Action
的情况下从CustomPopUpDelegate
和viewController1
委派动作。
我需要在同一位置从viewController2“ localy”编写回调,例如:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
CustomPopUpViewController *vc = (CustomPopUpViewController *)[storyboard instantiateViewControllerWithIdentifier:@"CustomPopUpViewControllerID"];
vc.textAlert = text;
vc.btn1TxtAlert = btn1Txt;
vc.isAlertOneBtn = YES;
vc.button1Callback {
// how write like this??
}
vc.button2Callback {
// how write like this??
}
解决方法
块是您在这里使用的。您可以说,它们是为此而制成的。
在需要回调的位置创建一个ivar。现在,回调是一个块,您将在需要时执行该代码。因此,块的类型将根据此而变化。这是一些例子...
@property (nonatomic,strong) void ( ^ callbackAction1 )( BOOL ); // Block is named callbackAction1 - it takes a BOOL arg and returns nothing
@property (nonatomic,strong) BOOL ( ^ callbackAction2 )( BOOL ); // Block is named callbackAction2 - it takes a BOOL and returns a BOOL
@property (nonatomic,strong) void ( ^ callbackAction3 )( void ); // Block is named callbackAction3 - it takes nothing and returns nothing
// Real life example
@property (nonatomic,strong) void ( ^ valueChanged )( XXBooleanCollectionViewCell *,BOOL );
...等等。
然后将这些块链接到您的操作,例如就您而言
- (IBAction)btnAction1:(id)sender {
[self dismissViewControllerAnimated:true completion:nil];
if ( self.callbackAction1 ) {
// Here we use the block
self.callbackAction1 ( YES ); // or whatever you need ...
}
}
有时,如果UI要求您可以在完成块中执行该块,并且如果完成块的格式正确,则您甚至可以将该块作为完成块传递,因此
- (IBAction)btnAction1:(id)sender {
[self dismissViewControllerAnimated:true completion:callbackAction3];
}
完成后将调用callbackAction3。
然后,要像调用它一样在本地调用它,请按照以下示例更改代码。
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
CustomPopUpViewController *vc = (CustomPopUpViewController *)[storyboard instantiateViewControllerWithIdentifier:@"CustomPopUpViewControllerID"];
vc.textAlert = text;
vc.btn1TxtAlert = btn1Txt;
vc.isAlertOneBtn = YES;
vc.button1Callback {
// how write like this??
}
vc.button2Callback {
// how write like this??
}
// This is how
vc.callbackAction1 = ^ ( BOOL p ) {
if ( p ) something ... the logic of the callback goes in here
};
vc.callbackAction2 = ^ BOOL { ... callback logic ... };
vc.callbackAction3 = ^ { ... };
这是此类事物的常见模式。我对您的逻辑尚不完全清楚,我的回答可能无法完全奏效,所以请告诉我,我会相应地进行更新,但这是工作的重点。
根据您的评论,我认为您的语法有误,应该是
vc.action1 = ^{ ... };
一旦您陷入语法http://goshdarnblocksyntax.com/中,这里就是一个不错的网站。