ios - 触摸单元格并打开一个新的 View Controller

标签 ios objective-c uitableview

以下代码在我的 menuViewController.m 中。现在我想在触摸特定单元格时转到另一个 View 。我应该怎么做才能去contactViewController?(我正在使用 Storyboard)

Storyboard图片:

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];
    };

}

最佳答案

I want to go to another view when touch on a specific cell.



为此,我会这样做:

第1步:

从 TableViewController 拖到 ContactViewController:
StepOne

第2步:

选择 segue 并指定 Segue 标识符(右侧栏中的 Show attributes Inspector 选项卡)
  • 我已将 Segue 标识符命名为 SegueTestID
  • 我选择了Push按照我的风格,但您可能需要Modal

  • StepTwo

    相应的代码(在你的 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: 里有很多东西。已经。
    显然...您需要正确连接。

    关于ios - 触摸单元格并打开一个新的 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23590019/

    相关文章:

    android - 可以在 iOS/Android 的 fragment 着色器中使用 highp 吗?

    ios - 以编程方式创建的 UILabel 在 iOS 6 中不起作用

    iphone - 如何确定 cocoa 中的字符串编码?

    ios - iOS 7 中与 AutoLayout 相关的崩溃 : "_UIPageViewControllerContentView' s implementation of -layoutSubviews needs to call super. "

    ios - 在不减慢 UI 的情况下在 UITableViewCell 中显示 MKMapView

    ios - 如何从Appdelegate显示uiviewcontoller

    c# - 如何在 Monotouch 中绑定(bind) NSObject<UIGestureRecognizerDelegate>?

    ios - ARC 不允许将 'long' 转换为 'id' - iOS Dev

    json - 从 TableView 中的 JSON 数据下载可重复使用的单元格或错误的异步图像

    swift - 如何从解析中检索 geoPoints 并将其存储在 tableView 中?