繁体   English   中英

触摸单元格并打开一个新的视图控制器

[英]Touch cell and open a new view controller

以下代码位于我的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];
    };

}

我想在触摸特定单元格时转到另一个视图。

为此,我这样做:

第1步:

从TableViewController拖到ContactViewController: 第一步


第2步:

选择segue并指定Segue Identifier( 右侧栏中的Show attributes Inspector选项卡

  • 我将Segue Identifier命名为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:已经有很多了。
显然......你需要正确地解决问题。

在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:方法。 通过实现此方法,我们可以将任何所需的数据传递给即将显示的视图控制器。 在这里,我们将选定的配方对象传递给详细视图控制器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM