【问题标题】:Passing a number object between two tableview controllers: IOS在两个 tableview 控制器之间传递一个数字对象:IOS
【发布时间】:2012-01-06 12:47:27
【问题描述】:

我是一个新手,试图将从视图控制器 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 中“received”的数字始终为“0”,无论选择哪一行。

我犯了一个新手错误。知道在哪里吗?非常感谢您的输入。

【问题讨论】:

  • didSelectRowAtIndexPath 中,你需要在 alloc-init vc2 之后执行:vc2.passedSectorNumber = [NSNumber numberWithInt:indexPath.row];。为此,您需要在 VC2 中声明属性 passedSectorNumber。您无需在 VC1 中声明属性passedSectorNumber
  • 你有vc2.passedSectorNumber = self.passedSectorNumber吗?
  • >>albertamg,谢谢,效果很好。非常感谢。

标签: objective-c uitableview uiviewcontroller parameter-passing


【解决方案1】:

假设你的第二个 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];

尚未测试任何代码,但这将接近您的需要。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-30
    • 1970-01-01
    • 2011-02-23
    • 2012-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多