質問

The following code is in my menuViewController.m. Now I want to go to another view when touch on a specific cell. What should I do to go to contactViewController?(I am using storyboard)

storyboard image:

https://drive.google.com/file/d/0BwOYR2GMJ7l8U1lYYVFVTWVkOG8/edit?usp=sharing

code:

- (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.

For this, I'd do it this way:

Step 1:

Drag from TableViewController to ContactViewController: StepOne


Step 2:

Select segue and specify the Segue Identifier (Show attributes Inspector tab in the right side bar)

  • I have named the Segue Identifier as SegueTestID
  • I chose Push as my style but it seems you might need Modal

StepTwo


And the corresponding code (in your MenuViewController.m) should be something like:

-(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: It seems you have alot in your -prepareForSegue: already.
Obviously... you'll need to hook things up properly.

他のヒント

In Storyboard , Do this ( In your case its contactviewcontroller ) give the name identifier name to contactViewController whatever you want as shown in image for showRecipeDetail and you can go to the contactviewcontroller

enter image description here

and then

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

In above method it shows how to pass the data from current viewcontroller to destviewcontroller where We simply set the property (i.e. recipeName) in the RecipeDetailViewController to pass the recipe name. Obviously, you can add other properties in the detail view controller to pass other recipe-related values. ( In your case it will be data you want to pass to contactviewcontroller)

When a segue is triggered, before the visual transition occurs, the storyboard runtime invokes prepareForSegue:sender: method of the current view controller. By implementing this method, we can pass any needed data to the view controller that is about to be displayed. Here, we’ll pass the selected recipe object to the detail view controller.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top