在关闭当前 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/