2015-10-07 40 views
9

我的應用程序在覈心數據對象中存儲了不同的屬性。它們都顯示在UITableView中,如果您單擊一個單元格,則會顯示一個DetailView。像Master-Detail-XCode-Template一樣。3D Touch從UITableViewCell觸摸Peek和Pop如何將數據交給其他UIViewController?

現在我想用peek和pop來實現3D Touch。就像在郵件應用程序中一樣,3D觸摸一個單元格,獲得預覽,按下更深,彈出細節。

我得到了這個到目前爲止的工作,但我可以T弄清楚如何在

- (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location 

- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit 

通過相應的數據到其他DetailViewController。

如果您只需單擊單元格(沒有3D觸摸)我交出的數據

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 

    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 
    NSManagedObject *object = [[self fetchedResultsController] objectAtIndexPath:indexPath]; 
    [[segue destinationViewController] setDetailItem:object]; 

我使用的是

id detailItem 
在每個視圖控制器

,我相應的對象設置爲它。

因此,我嘗試獲得類似於工作的東西,因此在我的DetailViewController中,我的「detailItem」需要選定(3D觸摸)單元格中相應的Core Data對象。

感謝您的幫助!

回答

6

使用故事板ID從故事板獲取目標視圖控制器,並傳遞您在prepareForSegue方法中執行的對象。

下面是我用來傳遞數據的代碼。它在Swift中,在Objective C中應該是類似的。讓我知道如果你想要Objective C的版本。

func previewingContext(previewingContext: UIViewControllerPreviewing,viewControllerForLocation location: CGPoint) -> UIViewController? method: 

    // Obtain the index path and the cell that was pressed. 
    guard let indexPath = tableView.indexPathForRowAtPoint(location), 
       cell = tableView.cellForRowAtIndexPath(indexPath) else { return nil } 

    // Create a destination view controller and set its properties. 
    guard let destinationViewController = storyboard?.instantiateViewControllerWithIdentifier("DestinationViewController") as? DestinationViewController else { return nil } 
    let object = fetchedResultController.objectAtIndexPath(indexPath) 
    destinationViewController.detailItem = object 

    /* 
     Set the height of the preview by setting the preferred content size of the destination view controller. Height: 0.0 to get default height 
    */ 
    destinationViewController.preferredContentSize = CGSize(width: 0.0, height: 0.0) 

    previewingContext.sourceRect = cell.frame 

    return destinationViewController 
} 
+0

謝謝,我真的很接近,但亂七八糟的東西了...;) – Kreuzberg

+2

@Dheeraj迪爾先生,你沒有提到你把這段代碼蘋果。 https://developer.apple.com/library/content/samplecode/ViewControllerPreviews/Listings/Projects_PreviewUsingDelegate_PreviewUsingDelegate_MasterViewController_UIViewControllerPreviewing_swift.html#//apple_ref/doc/uid/TP40016546-Projects_PreviewUsingDelegate_PreviewUsingDelegate_MasterViewController_UIViewControllerPreviewing_swift-DontLinkElementID_6 –

1

根據Dheeraj的回覆,調整一些代碼來糾正tableview單元格的錯誤觸摸位置。假設我們需要將名爲type的值傳遞給目標控制器。

- (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{ 
// check if we're not already displaying a preview controller 
if ([self.presentedViewController isKindOfClass:[DetailViewController class]]) { 
    return nil; 
} 

CGPoint cellPostion = [self.tableview convertPoint:location fromView:self.view]; 
NSIndexPath *path = [self.tableview indexPathForRowAtPoint:cellPostion]; 

if (path) { 
    UITableViewCell *cell = [self.tableview cellForRowAtIndexPath:path]; 
    type = [[self.data objectAtIndex:path.row] integerValue]; 
    // shallow press: return the preview controller here (peek) 
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; 
    DetailViewController *previewController = [storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"]; 
    previewController.type = type; 
    previewingContext.sourceRect = [self.view convertRect:cell.frame fromView:self.tableview]; 
    return previewController; 
} 
return nil; 

}

+1

只需調用[自我registerForPreviewingWithDelegate:自sourceView:self.tableView ]在某處像viewDidLoad使[self.tableview convertPoint:位置fromView:self.view]調用沒有必要。轉換將由UIKit爲您完成。 –

5

如果使用故事板塞格斯從細胞到新的視圖控制器,發送者是表視圖細胞。

所以,你可以實現prepareForSegue:這樣:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 

    if ([sender isKindOfClass:[UITableViewCell class]]) { 

     UITableViewCell *cell = sender; 
     NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; 

     id viewController = segue.destinationViewController; 

     // Get your data object 
     // Pass it to the new view controller 
    } 
} 
相關問題