xcode/ios:以编程方式推送视图控制器并传递行信息
xcode/ios: push view controller programatically and pass row info
我有一个table。当您单击 table 行时,您会使用 prepare for segue 获取详细信息。在详细信息页面中,我有一个编辑按钮,可让您以模态方式打开先前在情节提要中创建的视图控制器。
问题是如何传递详细信息项的行或以其他方式向编辑控制器提供要显示的项的信息?
这是启动视图控制器的代码。
//create Edit navigation button:
UIBarButtonItem *editButton = [[UIBarButtonItem alloc]
initWithTitle:@"Edit"
style:UIBarButtonItemStylePlain
target:self
action:
//next line calls method editView
@selector(editView:)];
self.navigationItem.rightBarButtonItem = editButton;
//method that fires when you click button to launch edit view controller
- (void) editView:(id) sender
{
NSLog(@"pressed");
UIStoryboard *storyBoard = self.storyboard;
NSString * storyboardName = [storyBoard valueForKey:@"name"];
UIViewController *vc = [[UIStoryboard storyboardWithName:storyboardName bundle:nil] instantiateViewControllerWithIdentifier:@"editvc"];
IDEditVC *secondViewController =
[storyBoard instantiateViewControllerWithIdentifier:@"editvc"];
}
但是我怎样才能传递关于要编辑的项目的信息呢?
感谢您的任何建议。
假设您在 UITableViewController
中构建 TableView 一个包含对象的数组(例如:MyObject.m/h)。您应该使用 didSelectRowAtIndexPath
检测用户拥有 select 的哪个单元格,然后在准备 segue 时使用该整数从数组中检索 MyObject。
例如:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
cellPressed =(int)indexPath.row; //cellPressed is an integer variable(global variable in my VC
[self performSegueWithIdentifier:@"toMyVC" sender:self];
}
现在开始 prepareForSegue:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([[segue identifier] isEqualToString:@"toMyVc"]){
MyVC *mVC = [segue destinationViewController];
mVC.myObject = [myArrayWithMyObjects objectAtIndex:cellPressed];
}
}
在您的编辑视图中:
IDEditVC *secondViewController =
[storyBoard instantiateViewControllerWithIdentifier:@"editvc"];
secondViewController.myObj = myObjectRetrievedFromArray;
注意:您应该在 .h 文件中声明一个变量 MyObject,以便对其他 classes 可见。
在 class 中声明一个 "global" 变量:
@interface ViewController : UIViewController{
int cellPressed; //This is accessible by any method in YourViewController.m
}
我有一个table。当您单击 table 行时,您会使用 prepare for segue 获取详细信息。在详细信息页面中,我有一个编辑按钮,可让您以模态方式打开先前在情节提要中创建的视图控制器。
问题是如何传递详细信息项的行或以其他方式向编辑控制器提供要显示的项的信息?
这是启动视图控制器的代码。
//create Edit navigation button:
UIBarButtonItem *editButton = [[UIBarButtonItem alloc]
initWithTitle:@"Edit"
style:UIBarButtonItemStylePlain
target:self
action:
//next line calls method editView
@selector(editView:)];
self.navigationItem.rightBarButtonItem = editButton;
//method that fires when you click button to launch edit view controller
- (void) editView:(id) sender
{
NSLog(@"pressed");
UIStoryboard *storyBoard = self.storyboard;
NSString * storyboardName = [storyBoard valueForKey:@"name"];
UIViewController *vc = [[UIStoryboard storyboardWithName:storyboardName bundle:nil] instantiateViewControllerWithIdentifier:@"editvc"];
IDEditVC *secondViewController =
[storyBoard instantiateViewControllerWithIdentifier:@"editvc"];
}
但是我怎样才能传递关于要编辑的项目的信息呢?
感谢您的任何建议。
假设您在 UITableViewController
中构建 TableView 一个包含对象的数组(例如:MyObject.m/h)。您应该使用 didSelectRowAtIndexPath
检测用户拥有 select 的哪个单元格,然后在准备 segue 时使用该整数从数组中检索 MyObject。
例如:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
cellPressed =(int)indexPath.row; //cellPressed is an integer variable(global variable in my VC
[self performSegueWithIdentifier:@"toMyVC" sender:self];
}
现在开始 prepareForSegue:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([[segue identifier] isEqualToString:@"toMyVc"]){
MyVC *mVC = [segue destinationViewController];
mVC.myObject = [myArrayWithMyObjects objectAtIndex:cellPressed];
}
}
在您的编辑视图中:
IDEditVC *secondViewController =
[storyBoard instantiateViewControllerWithIdentifier:@"editvc"];
secondViewController.myObj = myObjectRetrievedFromArray;
注意:您应该在 .h 文件中声明一个变量 MyObject,以便对其他 classes 可见。
在 class 中声明一个 "global" 变量:
@interface ViewController : UIViewController{
int cellPressed; //This is accessible by any method in YourViewController.m
}