2015-10-07 9 views
9

私のアプリはコアデータオブジェクトに異なる属性を格納します。それらはすべてUITableViewに表示されます。セルをクリックするとDetailViewが表示されます。 Master-Detail-XCode-Templateのような通常のものです。3D Touch PeekとPopからUITableViewCellへデータを他のUIViewControllerに渡す方法は?

今、3Dタッチをpeekとpopで実装したいと考えています。メールアプリケーションのように、3Dタッチセル、プレビューを取得し、詳細を押し、深く押してください。

私がこれまでに取り組んでこれを得たが、私はトン、他のDetailViewControllerに

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

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

に対応するデータを渡す方法を見つけ出すことができます。

あなたはちょうど私が

id detailItem 

おきのViewControllerでを使用していますが、私はそれに対応するオブジェクトを設定し、私は

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

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

にデータを引き渡すセル(なし3Dタッチ)をクリックした場合。

私はDetailViewControllerの "detailItem"に、選択した(3Dタッチされた)セルから対応するCore Dataオブジェクトが必要なので、仕事に似たものを得ようとします。

ありがとうございました!

答えて

6

ストーリーボードIDを使用してストーリーボードから宛先Viewコントローラーを取得し、prepareForSegueメソッドで行っているオブジェクトを渡します。

以下は、データを渡すために使用するコードです。そのSwiftの中で、それは客観的な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ディレクトリサー林檎。自己sourceView:self.tableView 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の回答に基づいて、テーブルビューセルの間違ったタッチ位置を修正するコードを調整します。 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を呼び出します] [self.tableview convertPoint:location fromView:self.view]を呼び出す必要がないように、viewDidLoadのような場所。変換はUIKitによって行われます。 –

5

新しいビューコントローラにセルからストーリーボードseguesを使用している場合、送信者は、テーブルビューのセルです。

だからのような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 
    } 
} 
関連する問題