Objective c,单击按钮时从 viewController 回调
Objective c, callback from viewController when button is clicked
我有 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];
}
然后 我无法理解 我如何在 [=26= 中没有全球实施 CustomPopUpDelegate
的情况下从 btn1Action
和 btn2Action
委派操作].
我需要在同一个地方从 viewController2“本地”编写回调,例如:
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??
}
Blocks 就是你在这里使用的。可以说,它们就是为此而生。
在你想要回调的地方创建一个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 );
...等等。
然后你 link 这些阻止你的行动,例如你的情况
- (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。
然后,如您所称,将其命名为 'locally',请按照以下示例更改您的代码。
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/.
中,这里是一个不错的网站
我有 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];
}
然后 我无法理解 我如何在 [=26= 中没有全球实施 CustomPopUpDelegate
的情况下从 btn1Action
和 btn2Action
委派操作].
我需要在同一个地方从 viewController2“本地”编写回调,例如:
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??
}
Blocks 就是你在这里使用的。可以说,它们就是为此而生。
在你想要回调的地方创建一个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 );
...等等。
然后你 link 这些阻止你的行动,例如你的情况
- (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。
然后,如您所称,将其命名为 'locally',请按照以下示例更改您的代码。
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/.
中,这里是一个不错的网站