Objective c is to click a table row and navigate to another page

I want the user to click on a table row , after which the user will navigate to another page. The page has a storyboard ID "Other View" and a recovery ID "Other View". But I can't go to the target view

This is my code:

 - (UITableViewCell *)tableView:(UITableView *)tabel cellForRowAtIndexPath:(NSIndexPath *)indeksBaris
{

    static NSString *simpleTableIdentifier = @"TampilanCell";
    TabelBeritaCell *cell = (TabelBeritaCell *)[tabel dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    NSLog(@"nilai : %d", indeksBaris.row );
    //detecting NIB of table view
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TableView" owner:self options:nil];
    cell = [nib objectAtIndex:0];

    //Go To View Controller which have identifier "Other View"
    UIViewController *otherViewCon;
    otherViewCon = [self.storyboard instantiateViewControllerWithIdentifier:@"Other View"];
    [self.navigationController pushViewController:otherViewCon animated:YES];

    cell.judulBerita.text =  [listBerita objectAtIndex:indeksBaris.row];

    return cell;
}

      

This code doesn't work:

UIViewController *otherViewCon;
otherViewCon = [self.storyboard instantiateViewControllerWithIdentifier:@"Other View"];
[self.navigationController pushViewController:otherViewCon animated:YES];

      

UPDATED I have new code. i use didSelectRowAtIndexPath method but it doesn't work:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        UIViewController *otherViewCon;
        otherViewCon = [self.storyboard instantiateViewControllerWithIdentifier:@"Other View"];
        [self.navigationController pushViewController:otherViewCon animated:YES];
    }

      

+3


source to share


7 replies


Try the method didSelectRowAtIndexPath

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self performSegueWithIdentifier:@"ShowDetail" sender:tableView];
}

      

If you want to pass a variable or perform an action before passing it, follow these steps:



- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"ShowDetail"]) {
        //Do something
        Detail *detailController = (Detail*)segue.destinationViewController;
    }
}

      

If you want to know how to name a segue, here is a great tutorial from the apple docs: Naming Segues

+4


source


Your code seems to be correct. I just feel like the problem is finding the UIStoryboard

.replace object the following things might work fine.



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        UIViewController *otherViewCon;
       UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"your_storyboard_name" bundle:nil];// like Main.storyboard for used "Main"
        otherViewCon = [storyboard instantiateViewControllerWithIdentifier:@"Other View"];
        [self.navigationController pushViewController:otherViewCon animated:YES];
}

      

+1


source


Inject your code into the method didSelectRowAtIndexPath

. And please read the documentation before writing code n research before asking questions here.

0


source


You should write your navigation code in

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

      

this method.

Or you can directly navigate to another view controller by drawing a push segue in the storyboard. No need to write code.

How to push sej on UITableViewCell selection

0


source


 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{
      NSString * storyboardIdentifier = @"storyboardName";// for example "Main.storyboard" then you have to take only "Main"
      UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardIdentifier bundle: [NSBundle mainBundle]];
      UIViewController * UIVC = [storyboard instantiateViewControllerWithIdentifier:@"secondVCStoryboardID"];
     [self presentViewController:UIVC animated:YES completion:nil];
}

      

0


source


Try it, it will help you. didSelectRowAtIndexPath

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{  
  if(indexpath.row==0)//means you click row 0 
   {
     UIViewController *otherView=[self.storyboard instantiateViewControllerWithIdentifier:@"otherview"];
        [self presentViewController:otherView animated:YES completion:nil];
   }
  else
   {
     UIViewController *detailView=[self.storyboard instantiateViewControllerWithIdentifier:@"detailView"];
        [self presentViewController:detailView animated:YES completion:nil];
   }
}

      

0


source


here showDataVC is my new filename for the second ViewController. in the first, a new file object is created and initialized. and initialize showDataVC with object to use object in pushviewcontroller.

(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    showDataVc *objshowDataVc = [self.storyboard instantiateViewControllerWithIdentifier:@"showDataVc"];

    [self.navigationController pushViewController:objshowDataVc animated:YES];
}

      

0


source







All Articles