iphone - 无法设置委托(delegate)和调用方法

标签 iphone ios delegates storyboard

在关闭当前 View Controller 后,我创建了一个委托(delegate)将数据传回前一个 View Controller 。

这就是我设置委托(delegate)的方式

PaymentViewController.m

-(IBAction)techProcessAction:(id)sender
{
TechProcessPaymentVC *techVC = [[TechProcessPaymentVC alloc]init];
[techVC setTechDelegate:self];
 NSLog(@"DELEGATE == %@",techVC.techDelegate);

[self performSegueWithIdentifier:@"techProcess" sender:nil];
}

我可以看到委托(delegate)已设置
Snap2Door[1765:1c703] DELEGATE == <PaymentDetailViewController: 0x9d3a360>

但是当我在 TechProcessPaymentVC 中查看它时的viewDidLoad我收到 DELEGATE == (null)
我想这就是我的回调方法没有被调用的原因,它位于 PaymentViewController.m 中。 .

这就是我在 TechProcessPaymentVC.h 中定义委托(delegate)的方式
@protocol techProcessDelegate <NSObject>
-(void) techProcessViewControllerDismissed:(NSString *)paymentStatus;
@end

@interface TechProcessPaymentVC : UIViewController<UIWebViewDelegate>
{
id techDelegate;
}
@property (nonatomic, assign) id<techProcessDelegate>    techDelegate;

这就是我试图调用 PaymentViewController.m 中存在的方法的方式
if ([[substrings objectAtIndex:0] isEqualToString:@"failure"]) {
        [self.techDelegate techProcessViewControllerDismissed:@"failure"];
        [self dismissViewControllerAnimated:YES completion:nil];
    }

最佳答案

你的问题是你从来没有对 techVC 做任何事情。 .您分配它,分配它的委托(delegate),然后让它超出范围(它将在 ARC 中被释放或以其他方式泄漏)。

请尝试以下操作:首先,从 techProcessAction: 中删除实例化和赋值。 .二、落实prepareForSegue:sender:然后分配self成为techDelegate segue的destinationViewController .您的方法将类似于以下“代码”(我从内存中键入但没有尝试编译):

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"techProcess"]) {
        ((TechProcessPaymentVC *)segue.destinationViewController).techDelegate = self;
    }
}

-(IBAction)techProcessAction:(id)sender
{
    [self performSegueWithIdentifier:@"techProcess" sender:nil];
}

关于iphone - 无法设置委托(delegate)和调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18482170/

相关文章:

ios - 由于应用程序中超过自动锁定时间,如何检测手机已被锁定?

ios - sqlite3_prepare_v2没有这样的表错误

ios - 与 Parse 后端保持同步的离线数据存储用例

iphone - 尝试在 uilabel 上设置背景颜色动画以实现 'fade' 效果,但它会立即应用

iphone - iOS 中 Google Maps SDK 中的动画标记

iphone - 在 iOS 中使用 GCD

ios - 在场景 View 中显示 3D map 图像

iOS:从委托(delegate)中获取实例变量值(属性)

ios - 在 Objective C 中,A 类可以作为 B 类的委托(delegate),而 B 类可以作为 A 类的委托(delegate)吗?

c# - 通过反射在 C# 中获取委托(delegate)集合的所有具体实现