Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 使用表格视图有困难_Objective C_Xcode_Cocoa Touch_Uitableview - Fatal编程技术网

Objective c 使用表格视图有困难

Objective c 使用表格视图有困难,objective-c,xcode,cocoa-touch,uitableview,Objective C,Xcode,Cocoa Touch,Uitableview,我创建了一个小表格视图(无滚动),希望用户选择一个选项。为了知道选择了哪个选项,我在视图控制器的头文件中定义了一个int类型,并将其作为属性公开。我定义了方法: - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { //some code here/ int optionSelected = indexPath.row; } 现在我想

我创建了一个小表格视图(无滚动),希望用户选择一个选项。为了知道选择了哪个选项,我在视图控制器的头文件中定义了一个
int
类型,并将其作为属性公开。我定义了方法:

- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
       //some code here/
       int optionSelected = indexPath.row;
}

现在我想在其他视图控制器中使用此
int选项selected
,但它不起作用。此外,它在我上面定义的函数中给出了一个错误,即“未使用的变量'optionSelected'”。请告诉我哪里可能出错。谢谢。

在您的代码中,
int options selected
定义了一个具有局部作用域的新整数,这意味着它仅在该方法内有效


如果已将名为
options selected
int
作为属性公开,请在代码中使用
self.options selected=indexPath.row
[self-setoptions selected:indexPath.row]

对其进行设置,
int options selected
定义一个具有局部作用域的新整数-这意味着它仅在该方法内有效


如果已将名为
optionSelected
int
作为属性公开,请使用
self.optionSelected=indexPath.row
[self-setOptionSelected:indexPath.row]
optionSelected
重新定义为上述代码中的局部变量。这意味着您没有将其分配给实例变量,因此以后使用它的任何尝试都不会起作用。从行首移除
int
。这将删除警告,如果其他一切正常,则允许您访问类中其他位置的选定行索引

您正在将
选项选为上述代码中的局部变量。这意味着您没有将其分配给实例变量,因此以后使用它的任何尝试都不会起作用。从行首移除
int
。这将删除警告,如果其他一切正常,则允许您访问类中其他位置的选定行索引

首先,请注意,选项selected将保存所选表格的行,而不是所选值。 其次,您已经创建了optionSelected作为当前类的实例变量,因此任何其他类都无法直接访问它。 要将该值传递给下一个视图,请在下一个视图的标题中添加选项Selected作为实例变量,就像在当前视图中一样。然后在配置下一个视图时将该值传递给下一个视图,然后将其推送:

// 1) create next view 2) configure next view 3) push next view
UIViewController *nextViewController= [[UIViewController alloc] initWithNibName:@"nextViewController" bundle:nil];

nextViewController.optionSelected = optionSelected;

[self.navigationController pushViewController:_tabBarController animated:YES];

首先,请注意,选项selected将保存所选表格的行,而不是所选值。 其次,您已经创建了optionSelected作为当前类的实例变量,因此任何其他类都无法直接访问它。 要将该值传递给下一个视图,请在下一个视图的标题中添加选项Selected作为实例变量,就像在当前视图中一样。然后在配置下一个视图时将该值传递给下一个视图,然后将其推送:

// 1) create next view 2) configure next view 3) push next view
UIViewController *nextViewController= [[UIViewController alloc] initWithNibName:@"nextViewController" bundle:nil];

nextViewController.optionSelected = optionSelected;

[self.navigationController pushViewController:_tabBarController animated:YES];