目标 c,单击按钮时来自 viewController 的回调

Objective c, callback from viewController when button is clicked

提问人:lqsdwtxkzlqafpvluq 提问时间:11/3/2020 更新时间:11/3/2020 访问量:316

问:

我有 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) 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];

}

然后我不明白我如何从全局实施和没有全局实施中委派操作。btn1Actionbtn2ActionCustomPopUpDelegateviewController1

我需要在同一个地方从 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??    

    }
iOS Objective-C 回调 委托

评论

1赞 skaak 11/3/2020
使用块...在 VC2 中,有两个 IVAR 或任何你需要的东西,然后从 VC1 设置它们并在 VC2 中执行它们。@property (nonatomic,strong) void ( ^ action1 )( void );
0赞 lqsdwtxkzlqafpvluq 11/3/2020
@skaak感谢您的回答,但您能否解释更多,我不清楚。例如,在 vc2 中,我做了,然后在 vc2 中,如果单击了一个按钮,我会调用方法。在 vc1 中,我试图创建这样的回调,但似乎有些不对劲(@property (nonatomic, strong) void ( ^ action1 )( void );[self action1];[vc action1:^ {}]
1赞 skaak 11/3/2020
好的,我会发布作为答案,因为我需要更多空间,但块是这项工作的人......

答:

1赞 skaak 11/3/2020 #1

块是你在这里使用的。你可以说,它们是为此而生的。

在需要回调的位置创建一个 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/,这是一个不错的网站。

评论

1赞 skaak 11/3/2020
很好 - 我确定以前有人问过这个问题,但您的代码正在乞求块,并且很容易将这些片段插入代码中的正确位置。但同样,也许我在某个地方弄错了,所以请告诉我,我会修复它......
1赞 skaak 11/3/2020
很棒的东西 - 你的想法是正确的,我可以看到在你的代码中,你只是缺乏技术。现在你知道了 - 它很棒并且使用了很多,例如所有这些完成处理程序。有时你也会使用委托,但这有点重 - 块很好,很轻量级。谢谢