以下代码位于menuViewController.m中。现在我想在触摸特定单元格时转到另一个视图。我应该怎么去联系ViewController?(我正在使用故事板)
故事板图片:
https://drive.google.com/file/d/0BwOYR2GMJ7l8U1lYYVFVTWVkOG8/edit?usp=sharing
码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row==2)
{
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Set the title of navigation bar by using the menu items
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
UINavigationController *destViewController = (UINavigationController*)segue.destinationViewController;
destViewController.title = [[menuItems objectAtIndex:indexPath.row] capitalizedString];
if ( [segue isKindOfClass: [SWRevealViewControllerSegue class]] ) {
SWRevealViewControllerSegue *swSegue = (SWRevealViewControllerSegue*) segue;
swSegue.performBlock = ^(SWRevealViewControllerSegue* rvc_segue, UIViewController* svc, UIViewController* dvc) {
UINavigationController* navController = (UINavigationController*)self.revealViewController.frontViewController;
[navController setViewControllers: @[dvc] animated: NO ];
[self.revealViewController setFrontViewPosition: FrontViewPositionLeft animated: YES];
};
}
答案 0 :(得分:2)
我想在触摸特定单元格时转到另一个视图。
为此,我会这样做:
从TableViewController拖到ContactViewController:
选择segue并指定Segue Identifier(右侧栏中的 Show attributes Inspector
标签)
SegueTestID
Push
作为我的风格,但似乎您可能需要Modal
相应的代码(在MenuViewController.m
中)应该是这样的:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 2) {
[self performSegueWithIdentifier:@"SegueTestID" sender:self];
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
//following lines needed only if you need to send some detail across to ContactViewController
if ([segue.identifier isEqualToString:@"SegueTestID"]) {
ContactViewController *destinationViewController = segue.destinationViewController;
destinationViewController.strTest = @"Check";
//where strTest is a variable in ContactViewController. i.e:
//"@property (nonatomic, strong) NSString *strTest;"
//declared in `ContactViewController.h`
}
//...
}
PS:你的-prepareForSegue:
已经有很多了
显然......你需要正确地解决问题。
答案 1 :(得分:1)
在Storyboard中,执行此操作(在您的情况下,其contactviewcontroller)为contactViewController提供名称标识符名称,如图所示showRecipeDetail 你可以去contactviewcontroller
然后
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showRecipeDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
RecipeDetailViewController *destViewController = segue.destinationViewController;
destViewController.recipe = [recipes objectAtIndex:indexPath.row];
}
}
在上面的方法中,它展示了如何将数据从当前viewcontroller传递到destviewcontroller 我们只是在RecipeDetailViewController中设置属性(即recipeName)来传递配方名称。显然,您可以在详细视图控制器中添加其他属性以传递其他与配方相关的值。 (在您的情况下,它将是您要传递给contactviewcontroller的数据)
当触发segue时,在视觉转换发生之前,storyboard运行时调用当前视图控制器的prepareForSegue:sender:方法。通过实现此方法,我们可以将任何所需的数据传递给即将显示的视图控制器。在这里,我们将所选的配方对象传递给详细视图控制器。