-1

I used the storyboard for the project.The problem is it cannot push to the Function1DetailViewController.Moreover, since the tableview do not have the cell inside,I don't know how to draw the segue to link between the two view controllers. Also, I don't know how to write the prepareForSegue method.

Should I also need to make some changes in didSelectRowAtIndexPath method?

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *tempSqlStatement = @"";
    NSString *tempString = @"%";
    databaseName = @"TCMdb8.sql";
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains
    (NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];
    databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
    if (buttonNum == 0) {
        Function1DetailViewController *function1DetailViewController = [[Function1DetailViewController alloc] initWithKey:[listOfItems objectAtIndex:indexPath.row] type:@"1"];
        [self.navigationController pushViewController:function1DetailViewController animated:YES];
    } else if (buttonNum != 0){
        if (buttonNum == 1) {
            tempSqlStatement = [NSString stringWithFormat:@"select name from Medicine where stroke ='%@' ORDER BY length(name) ASC", [listOfItems objectAtIndex:indexPath.row]];
        }
        function1SQLStatement = [tempSqlStatement UTF8String];
        [self checkAndCreateDatabase];
        [self readFromDatabase];
        [tableList reloadData];
        buttonNum = 0;
    }
}
JoeFryer
  • 2,751
  • 1
  • 18
  • 23

3 Answers3

1

You can draw a segue between two view controllers in storyboard. Select the whole view controller (best to click where the status bar would be), ctrl-drag to the other. Give that new segue an identifier.

When you want to trigger it in the view controller:

[self performSegueWithIdentifier:@"TheIdentifierYouPutInIB"];

You can replace the navigation logic in your didSelectRowAtIndexPath method with the above. See here for prepare for segue example.

Community
  • 1
  • 1
danh
  • 62,181
  • 10
  • 95
  • 136
  • should i need to write prepareForSegue method? – user2914810 Mar 08 '14 at 14:53
  • A common reason to implement prepareForSegue is that the destination view controller needs some property set, often the part of the model that was selected to trigger the segue. The answer you marked correct left out a line of code in prepare for segue: NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; The idea is, on our way to the destination view controller, find out what the user selected to make us go there, look that item up in our model (the same array that supports the table), and tell the destination vc which model item to deal with. – danh Mar 08 '14 at 15:45
  • But after I add 'NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; '. It still have error:'Property 'tableview' not found on object of type 'Function1ViewController' 'and 'No known instance method of selector ''type'. – user2914810 Mar 08 '14 at 16:11
  • Can I get more help from you? since the code is very complicate,I cannot put all here. – user2914810 Mar 08 '14 at 16:13
  • @user2914810 you will need to create an IBoutlet connection to your tableview since you're not using a `UITableViewController` see this answer http://stackoverflow.com/a/22119156/1880431 – meda Mar 08 '14 at 16:39
  • what about the error about 'No known instance method of selector ''type', due to the code (Function1DetailViewController *)segue.destinationViewController; functionVC.item = [listOfItems objectAtIndex:indexPath.row] type:@"1"]; – user2914810 Mar 09 '14 at 15:12
  • The source can download from here. I still get error when I follow your method. Please help me to look at which problem it is . https://drive.google.com/file/d/0B4XiCB1Zt5QuVFN6bEdLWV9LLWM/edit?usp=sharing – user2914810 Mar 09 '14 at 15:29
  • @user2914810, I reviewed the code. The problem is invoking the method type: on the element in the array. Change that line to function1DetailViewController.selectedItem = listOfItems[indexPath.row]; and it compiles. Also, there are 108 warnings in the project. When you're just starting out, it's better to learn on a smaller, simpler project. – danh Mar 09 '14 at 17:01
  • Although the code do not have error , when I run the apps it cannot work. See this picture, https://docs.google.com/file/d/0B4XiCB1Zt5QuamZpN0E5SHRCVms/edit – user2914810 Mar 11 '14 at 10:11
1

Here are different ways to create a segues:

1 - From Cell To detail Controller:

From Cell To detail Controller


2 - Using Connection Inspector

Using Connection Inspector


3 - From View Controller to View Controller

Kill it with MOAR fire!


  • #1 and #3 you will need to hold control before dragging.
  • #2 & #3 : You will need to give an identifier to your segue, unlike #1 You have to performe the segue using code:

add identifier:

enter image description here

Perform the segue:

[self performSegueWithIdentifier:@"Function1Segue" sender:sender];

Now here is the thing, this will just perform the segue, if you ever needed to pass some data to that view controller. Then you have to implement the following segue deleguate:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Make sure your segue name in storyboard is the same as this line
    if ([[segue identifier] isEqualToString:@"Function1Segue"])
    {
       NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 
        // Get reference to Function1DetailViewController
       Function1DetailViewController *functionVC =
                (Function1DetailViewController *)segue.destinationViewController;
       functionVC.item = [listOfItems objectAtIndex:indexPath.row] type:@"1"];
    }
}

functionVC.item because you would have a property called item that you want to set in Function1DetailViewController.

Community
  • 1
  • 1
meda
  • 45,103
  • 14
  • 92
  • 122
  • Some error in the prepareForSegue method. It is 'use of undeclared identifier 'indexPath'.Do you mean 'NSIndexPath'?' – user2914810 Mar 08 '14 at 15:20
  • read my answer carefully! You need the identifier !!! http://i.stack.imgur.com/OJ7aJ.png :) – meda Mar 08 '14 at 15:28
  • I mean the code 'functionVC.item = [listOfItems objectAtIndex:indexPath.row] type:@"1"];' got this error – user2914810 Mar 08 '14 at 15:46
  • @meda, it's a good answer, but missing the declaration and initialization of indexPath. The OP's compiler is reading your answer even more carefully :-)! – danh Mar 08 '14 at 15:46
  • ohhh you right guys, I added the missing line! I had skipped this code my appologies! – meda Mar 08 '14 at 15:53
  • Can I get more help from you? since the code is very complicate,I cannot put all here. – user2914810 Mar 08 '14 at 16:13
0

select the view controller icon (yellow below source view controller) and drag it to destination view controller. You'll be able to call it by program.

Armand DOHM
  • 1,121
  • 1
  • 7
  • 9