Q:3D Touch Peek and Pop from UITableViewCell how to hand over data to other UIViewController? |
Q:3D触摸PEEK和流行从UITableViewCell如何数据交给其他窗口? |
my app stores different attributes in Core Data objects. They are all shown in a UITableView, if you click a cell, a DetailView is shown. the usual stuff like in the Master-Detail-XCode-Template.
Now I want to implement 3D Touch with peek and pop. Like in the Mail app, 3D touch a cell, get preview, press deeper, pop in the detail.
I got this working so far, but I can t figure out how to pass the corresponding data in
- (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
and
- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
to the other DetailViewController.
If you just click the cell (no 3D touch) I hand over the data in
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSManagedObject *object = [[self fetchedResultsController] objectAtIndexPath:indexPath];
[[segue destinationViewController] setDetailItem:object];
I am using a
id detailItem
in every ViewController and I set the corresponding object to it.
So I try to get something similar to work, so in my DetailViewController my "detailItem" needs the corresponding Core Data object from the selected (3D touched) cell.
Thanks for help ! |
我的应用程序在核心数据对象中存储不同的属性。他们都在一个表格所示,如果你点击一个细胞,一个DetailView显示。平常的东西像在主从Xcode模板。
现在我想实现3D触摸与PEEK和POP。像在邮件应用程序中,3D触摸一个单元格,得到预览,按下更深,弹出的细节。
到目前为止,我已经完成了这项工作,但我不知道如何传递相应的数据
- (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
和
- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
对其他detailviewcontroller。
If you just click the cell (no 3D touch) I h和 over the data in
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSManagedObject *object = [[self fetchedResultsController] objectAtIndexPath:indexPath];
[[segue destinationViewController] setDetailItem:object];
我用的是
id detailItem
in every ViewController 和 I set the corresponding object to it.
所以我尝试类似的工作,所以我detailviewcontroller我”detailitem”需要相应的核心数据对象的选择(3D感动)细胞。
谢谢帮助! |
answer1: |
回答1: |
Get the destination View controller from the storyboard using its storyboard ID and pass the objects that you are doing in prepareForSegue method.
Below is the code that I use to pass the data. Its in Swift, it should be similar in objective C. Let me know if you want Objective C version.
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
}
|
Get the destination View controller from the storyboard using its storyboard ID 和 pass the objects that you are doing in prepareForSegue method.
下面是我用来传递数据的代码。它的迅速,它应该是类似的目标C.让我知道,如果你想要客观的C版本。
func previewingContext(previewingContext: UIViewControllerPreviewing,viewControllerForLocation location: CGPoint) -> UIViewController? method:
// Obtain the index path 和 the cell that was pressed.
guard let indexPath = tableView.indexPathForRowAtPoint(location),
cell = tableView.cellForRowAtIndexPath(indexPath) else { return nil }
// Create a destination view controller 和 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
}
|
answer2: |
回答2: |
If you are using storyboard segues from the cell to the new view controller, the sender is the table view cell.
So you could implement prepareForSegue: as such:
- (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
}
}
|
如果您使用的是故事就从细胞到新的视图控制器,发件人是表观细胞。
所以你可以实现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
}
}
|
answer3: |
回答3: |
Base on Dheeraj's reply, adjust some code to rectify the wrong touch location of tableview cell. Assume we need pass the value named type to destination controller.
- (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;
} |
在答复Dheeraj的基地,调整一些代码来纠正错误的触摸位置tableview细胞。假设我们需要将类型命名的值传递给目标控制器。
- (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;
} |
ios
objective-c
uitableview
3dtouch
|
|