在两个tableview控制器之间传递一个数字对象:IOS

时间:2012-01-06 12:47:27

标签: objective-c uitableview uiviewcontroller parameter-passing

我是一个新手试图将从视图控制器1中的表中选择的行号传递给第二个视图控制器。

我正在尝试使用VC1中数字的属性声明来执行此操作:

@property (nonatomic, retain) NSNumber *passedSectorNumber;

然后在VC1中进行@synthesized并在VC1的didSelectRowatIndexPath中使用适当的行号进行设置:

self.passedSectorNumber = [NSNumber numberWithInt:[indexPath row]];
        VC2 *vc2 = [[SectorEditor alloc] initWithNibName:@"vc2nibname" bundle:nil];
        [self.navigationController pushViewController:vc2 animated:YES];
        [vc2 release];

在VC2中,我还定义了一个具有相同名称的NSNumber属性,并将其合成。

在VC2中也是:

@property (nonatomic, retain) NSNumber *passedSectorNumber;

我在VC 2中测试传递的值:

NSInteger intvalue = [self.passedSectorNumber integerValue];
    NSLog(@"The value of the integer is: %i", intvalue);

无论选择哪一行,VC2中“已收到”的数字始终为“0”。

我正在犯一个菜鸟错误。知道在哪里?非常感谢投入。

1 个答案:

答案 0 :(得分:0)

假设您的第二个VC名为SectorEditor:

self.passedSectorNumber = [NSNumber numberWithInt:[indexPath row]];
VC2 *vc2 = [[SectorEditor alloc] initWithNibName:@"vc2nibname" bundle:nil];
[self.navigationController pushViewController:vc2 animated:YES];
[vc2 release];

应该是这样的:

VC2 *vc2 = [[SectorEditor alloc] initWithNibName:@"vc2nibname" bundle:nil];
vc2.passedSectorNumber = [NSNumber numberWithInt:[indexPath row]];
[self.navigationController pushViewController:vc2 animated:YES];
[vc2 release];

或者甚至更好,在第二个VC中声明一个名为initWithPassedNumber的类方法,并在其中调用initWithNibName,如下所示:

- initWithPassedSectorNumber:(NSInteger)sectorNumber
{
    if ((self = [super initWithNibName:@"vc2nibname" bundle:nil])) {
         self.passedSectorNumber = sectorNumber
    }
}

然后对此的调用将是这样的:

VC2 *vc2 = [[SectorEditor alloc] initWithPassedSectorNumber:indexPath.row bundle:nil];
[self.navigationController pushViewController:vc2 animated:YES];
[vc2 release];

没有测试任何代码,但这将接近你所需要的。

相关问题