【问题标题】:Trouble with Segue between ViewControllerViewController 之间的 Segue 问题
【发布时间】:2015-12-03 20:20:12
【问题描述】:

我对视图控制器之间的切换仍然有些模糊。我正在制作一个应用程序,可以让你回答问题以确定你的性格类型。我正试图从我的主菜单转到另一个带有表格的视图控制器来保存所有问题。当我单击所需的按钮“参加测试”而不是转到所需的视图控制器时,我的应用程序崩溃了。我真的不想将任何信息传递给表格视图,只需转到 TestViewController。

当点击“参加考试”时,我将 segue 设置为推送。它的标识符是“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

【问题讨论】:

  • 你的Menu View Controller是最初的viewController吗?
  • 错误是什么?你在故事板中正确命名了 segue 吗?
  • 你能告诉我们你的错误日志吗?
  • 它显示“威胁 1:信号 SIGABRT”。我可以上传一张图片,我不确定那是什么意思。是的,我将 segue 的标识符命名为“showTestDetail”,然后将其复制到 prepareForSegue 中的 if 语句条件中。这就是你正确命名的意思吗?
  • @Talcicio 检查我的答案

标签: ios objective-c iphone segue


【解决方案1】:

要使推送序列起作用,应该有一个NavigationController。也就是说,如果你想从VC1 推送到VC2VC1 应该在UINavigationController 内。

这样做:
1. 将UINavigationController 拖放到情节提要中。会有一个viewController 附加到它上面。暂时删除 VC。
2. 将连接从UINavigationController 拖到您的MenuViewController。在出现的弹出窗口中,选择root view controller

现在你的MenuViewController 包含在NAvigationController 中,所以segue 应该可以工作了。

注意: 在您的屏幕截图中,顶部似乎有一个 NavigationController 连接到 TestViewController。 NavController 不是必需的。因为当你推送到TestViewController 时,它会被添加到MenuViewControllerNAvigationController

【讨论】:

  • @Talcicio 可能你在没有导航栏时使用 push segue,尝试将 segue kind 更改为 present modally 并查看
  • 您能在进行这些更改后分享您的新故事板吗?
  • @ShahiM 我的桌面视图没有填充我的问题。这些问题以字符串形式存在于 NSArray 中。我通过将链接拖动到我的 VC 顶部的圆形图标,将 tableview 与委托和源连接起来。我将 tableview 链接到我声明的 tableview 属性。我实现了 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 和 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 方法。而且我的表格仍然没有显示问题。
  • 尝试在cellForRowAtIndexPath: 中放置一个断点,看看它是否被调用。
  • @ShahiM 它似乎没有被调用。应用没有在断点处暂停
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多