我对视图控制器之间的隔离仍然有点模糊。我正在创建一个应用程序,让您回答问题以确定您的个性类型。我正试图从我的主菜单转到另一个带有表的视图控制器来保存所有问题。当我点击我想要的按钮“Take The Test”而不是去想要的视图控制器时,我的应用程序崩溃了。我真的不想将任何信息传递给表视图,只是转到TestViewController。
单击“Take The Test”时,我将segue设置为push。它的标识符是“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
答案 0 :(得分:0)
要使推送序列有效,应该有NavigationController
。即如果您想从VC1
推送到VC2
,则VC1
应位于UINavigationController
内。
这样做:
1.将UINavigationController
拖放到故事板中。将附加一个viewController
。暂时删除VC
2.将连接从UINavigationController
拖到MenuViewController
。在显示的弹出式窗口中,选择root view controller
。
现在您的MenuViewController
已包含在NAvigationController
中,因此segue应该有效。
注意:在屏幕截图中,顶部似乎有一个连接到TestViewController
的NavigationController。那个NavController不是必需的。当你推到TestViewController
时,它会被添加到MenuViewController
&#39; NAvigationController
。