调用setDelegate时发生NSInvalidArgumentException错误

时间:2012-03-27 12:16:05

标签: ios

我正试图从iOS5中的uitableview转到另一个屏幕。我已经设置了一个似乎有效的委托等(发生了segue)但我想我需要“设置委托来初始化我想要在新屏幕中显示的数据。虽然我调用它时遇到NSInvalidArgumentException错误prepareforsegue。

以下是uitableview部分的代码......

#import "iTanksV2ListViewController.h"
#import "tank.h"
#import "tankDetailViewController.h"

@interface iTanksV2ListViewController () 
@property tank *selectedTank;
@end

@implementation iTanksV2ListViewController
@synthesize tanks = _tanks;
@synthesize tankTableView = _tankTableView;
@synthesize delegate = _delegate;
@synthesize selectedTank = _selectedTank;


- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
    // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.tankTableView.delegate = self;
    self.tankTableView.dataSource = self;
}

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.identifier isEqualToString:@"Show Tank Details"])
    {
        UILabel *myLabel = [[UILabel alloc] init];
        myLabel.text = self.selectedTank.tankNumber;
        [segue.destinationViewController setTankNumberLabel:myLabel];
        [segue.destinationViewController setDelegate:self]; ///this is where it fails!!!
    }
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.selectedTank = [self.tanks objectAtIndex:indexPath.row];
    [self.delegate iTanksListViewController:self choseTank:self.selectedTank];
}

然后在详细视图中我使用以下内容...

-(void)iTanksListViewController:(iTanksV2ListViewController *)sender choseTank:(id)tank
{
    self.tankToShow = tank;
}

但这不会被调用 - 大概是因为我没有成功调用setdelegate方法?!

2 个答案:

答案 0 :(得分:0)

您不能合成您的委托属性。此外,请确保您的头文件正确具有引用的委托协议,如

@interface TankDetailViewController : UITableViewController <DELEGATEPROTOCOL>

答案 1 :(得分:0)

我以为我有...这个片段来自itanksv2listviewcontroller标题:

@interface iTanksV2ListViewController : UITableViewController
@property (nonatomic, strong) NSArray *tanks;
@property (weak, nonatomic) IBOutlet UITableView *tankTableView;
@property (weak, nonatomic) id <iTanksV2ListViewControllerDelegate> delegate;
@end

这来自m文件:

@synthesize delegate = _delegate; 

这就是我在detailview m文件中添加的内容:

@interface tankDetailViewController () <iTanksV2ListViewControllerDelegate>
@end