点击搜索结果后执行Segue [英] Perform Segue after clicking on search result
问题描述
我尝试通过单击相应的搜索结果执行一个名为Detail Student的子类的segue。不幸的是它不工作。
我不知道为什么它不工作。这里是我的代码到目前为止:
I try to to perform a segue to a subclass called "Detail Student" by clicking on the corresponding search result. Unfortunatelly it doesn't work. I cannot figure out why it doesn't work. Here is my code so far:
if (tableView == self.searchDisplayController.searchResultsTableView)
{student = [self.searchResults objectAtIndex:indexPath.row];
NSString *fullname = [NSString stringWithFormat:@"%@ %@", student.vorname, student.name];
cell.textLabel.text = fullname;
cell.detailTextLabel.text = student.hatBetrGrund.name;
DetailStudent *controller = [self.storyboard instantiateViewControllerWithIdentifier :@"DetailStudent"];
controller.delegate = self;
controller.managedObjectContext = self.managedObjectContext;
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
self.selectedStudent = [self.fetchedResultsController objectAtIndexPath:indexPath];
controller.student = self.selectedStudent;
NSLog(@"Wow");
}
推荐答案
您的代码中不清楚的几个事情:
There are several things that are not clear in your code:
首先,您正在设置文本和详细标签的文本。这属于 cellForRowAtIndexPath
或某些其他方法如 configureCell
。但是,从表视图中选择是通过 didSelectRowAtIndexPath
捕获的。请确保您使用一种方法配置单元格,并对另一个方法中的选择做出反应。
First, you are setting the text of text and detail labels. This belongs into cellForRowAtIndexPath
or some other method like configureCell
. However, selecting from a table view is caught via didSelectRowAtIndexPath
. Make sure you configure your cell in one method and react to selection in another.
其次,您实例化了视图控制器,但不显示它。您可以
Second, you are instantiating your view controller but not presenting it. You can either
- 实例化视图控制器,并通过
presentViewController:animated:
或 - 通过故事板中的segue链接详细视图控制器。在这种情况下,您不需要实例化它,而只需调用
performSegueWithIdentifier
。您可以在prepareForSegue
中配置详细视图。
- instantiate the view controller and present it via
presentViewController:animated:
or - link the detail view controller via a segue in storyboard. In this case, you do not need to instantiate it but simply call
performSegueWithIdentifier
. You configure your detail view inprepareForSegue
.
这篇关于点击搜索结果后执行Segue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!