[英]unable to set delegate and call method
I created a delegate to pass data back to previous view controller after dismissing the current view controller. 我创建了一个委托,以在关闭当前视图控制器之后将数据传递回以前的视图控制器。
this is how I am setting the delegate in 这就是我在设置代表的方式
PaymentViewController.m PaymentViewController.m
-(IBAction)techProcessAction:(id)sender
{
TechProcessPaymentVC *techVC = [[TechProcessPaymentVC alloc]init];
[techVC setTechDelegate:self];
NSLog(@"DELEGATE == %@",techVC.techDelegate);
[self performSegueWithIdentifier:@"techProcess" sender:nil];
}
I can see that the delegate is set 我可以看到代表已设置
Snap2Door[1765:1c703] DELEGATE == <PaymentDetailViewController: 0x9d3a360>
but when I check it in TechProcessPaymentVC
's viewDidLoad
I'm getting DELEGATE == (null)
但是当我在TechProcessPaymentVC
的viewDidLoad
检查它时,我得到的是TechProcessPaymentVC
DELEGATE == (null)
I guess this is the reason my callback method is not called which is in PaymentViewController.m
. 我想这就是我的回调方法未在PaymentViewController.m
调用的原因。
this is how I defined the delegate in TechProcessPaymentVC.h
这就是我在TechProcessPaymentVC.h
定义委托的TechProcessPaymentVC.h
@protocol techProcessDelegate <NSObject>
-(void) techProcessViewControllerDismissed:(NSString *)paymentStatus;
@end
@interface TechProcessPaymentVC : UIViewController<UIWebViewDelegate>
{
id techDelegate;
}
@property (nonatomic, assign) id<techProcessDelegate> techDelegate;
and this is how I am trying to call the method which is present in PaymentViewController.m
这就是我试图调用PaymentViewController.m
存在的方法的方式
if ([[substrings objectAtIndex:0] isEqualToString:@"failure"]) {
[self.techDelegate techProcessViewControllerDismissed:@"failure"];
[self dismissViewControllerAnimated:YES completion:nil];
}
Your problem is that you don't ever do anything with techVC
. 您的问题是您永远不会对techVC
做任何事情。 You allocate it, assign its delegate, then let it go out of scope (it'll be deallocated in ARC or leaked otherwise). 您分配它,分配它的委托,然后让它超出范围(它将在ARC中释放或以其他方式泄漏)。
Try the following: First, remove the instantiation and assignment from techProcessAction:
. 请尝试以下操作:首先,从techProcessAction:
删除实例化和分配。 Second, implement prepareForSegue:sender:
and then assign self
to be the techDelegate
of the segue's destinationViewController
. 其次,实现prepareForSegue:sender:
techDelegate
然后将self
分配为segue的destinationViewController
的techDelegate
。 Your methods will look something like the following "code" (which I typed from memory and didn't try to compile): 您的方法看起来像下面的“代码”(我从内存中键入但未尝试编译):
- (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];
}
viewDidLoad
gets called when you init
the TechProcessPaymentVC
instance. init
TechProcessPaymentVC
实例时,将调用viewDidLoad
。 Delegate gets set after that. 之后设置代表。 Hence it is nil
. 因此,它为nil
。 Modify your init
method to initWithDelegate
and then assign the delegate
and use it in viewDidLoad
. 将您的init
方法修改为initWithDelegate
,然后分配delegate
并在viewDidLoad
使用它。
EDIT 编辑
Code: 码:
TechProcessPaymentVC.h TechProcessPaymentVC.h
@interface TechProcessPaymentVC : UIViewController
- (id) initWithDelegate:(id<techProcessDelegate>)delegate;
@end
TechProcessPaymentVC.m TechProcessPaymentVC.m
#import "TechProcessPaymentVC.h"
@implementation TechProcessPaymentVC
- (id) initWithDelegate:(id<techProcessDelegate>)delegate {
self = [super init];
if(self) {
self.techDelegate = delegate;
}
return self;
}
@end
Modify your call 修改通话
-(IBAction)techProcessAction:(id)sender
{
TechProcessPaymentVC *techVC = [[TechProcessPaymentVC alloc]initWithDelegate:self];
//....
}
Hope that helps! 希望有帮助!
If your destination view controller is wrapped into a navigation controller, you have to refer to it differently in prepareForSegue: 如果将目标视图控制器包装到导航控制器中,则必须在prepareForSegue中以不同的方式引用它:
UINavigationController *nav = segue.destinationViewController;
DetailViewController *dvc = [nav.viewControllers objectAtIndex:0];
In TechProcessPaymentVC.h: 在TechProcessPaymentVC.h中:
@interface TechProcessPaymentVC : UIViewController<UIWebViewDelegate>
{
id techDelegate_;
}
@property (nonatomic, assign) id<techProcessDelegate> techDelegate;
@end
In TechProcessPaymentVC.m: 在TechProcessPaymentVC.m中:
@implementation TechProcessPaymentVC
@synthesize techDelegate = techDelegate_;
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"%@", techDelegate_);
}
.
.
.
@end
Hope it's work for you!!! 希望它为您工作!!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.