Iphone 集合委托对象中的委托方法没有响应
我对这个问题也有类似的担忧 但是,在我的情况下,在访问委托对象(名为BarCodeViewController的UIViewController)之前,我应该首先经过初始视图控制器(即CardViewController,即表视图控制器)中的2个视图控制器。我通过以下方式设置自定义委托的委托对象:Iphone 集合委托对象中的委托方法没有响应,iphone,ios,xcode,delegates,uitableview,Iphone,Ios,Xcode,Delegates,Uitableview,我对这个问题也有类似的担忧 但是,在我的情况下,在访问委托对象(名为BarCodeViewController的UIViewController)之前,我应该首先经过初始视图控制器(即CardViewController,即表视图控制器)中的2个视图控制器。我通过以下方式设置自定义委托的委托对象: - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
- (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.
}
在我的delegate对象(即BarCodeViewController)中,我实现了delegate方法
#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
#导入“CardWalletViewController.h”
@界面条形码视图控制器()
@结束
@实施
-(无效)cardWalletViewController:(cardWalletViewController*)当前卡发件人:(卡*)当前卡
{
卡片*myCurrentCard=currentCard;
NSLog(@“这是我的当前卡:%@”,myCurrentCard);
}
@结束
我想我可以设置我的委托对象,但是没有实现委托方法,因为我在控制台中没有看到NSLog(@“这是我当前的……”);当我到达条形码控制器时
请给出建议。这不是代表的标准用法,很难说出您真正想要发生什么。但是,看起来你的代码
[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];
因此,消息实际上被发送到代理在设置为barCodeVC(另一个视图控制器,或nil,或…)之前设置为的任何位置。也许这就是你想要发生的,但它看起来很可疑。我希望发生的是将所选卡片传递给BarCodeViewController,这就是barCodeVC(编辑我的帖子)。关于你的见解,我不太明白你的意思;明白了。Ahm我放置了[self-setDelegate:selfBarCodeVC];调用viewDidLoad方法,以便我可以立即设置委托。然而,一旦我到达BarCodeViewController,委托方法仍然没有响应。请注意,在转到BarCodeViewController之前,单击CardWalletViewController中的表格单元格后,我会经过另一个表格视图(名为CardDetailsViewController)。然后,当我单击另一个表格单元格(参考CardDetailsViewController)时,将显示一个新的视图控制器,即PerksDetailsViewController。(下面继续…)此PerksDetailsViewController中存在一个按钮,按下该按钮后,将按下条形码视图控制器。现在,当我到达BarCodeViewController时,我希望看到控制台中的日志显示NSLog(“这是我当前的卡%@”,myCurrentCard),这表明委托方法正在响应。是的,我可能误解了代码,但是,看起来您在将委托设置为BarCodeViewController之前调用了委托方法。