0

我正在尝试从表格视图切换到详细视图。当我单击单元格时,它会崩溃。我已经使用中断来尝试找到它在代码中崩溃的位置,但我找不到特定的行。当从表视图到详细视图执行 segue 时,它​​会崩溃。我想将一些变量传递给详细视图,但我首先需要让它切换到详细视图。我正在使用故事板仅供参考。任何帮助,将不胜感激。

更新:崩溃日志如下

arg c = (int) 1
arg v = (char **) 0x2fd63cdc

这是崩溃所在的行

return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));

更新 2

当我单步执行代码时,它崩溃之前的最后一段代码是为 segue 函数做准备。在其中的最后一行代码之后,它崩溃并转到上面的行。

更新 3

我意识到控制台被隐藏了,所以这是控制台中显示的错误。我的两个为 segue 做准备的 nslog 都被调用了。

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<DetailViewController 0x1e098bd0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key name.'
*** First throw call stack:
(0x3a1e63e7 0x39072963 0x3a1e60d5 0x3a418fe9 0x3a414d53 0x3a16c8a5 0x39665d7d 0x396655ff 0x3955e039 0x394e8421 0x3953d1b7 0x3953d0fd 0x3953cfe1 0x3953cf0d 0x3953c659 0x3953c541 0x3952ab33 0x3952a7d3 0x3958b2ad 0x3960dca1 0x3a4b9e67 0x3a1bb857 0x3a1bb503 0x3a1ba177 0x3a12d23d 0x3a12d0c9 0x3746633b 0x3951d291 0x69a9 0x3a91eb20)
libc++abi.dylib: terminate called throwing an exception

根视图控制器.m

#import "RootViewController.h"
#import "DetailViewController.h"



@implementation RootViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    //Initialize the array.
    listOfItems = [[NSMutableArray alloc] init];

    //Add items
    [listOfItems addObject:@"Jane"];
    [listOfItems addObject:@"Johnny"];
    [listOfItems addObject:@"Deanne"];
    [listOfItems addObject:@"John"];
    [listOfItems addObject:@"Susan"];

    //Set the title
    self.navigationItem.title = @"Countries";
}

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

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

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

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

    static NSString *CellIdentifier = @"MyCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Set up the cell...
    NSString *cellValue = [listOfItems objectAtIndex:indexPath.row];
    cell.textLabel.text = cellValue;


    return cell;
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    DetailViewController *DVC = [[DetailViewController alloc]init];
    DVC = [segue destinationViewController];
    NSIndexPath *path = [self.tableView indexPathForSelectedRow];
    NSString *name = [listOfItems objectAtIndex:path.row];
    DVC.name1.text = name;
}

RootViewController.h

#import <UIKit/UIKit.h>

@interface RootViewController : UITableViewController <UITableViewDataSource,UITableViewDelegate> {

    NSMutableArray *listOfItems;
    IBOutlet NSMutableArray *detailListOfItems;
}
@end

细节视图控制器.m

#import "DetailViewController.h"



@implementation DetailViewController
@synthesize selectedCountry;
@synthesize name1;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];


    //Set the title of the navigation bar
    //self.navigationItem.title = name1.text;

}

DetailViewController.h

#import <UIKit/UIKit.h>

@interface DetailViewController : UIViewController


@property (nonatomic,strong) IBOutlet UILabel *name1;
@property (nonatomic, retain) NSString *selectedCountry;
@end
4

2 回答 2

5

添加异常断点......

这是顶部的选项卡:

在此处输入图像描述

您需要选择倒数第二个断点图标之一。按下它并查看底部的添加图标:

在此处输入图像描述

然后按添加并创建一个异常断点并将其修改为这些设置:

在此处输入图像描述

这是你的断点异常!

在此处输入图像描述

然后运行您的应用程序,它应该会在您崩溃的线路上崩溃。您可能会看到一些崩溃日志,并且您可能必须将方案更改为 GDB


更新

是否有任何类型的崩溃日志或堆栈跟踪?另外,尝试将方案更改为 GDB ...... 以下是如何:

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

切换到 GDB 通常可以解决此问题。如果您已经在使用 GDB,请切换到 LLDB 看看会发生什么!

注意:正如您发布的那样,

arg c = (int) 1

arg v = (char **) 0x2fd63cdc

不是的崩溃日志吗...


更新

如果这仍然不起作用,您应该NSLog()在各处放置断点和 ' 以进行调试并找出错误的崩溃代码的位置!


更新

看,我们显然无法找出它在哪里崩溃,但我可能对事情在哪里崩溃以及为什么崩溃有一个微妙的线索。它与您的表格视图和listOfItems. 通常,表视图会在调用代码之前或同时重新加载其数据并调用其委托和数据源方法viewDidLoad。所以转移listOfItemsto对象的初始化和添加,viewWillAppear看看会发生什么。


更新

检查故事板中的所有插座DetailViewController并确保它们没有连接到不再存在的任何东西

于 2012-12-25T03:08:47.283 回答
0

这是解决方案:检查您的单元格标识符的名称并将此标识符应用于您的 segue。每个 Segue 都有一个标识符..默认为空..所以尝试分配一个与您的代码相似的名称,特别是您的详细信息的 segue视图控制器。

于 2015-07-14T19:50:28.240 回答