2015-10-07 6 views
9

मेरी एप्लिकेशन स्टोर अलग-अलग विशेषताओं। यदि आप किसी सेल पर क्लिक करते हैं, तो वे सभी UITableView में दिखाए जाते हैं, एक विवरण दृश्य दिखाया जाता है। मास्टर-विस्तार-एक्सकोड-टेम्पलेट की तरह सामान्य सामान।3 डी टच पीक और UITableViewCell से पॉप कैसे अन्य UIViewController के लिए डेटा को सौंपने के लिए? कोर डाटा वस्तुओं में

अब मैं झांकना और पॉप के साथ 3 डी टच लागू करना चाहते हैं। मेल ऐप की तरह, 3 डी एक सेल को स्पर्श करें, पूर्वावलोकन प्राप्त करें, गहरा दबाएं, विस्तार से पॉप करें।

मैं इस अब तक काम कर रहा है, लेकिन मुझे टी यह पता लगाने अन्य DetailViewController को

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

और

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

में इसी डेटा भेजने का तरीका कर सकते हैं।

तुम सिर्फ सेल (कोई 3 डी टच) पर क्लिक करें अगर मैं

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

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

में डेटा को सौंपने मैं एक

id detailItem 

हर ViewController में उपयोग कर रहा हूँ और मैं इसे करने के लिए इसी ऑब्जेक्ट सेट ।

तो मैं, तो मेरे DetailViewController में मेरे "detailItem" चयनित (3 डी छुआ) सेल से संबंधित कोर डाटा वस्तु की जरूरत है काम करने के लिए कुछ इसी तरह की कोशिश।

मदद के लिए धन्यवाद!

उत्तर

6

स्टोरीबोर्ड अपने स्टोरीबोर्ड आईडी का उपयोग करने से गंतव्य देखें नियंत्रक जाओ और वस्तुओं है कि आप prepareForSegue विधि में क्या कर रहे हैं गुजरती हैं।

नीचे कोड है जो मैं डेटा पास करने के लिए उपयोग करता हूं। स्विफ्ट में, यह उद्देश्य सी में समान होना चाहिए। यदि आप उद्देश्य सी संस्करण चाहते हैं तो मुझे बताएं।

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

धीरज के उत्तर पर आधार, टेबलव्यू सेल के गलत स्पर्श स्थान को सुधारने के लिए कुछ कोड समायोजित करें। मान लें कि हमें गंतव्य नामक प्रकार के गंतव्य नियंत्रक को पास करने की आवश्यकता है।

- (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 ] कहीं भी देखने के लिएDidLoad बनाने के लिए [self.tableview convertPoint: स्थान से दृश्य: self.view] कॉल आवश्यक नहीं है। 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 
    } 
} 
संबंधित मुद्दे