【发布时间】:2021-02-16 01:52:48
【问题描述】:
我有 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 委托方法中创建,如下所示:
@protocol CustomPopUpDelegate;
@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;
@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];
}
然后我无法理解如何在 viewController1 中没有全局实现 CustomPopUpDelegate 的情况下从 btn1Action 和 btn2Action 委派操作。
我需要从 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??
}
【问题讨论】:
-
在 vc2 中使用块 ...,有两个 ivars
@property (nonatomic,strong) void ( ^ action1 )( void );或您需要的任何东西,然后从 vc1 设置它们并在 vc2 中执行它们。 -
@skaak 谢谢你的回答,但你能解释一下吗,我不清楚。例如,在 vc2 中,我创建了
@property (nonatomic, strong) void ( ^ action1 )( void );,然后在 vc2 中,如果单击了按钮,我将调用方法[self action1];。在 vc1 中,我尝试创建这样的回调[vc action1:^ {}]但似乎有问题( -
好的,因为我需要更多空间,所以我会发布作为答案,但是块是这项工作的人......
标签: ios objective-c callback delegates