1

I am still a little fuzzy on segueing between view controllers. I'm making an app that lets you answer questions to determine your personality type. I'm trying to go from my main menu to another view controller with a table to hold all the questions. My app crashes though when I click my desired button "Take The Test" instead of going to the desired view controller. I don't really want to pass any info to the table view, just segue to the TestViewController.

I set the segue as push when the "Take The Test" is clicked. And it's identifier is "showTestDetail"

MenuViewController.h

#import <UIKit/UIKit.h>

@interface MenuViewController : UIViewController

// Declaring Menu variables
@property (weak, nonatomic) IBOutlet UIImageView *titlePhoto;
@property (weak, nonatomic) IBOutlet UIButton *takeTest;
@property (weak, nonatomic) IBOutlet UIButton *personailtyTypes;
@property (weak, nonatomic) IBOutlet UIButton *readTheory;

@end

MenuViewController.m

import "MenuViewController.h"
#import "TestViewController.h"

@interface MenuViewController ()

@end

@implementation MenuViewController

@synthesize titlePhoto;
@synthesize takeTest;
@synthesize personailtyTypes;
@synthesize readTheory;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    // Initialize menu title image
    self.titlePhoto.image = [UIImage imageNamed:@"mainTitle_16pt.png"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([segue.identifier isEqualToString:@"showTestDetail"]) {
        TestViewController *testDetailController = (TestViewController *)segue.destinationViewController;
    }
}
@end

TestViewController.h

#import <UIKit/UIKit.h>
#import "Questions.h"

@interface TestViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) IBOutlet UITextView *instructions;
@property (weak, nonatomic) IBOutlet UIButton *results;
@property (nonatomic, strong) Questions *survey;

@end

TestViewController.m

#import "TestViewController.h"

@interface TestViewController ()

@end

@implementation TestViewController {
    NSDictionary *allQuestions;
}

@synthesize survey;
@synthesize results;
@synthesize instructions;

- (void)viewDidLoad {
    [super viewDidLoad];

    // set up question array
    survey.questions = @[@"1. You find it difficult to introduce yourself to other people",
                        @"2. You often get so lost in thoughts that you ignore or forget your surroundings",
                        @"3. You try to respond to your e-mails as soon as possible and cannot stand a messy inbox",
                        @"4. You find it easy to stay relaxed and focused even when there is some pressure",
                        @"5. You don’t usually initiate conversations",
                        @"6. You rarely do something just out of sheer curiosity",
                        @"7. You feel superior to other people",
                        @"8. Being organized is more important to you than being adaptable",
                        @"9. You are usually highly motivated and energetic",
                        @"10. Winning a debate matters less to you than making sure no one gets upset",
                        @"11. You often feel as if you have to justify yourself to other people",
                        @"12. Your home and work environments are quite tidy",
                        @"13. You do not mind being at the center of attention",
                        @"14. You consider yourself more practical than creative",
                        @"15. People can rarely upset you",
                        @"16. Your travel plans are usually well thought out",
                        @"17. It is often difficult for you to relate to other people’s feelings",
                        @"18. Your mood can change very quickly",
                        @"19. In a discussion, truth should be more important than people’s sensitivities",
                        @"20. You rarely worry about how your actions affect other people",
                        @"21. Your work style is closer to random energy spikes than to a methodical and organized approach",
                        @"22. You are often envious of others",
                        @"23. An interesting book or a video game is often better than a social event",
                        @"24. Being able to develop a plan and stick to it is the most important part of every project",
                        @"25. You rarely get carried away by fantasies and ideas",
                        @"26. You often find yourself lost in thought when you are walking in nature",
                        @"27. If someone does not respond to your e-mail quickly, you start worrying if you said something wrong",
                        @"28. As a parent, you would rather see your child grow up kind than smart",
                        @"29. You do not let other people influence your actions",
                        @"30. Your dreams tend to focus on the real world and its events",
                        @"31. It does not take you much time to start getting involved in social activities at your new workplace",
                        @"32. You are more of a natural improviser than a careful planner",
                        @"33. Your emotions control you more than you control them",
                        @"34. You enjoy going to social events that involve dress-up or role-play activities",
                        @"35. You often spend time exploring unrealistic and impractical yet intriguing ideas",
                        @"36. You would rather improvise than spend time coming up with a detailed plan",
                        @"37. You are a relatively reserved and quiet person",
                        @"38. If you had a business, you would find it very difficult to fire loyal but underperforming employees",
                        @"39. You often contemplate the reasons for human existence",
                        @"40. Logic is usually more important than heart when it comes to making important decisions",
                        @"41. Keeping your options open is more important than having a to-do list",
                        @"42. If your friend is sad about something, you are more likely to offer emotional support than suggest ways to deal with the problem",
                        @"43. You rarely feel insecure",
                        @"44. You have no difficulties coming up with a personal timetable and sticking to it",
                        @"45. Being right is more important than being cooperative when it comes to teamwork",
                        @"46. You think that everyone’s views should be respected regardless of whether they are supported by facts or not",
                        @"47. You feel more energetic after spending time with a group of people",
                        @"48. You frequently misplace your things",
                        @"49. You see yourself as very emotionally stable",
                        @"50. Your mind is always buzzing with unexplored ideas and plans",
                        @"51. You would not call yourself a dreamer.",
                        @"52. You usually find it difficult to relax when talking in front of many people",
                        @"53. Generally speaking, you rely more on your experience than your imagination",
                        @"54. You worry too much about what other people think",
                        @"55. If the room is full, you stay closer to the walls, avoiding the center",
                        @"56. You have a tendency to procrastinate until there is not enough time to do everything",
                        @"57. You feel very anxious in stressful situations",
                        @"58. You believe that it is more rewarding to be liked by others than to be powerful",
                        @"59. You have always been interested in unconventional and ambiguous things, e.g. in books, art, or movies",
                        @"60. You often take initiative in social situations"];

    // set up answers array
    survey.answers = @[[NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1],
                       [NSNumber numberWithInt:-1]];
    // set up dictionary of question/answer relationship
    allQuestions = [NSDictionary dictionaryWithObjects:survey.questions forKeys:survey.answers];

    // set up types array
    survey.types = @[@"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral",
                     @"neutral"];

    NSString *instr = @"Things to know:\n 1. Takes less than 12 minutes.\n 2. Answer honestly, even if you don't like the answer. \n 3. Try not to leave any 'neutral' answers ";
    UIFont *instrFont = [UIFont fontWithName:@"HelveticaNeue" size:12];
    CGSize textviewSize = [instr sizeWithFont:instrFont constrainedToSize:CGSizeMake(300, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping];
    instructions = [[UITextView alloc] initWithFrame:CGRectMake(10, 10, textviewSize.width, textviewSize.height)];
    instructions.text = instr;
    [self.view addSubview:instructions];


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [survey.questions count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *simpleTableIdentifier = @"QuestionCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    NSString *que = (NSString *)[survey.questions objectAtIndex:indexPath.row];
    cell.textLabel.text = que;

    return cell;
}

@end
Talcicio
  • 65
  • 7

1 Answers1

0

For a push seque to work, there should be a NavigationController. i.e if you want to push from VC1 to VC2, VC1 should be inside a UINavigationController.

So do this:
1. Drag and drop a UINavigationController into the storyboard. there will be a viewController attached to it. delete the VC for now.
2. Drag a connection from the UINavigationController to your MenuViewController. In the popup that appears, select root view controller.

Now your MenuViewController is enclosed in a NAvigationController, so the segue should work.

Note : in your screenshot, there seems to be a NavigationController on the top that connects to TestViewController. That NavController is not required. Coz when you push to TestViewController, it is added to the MenuViewController's NAvigationController.

ShahiM
  • 3,179
  • 1
  • 33
  • 58
  • @Talcicio probably u using push segue when there's no nav bar, try change the segue `kind` to `present modally` and see – Tj3n Dec 03 '15 at 11:37
  • can you share your new storyboard after making these changes? – ShahiM Dec 03 '15 at 11:38
  • @ShahiM My tableview isn't getting populated with my questions. The questions are in an NSArray as strings. I connected the tableview with the delegate and source by dragging the link to the circle icon at the top of my VC. I linked the tableview to the tableview property I declared. I implemented the - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section and - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath methods. And my table still doesn't show the questions. – Talcicio Dec 03 '15 at 12:08
  • try putting a breakpoint inside `cellForRowAtIndexPath:` to see if it is getting called. – ShahiM Dec 03 '15 at 12:13
  • @ShahiM it doesn't seem to be getting called. The app didn't pause at the breakpoint – Talcicio Dec 03 '15 at 12:16
  • then double check if the `tableView`, `delegate` and `datasource` are all properly connected. – ShahiM Dec 03 '15 at 12:18
  • also check what `numberOfRowsInSection:` is returning. If that is 0, `cellForRowAtIndexPath` will not get called. – ShahiM Dec 03 '15 at 12:20
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/96886/discussion-between-talcicio-and-shahim). – Talcicio Dec 03 '15 at 12:22
  • oh. and you didn't initialize `survey`. add `survey = [[Questions alloc] init];` at the start of `viewDidLoad` – ShahiM Dec 03 '15 at 12:22