设置委托对象中的委托方法没有响应
问题描述:
我对这个问题a custom delegate method inside didSelectRowAtIndexPath有点类似的问题。设置委托对象中的委托方法没有响应
但是,在我的情况下,在获得委托对象之前,它是一个名为BarCodeViewController的UIViewController,我应该首先通过2个视图控制器从初始视图控制器CardViewController这是一个表视图控制器。我通过这个设置为我的自定义委托的委托对象:
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
CardDetailsViewController *details = [self.storyboard instantiateViewControllerWithIdentifier:@"cardDetails"];
Card *selectedCard = [self.myWallet objectAtIndex:indexPath.row]; // I want this selected card to be accessible until the user clicks another card or during end of program.
[self.navigationController pushViewController:details animated:YES];
[self.delegate cardWalletViewController:self withCurrentCard:selectedCard];
[self setDelegate:self.barCodeVC]; // barCodeVC is declared in CardWalletViewController.h as @property (nonatomic, strong) BarCodeViewController *barCodeVC;
if (self.delegate) {
NSLog(@"delegate is not nil");
}
}
,这是我如何实例化,我设置为委托对象
- (void)viewDidLoad
{
[self setBarCodeVC:[self.storyboard instantiateViewControllerWithIdentifier:@"myBarcodeVC"]];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
而在我的委托对象视图控制器,这是BarCodeViewController我实现委托方法
#import "CardWalletViewController.h"
@interface BarCodeViewController() <CardWalletDelegate>
@end
@implementation
- (void)cardWalletViewController:(CardWalletViewController *)sender withCurrentCard:(Card *)currentCard
{
Card *myCurrentCard = currentCard;
NSLog(@"This is my current card: %@", myCurrentCard);
}
@end
我想我能够把我的委托对象,但后来没有被实施的委托方法我没有看到在控制台NSLog(@“这是我目前的......”);当我到达BarCodeViewController时。
请咨询。
答
这不是代表的标准用法,很难说出你真正想要发生的事情。但是,它看起来像你的代码...
[self.delegate cardWalletViewController:self withCurrentCard:selectedCard];
[self setDelegate:self.barCodeVC];
正在对任何“老”代表是(将其设置为barCodeVC之前)呼叫。你真的想要打电话给“新”代表吗?它应该是...
[self setDelegate:self.barCodeVC];
[self.delegate cardWalletViewController:self withCurrentCard:selectedCard];
编辑
我想说的是,你要发送消息给该行的委托......
[self.delegate cardWalletViewController:self withCurrentCard:selectedCard];
,然后你正在设置代理barCodeVC
[self setDelegate:self.barCodeVC];
所以,messag实际上,e被发送到代理设置为barCodeVC(另一个视图控制器或nil或...)之前设置的任何地方。也许这就是你想要发生的事情,但它看起来很可疑。
我希望发生的是将selectedCard传递给barCodeViewController,它是barCodeVC(编辑我的文章)。关于你的见解,我没有完全明白你的意思。 – Grauzten 2012-04-26 03:35:02
编辑:在[self setDelegate:self.barCodeVC]旁边添加了一条评论; – Grauzten 2012-04-26 03:42:22
明白了。 Ahm我放置了[self setDelegate:selfBarCodeVC];到viewDidLoad方法,以便我可以立即设置我的委托。但是,一旦我到达BarCodeViewController,委托方法仍然没有响应。请注意,在我可以访问BarCodeViewController之前,在单击CardWalletViewController中的表格单元格后,我会传递另一个表视图(名为CardDetailsViewController)。然后,一旦我点击另一个表格单元格(引用CardDetailsViewController),就会显示一个新的视图控制器,它是PerksDetailsViewController。 (接下来...) – Grauzten 2012-04-26 04:06:57