【问题标题】:Why NSInteger cannot set from parent view controller?为什么 NSInteger 不能从父视图控制器设置?
【发布时间】:2013-07-10 11:06:28
【问题描述】:

在我的 childview 控制器中,我有这个属性:

@property (nonatomic, assign) NSInteger currentItemIndex;

在父视图控制器中,我想设置该值。

    [childViewController setCurrentItemIndex:5];
    [self.navigationController pushViewController:childViewController animated:YES];

但在 childViewController 中,currentItemIndex 为 0。

为什么我不能设置?我哪里错了?

【问题讨论】:

  • 合成了吗?你是在别的什么地方设置的吗?
  • 在访问时发布您的代码

标签: ios nsinteger


【解决方案1】:

ReceiverViewController.h

@property (nonatomic, assign) NSInteger currrentIndex;

ReceiverViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSLog(@"Index value %d",self.currrentIndex);
}

DataPassingViewController.m

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.


     DataPassingViewController *detailViewController = [[DataPassingViewController alloc] initWithNibName:@"DataPassingViewController" bundle:nil];
    detailViewController.currrentIndex = 5;
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
}

我使用了上面的代码,输出如下:

2013-07-10 17:50:10.230 DataPassing[3881:c07] Index value 5

【讨论】:

    【解决方案2】:

    尝试在您的 childview 类中读取 _CurrentIndex,此变量应自动生成并使用 setter setItemIndex 方法进行设置。 我也有使用 Xcode 的经验,当我为 64 位目标进行构建时,会自动生成带下划线的内部变量,但对于 32 位目标,我必须在接口中声明它并在实现中使用 @synthesize 语句来获取编译代码。

    【讨论】:

      【解决方案3】:

      只需简单地声明,如

      @property NSInteger currentItemIndex;
      

      如果它不能解决问题,你必须告诉你在哪里声明属性,哪个类,在接口声明中与否,你正在使用哪个 xcode 等等。发布更多代码。

      例如对于 xcode 4.6,这没关系。但是对于旧的 xcode,您已经综合了该属性。

      【讨论】:

      • 为什么 NSInteger 不能是非原子的并赋值?
      • 当你声明一个你提到的属性时,你也在使用“assign”和“atomic”,因为它们是默认行为。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-03
      相关资源
      最近更新 更多