0

So want I want to be able to do is click on a button, have a tableViewController pop up appear and then click on one of the entries to bring me to a new view.

I already have the tableView pop up set up with the proper entries that I get from an NSArray.

I just don't know how to make it so that when I click on an entry, it brings me to my desired view. I believe it has something to do with the following default function:

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

but I'm not sure. I have an NSArray *array of entries and a MyViewController class and so if you can help me make it do something like the following it'd be great:

if(array entry 1) go to MyViewController1
if(array entry 2) go to MyViewController2 
etc...
Lukas Kukacka
  • 7,604
  • 2
  • 25
  • 50
user1782677
  • 1,963
  • 5
  • 26
  • 48

4 Answers4

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

get called when you click on the tableview.the indexpath in the method gives you the cell you selected

so the indexpath.row gives you the entry on the array which should be viewed.amke the logic upon that

  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
 int selectedEntry=indexpath.row;
 switch (selectedEntry) {
        case 1:
            //goto viewcontroller 1
            break;
        case 2:
            //goto viewcontroller 2
            break;
        default:
            break;
    }
}

Push methods

Using storyboard

Community
  • 1
  • 1
Lithu T.V
  • 19,955
  • 12
  • 56
  • 101
  • I see, thanks! But I'm still confused as to how I'd actually go about changing the view to the next view controller. Like how would I go about implementing the lines in the switch statement that you commented out? – user1782677 May 24 '13 at 12:10
  • 1
    From the sounds of it, it seems like he created his app without first embedding his root view controller into a UINavigationController, otherwise, for each of the switch statement, it'll be a simple [self.navigationController pushViewController:viewController2 animated:YES]; – Zhang May 24 '13 at 12:22
  • How do I embed the root view controller? – user1782677 May 24 '13 at 12:26
0

this is the thing you have to do mine friend: when selecting a row this delegate method is called up.

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

     switch (indexpath.row) {
            case 1:
                {
                ViewController1 *VC1=[ViewController1 alloc]init];
                [self.navigationController pushViewController:VC1 animated:No];
                 break;
                }
           case 2:
               {
                ViewController2 *VC2=[ViewController2 alloc]init];
                [self.navigationController pushViewController:VC2 animated:No];
                break;
               }
          default:
                break;
        }
    }

hope this helps you mine friend..

  • So apparently this method would only work if I first embed my root view controller into a UINavigationController...but I don't quite know how to do that either. Is this something I also do in the UITableViewController? – user1782677 May 24 '13 at 12:33
  • You have to just use the reference of UINavigationController instead of Self.navigationController mine friend for Example: an reference UINavigationController *navi; then you have to use like this [navi pushViewController:VC2 animated:No]; – Purushottam Sen May 24 '13 at 12:45
  • So I declare and initialize a UINavigationController *navi in my .h file as a property and then initialize it as navi = [[UINavigationController alloc]init] in viewDidLoad() and then after that in didSelectRowAtIndexPath() I write ViewController1 *VC1=[ViewController1 alloc]init]; [self.navi pushViewController:VC1 animated:No]; But this doesn't work either. – user1782677 May 24 '13 at 12:55
  • add a breakpoint into this delegate and first check is it working? – Purushottam Sen May 24 '13 at 12:58
0
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
     int selectedEntry=indexpath.row;
     switch (selectedEntry) {
            case 1:
                ViewController1 *vc = [[ViewController1 alloc] init];    
                // Push View Controller 1 onto Navigation Stack
                [self.navigationController pushViewController:vc animated:YES];
                break;
            case 2:
                ViewController2 *vc2 = [[ViewController1 alloc] init];    
                // Push View Controller 2 onto Navigation Stack
                [self.navigationController pushViewController:vc2 animated:YES];
                break;
            default:
                break;
        }
    }
Khawar
  • 9,151
  • 9
  • 46
  • 67
  • You can also Present Modally like this: [self presentViewController:vc animated:YES completion:nil]; – Khawar May 24 '13 at 12:26
0
 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row==1)
{
//Go to view
}
else if(indexPath.row==2)
{
//Go to view
}


}
Bhavesh Nayi
  • 3,626
  • 1
  • 27
  • 42