2015-04-05 37 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

相關問題