2015-04-05 25 views
0

我有一张桌子。当您点击表格的行时,您可以使用准备for segue来获得详细信息。从详细信息页面,我有一个编辑按钮,可让您以模态方式打开以前在故事板中创建的视图控制器。xcode/ios:以编程方式推视图控制器并传递行信息

问题是我该如何传递细节项的行或者给编辑控制器显示什么项的信息?

这里是启动视图控制器的代码。

//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"]; 
} 

但我该如何传递项目上的信息来编辑?

感谢您的任何建议。

+0

你说的“以前在故事板模态创建”呢?您正在使用editView方法创建控制器。你在那里有一个指针(secondViewController),所以你可以在这个方法中传递你需要的任何信息。这完全不清楚为什么你要在editView中实例化2个控制器,vc和secondViewController,并且你对它们中的任何一个都没有做任何事情。 – rdelmar 2015-04-05 04:48:47

+0

您可以轻松地将数据传递给视图控制器。它取决于你如何将数据作为数组或字符串。您可以在编辑视图控制器中定义变量并从详细信息页面传递数据。 – 2015-04-05 07:12:37

回答

0

让我们假设您在UITableViewController中构建TableView的一个包含对象的数组(例如:MyObject.m/h)。您应该使用didSelectRowAtIndexPath来检测用户选择了哪个单元格,然后使用该整数从数组中检索MyObject以准备segue。 如:

- (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来以可见来自其他类。

在一个类中声明一个“全局”变量:

@interface ViewController : UIViewController{ 

    int cellPressed; //This is accessible by any method in YourViewController.m 
} 
+0

这是使用核心数据相同。在prepareforsegue我目前只有Items * item = [self.fetchedResultsController objectAtIndexPath:indexPath];然后destViewController.item = item – user1904273 2015-04-05 17:44:24

+0

意识到我不知道如何创建一个全局变量。你用extern吗? – user1904273 2015-04-05 18:33:23

+0

我更新了答案 – BlackM 2015-04-05 22:20:20

相关问题