Ios 推送到详细视图时应用程序崩溃
我正在开发一个表格视图应用程序,其中的单元格包含标题和说明。点击单元格时,将移动到显示相同标题和说明以及长文本(从txt文件加载)的详细视图 这在几天前起了作用,但从那以后我做了一些改变,似乎把事情搞砸了。现在,当我点击手机时,应用程序崩溃 这是我的代码: AlfaTableViewController.mIos 推送到详细视图时应用程序崩溃,ios,objective-c,xcode,uitableview,Ios,Objective C,Xcode,Uitableview,我正在开发一个表格视图应用程序,其中的单元格包含标题和说明。点击单元格时,将移动到显示相同标题和说明以及长文本(从txt文件加载)的详细视图 这在几天前起了作用,但从那以后我做了一些改变,似乎把事情搞砸了。现在,当我点击手机时,应用程序崩溃 这是我的代码: AlfaTableViewController.m #import "AlfaTableViewController.h" #import "TableCell.h" #import "DetailViewController.h" @in
#import "AlfaTableViewController.h"
#import "TableCell.h"
#import "DetailViewController.h"
@interface AlfaTableViewController () {
NSDictionary *Title;
NSDictionary *Description;
NSDictionary *Text;
NSArray *songSectionTitles;
}
@end
@implementation AlfaTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
Title = @{@"A" : @[@"Adventstid", @"Albertina", @"Alfabetsvisan"],
@"B" : @[@"Badvisa för småbjörnar", @"Bagaren"],
@"C" : @[@"Citron"]};
Description = @{@"A" : @[@"Text: Nisse Lamm", @"Text: Mora Träsk", @"Text: Alice Tegner"],
@"B" : @[@"Text: Lennart Hellsing", @"Text: Alice Tegner"],
@"C" : @[@"Text: Dockmormor"]};
Text = @{@"A" : @[@"adventstid", @"albertina", @"alfabetsvisan"],
@"B" : @[@"badvisa", @"bagaren"],
@"C" : @[@"citron"]};
songSectionTitles = [[Title allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [songSectionTitles count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [songSectionTitles objectAtIndex:section];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSString *sectionTitle = [songSectionTitles objectAtIndex:section];
NSArray *sectionName = [Title objectForKey:sectionTitle];
return [sectionName count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"TableCell";
TableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
NSString *sectionTitle = [songSectionTitles objectAtIndex:indexPath.section];
NSArray *sectionName = [Title objectForKey:sectionTitle];
NSString *name = [sectionName objectAtIndex:indexPath.row];
NSArray *sectionDescription = [Description objectForKey:sectionTitle];
NSString *description = [sectionDescription objectAtIndex:indexPath.row];
cell.TitleLabel.text = name;
cell.DescriptionLabel.text = description;
return cell;
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"ShowDetails"]) {
DetailViewController *detailviewcontroller = segue.destinationViewController;
detailviewcontroller.DetailModal = @[Title, Description, Text];
}
}
@end
#import "DetailViewController.h"
@interface DetailViewController ()
@end
@implementation DetailViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
_TitleLabel.text = _DetailModal[0];
_DescriptionLabel.text = _DetailModal[1];
_TextView.text =_DetailModal[2];
self.navigationItem.title = _DetailModal[0];
NSString *path = [[NSBundle mainBundle]pathForResource:_DetailModal[2] ofType:@"txt"];
NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
self.TextView.text = content;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
DetailViewController.m
#import "AlfaTableViewController.h"
#import "TableCell.h"
#import "DetailViewController.h"
@interface AlfaTableViewController () {
NSDictionary *Title;
NSDictionary *Description;
NSDictionary *Text;
NSArray *songSectionTitles;
}
@end
@implementation AlfaTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
Title = @{@"A" : @[@"Adventstid", @"Albertina", @"Alfabetsvisan"],
@"B" : @[@"Badvisa för småbjörnar", @"Bagaren"],
@"C" : @[@"Citron"]};
Description = @{@"A" : @[@"Text: Nisse Lamm", @"Text: Mora Träsk", @"Text: Alice Tegner"],
@"B" : @[@"Text: Lennart Hellsing", @"Text: Alice Tegner"],
@"C" : @[@"Text: Dockmormor"]};
Text = @{@"A" : @[@"adventstid", @"albertina", @"alfabetsvisan"],
@"B" : @[@"badvisa", @"bagaren"],
@"C" : @[@"citron"]};
songSectionTitles = [[Title allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [songSectionTitles count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [songSectionTitles objectAtIndex:section];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSString *sectionTitle = [songSectionTitles objectAtIndex:section];
NSArray *sectionName = [Title objectForKey:sectionTitle];
return [sectionName count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"TableCell";
TableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
NSString *sectionTitle = [songSectionTitles objectAtIndex:indexPath.section];
NSArray *sectionName = [Title objectForKey:sectionTitle];
NSString *name = [sectionName objectAtIndex:indexPath.row];
NSArray *sectionDescription = [Description objectForKey:sectionTitle];
NSString *description = [sectionDescription objectAtIndex:indexPath.row];
cell.TitleLabel.text = name;
cell.DescriptionLabel.text = description;
return cell;
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"ShowDetails"]) {
DetailViewController *detailviewcontroller = segue.destinationViewController;
detailviewcontroller.DetailModal = @[Title, Description, Text];
}
}
@end
#import "DetailViewController.h"
@interface DetailViewController ()
@end
@implementation DetailViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
_TitleLabel.text = _DetailModal[0];
_DescriptionLabel.text = _DetailModal[1];
_TextView.text =_DetailModal[2];
self.navigationItem.title = _DetailModal[0];
NSString *path = [[NSBundle mainBundle]pathForResource:_DetailModal[2] ofType:@"txt"];
NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
self.TextView.text = content;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
还有坠机日志
2016-11-03 17:17:22.637 Barntexter[11893:936314] -[__NSDictionaryI length]: unrecognized selector sent to instance 0x7fc813f07e10
2016-11-03 17:17:22.641 Barntexter[11893:936314] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryI length]: unrecognized selector sent to instance 0x7fc813f07e10'
*** First throw call stack:
(
0 CoreFoundation 0x000000010a0fbd85 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000109b6fdeb objc_exception_throw + 48
2 CoreFoundation 0x000000010a104d3d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010a04acfa ___forwarding___ + 970
4 CoreFoundation 0x000000010a04a8a8 _CF_forwarding_prep_0 + 120
5 CoreFoundation 0x000000010a0e58cd isEqualToString + 61
6 CoreFoundation 0x000000010a0e5a5c -[NSTaggedPointerString isEqualToString:] + 28
7 UIKit 0x000000010a724df5 -[UILabel _setText:] + 82
8 Barntexter 0x00000001096651e5 -[DetailViewController viewDidLoad] + 149
9 UIKit 0x000000010a64c984 -[UIViewController loadViewIfRequired] + 1198
10 UIKit 0x000000010a65293b -[UIViewController __viewWillAppear:] + 120
11 UIKit 0x000000010a682750 -[UINavigationController _startCustomTransition:] + 1203
12 UIKit 0x000000010a692b9b -[UINavigationController _startDeferredTransitionIfNeeded:] + 712
13 UIKit 0x000000010a693d0b -[UINavigationController __viewWillLayoutSubviews] + 57
14 UIKit 0x000000010a842503 -[UILayoutContainerView layoutSubviews] + 248
15 UIKit 0x000000010a56c980 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 703
16 QuartzCore 0x000000010e593c00 -[CALayer layoutSublayers] + 146
17 QuartzCore 0x000000010e58808e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
18 QuartzCore 0x000000010e587f0c _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
19 QuartzCore 0x000000010e57c3c9 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277
20 QuartzCore 0x000000010e5aa086 _ZN2CA11Transaction6commitEv + 486
21 UIKit 0x000000010a4de19b _afterCACommitHandler + 174
22 CoreFoundation 0x000000010a020c37 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
23 CoreFoundation 0x000000010a020ba7 __CFRunLoopDoObservers + 391
24 CoreFoundation 0x000000010a0167fb __CFRunLoopRun + 1147
25 CoreFoundation 0x000000010a0160f8 CFRunLoopRunSpecific + 488
26 GraphicsServices 0x000000010d97bad2 GSEventRunModal + 161
27 UIKit 0x000000010a4b1f09 UIApplicationMain + 171
28 Barntexter 0x00000001096659ff main + 111
29 libdyld.dylib 0x000000010c8d592d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
在以下代码中:
detailviewcontroller.DetailModal = @[Title, Description, Text];
您正在将字典传递到详细信息模式,并且在设置
_TitleLabel.text = _DetailModal[0];
_DescriptionLabel.text = _DetailModal[1];
_TextView.text =_DetailModal[2];
您正在为文本分配字典,这就是它在这里崩溃的原因
试试这个
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"ShowDetails"]) {
DetailViewController *detailviewcontroller = segue.destinationViewController;
NSIndexPath *indexPath= [self.tableView indexPathForSelectedRow];
NSString *sectionTitle = [songSectionTitles objectAtIndex:indexPath.section];
NSArray *sectionName = [Title objectForKey:sectionTitle];
NSString *name = [sectionName objectAtIndex:indexPath.row];
NSArray *sectionDescription = [Description objectForKey:sectionTitle];
NSString *description = [sectionDescription objectAtIndex:indexPath.row];
detailviewcontroller.DetailModal = @[sectionName, name ,description ]
}
}
问题是您正在将
DetailModal
创建为字典的数组,但稍后在DetailViewController
中,您将其用作字符串的数组
在此代码中:
_TitleLabel.text = _DetailModal[0];
_DescriptionLabel.text = _DetailModal[1];
_TextView.text =_DetailModal[2];
self.navigationItem.title = _DetailModal[0];
NSString *path = [[NSBundle mainBundle]pathForResource:_DetailModal[2] ofType:@"txt"];
值\u DetailModal[0]
、\u DetailModal[1]
和\u DetailModal[2]
不是字符串,这就是应用程序崩溃的原因。
它们是字典:您需要从字典中获取正确的文本,然后您可以将其分配给标签。请记录崩溃日志,否则很难找到问题此代码有很多地方可能会失败。因此,请使用日志errordetailviewcontroller.DetailModal=@[Title,Description,Text]更新问题;您正在将字典传递到详细模式,当您设置_TitleLabel.text=_DetailModal[0]时_DescriptionLabel.text=_DetailModal[1]_TextView.text=_DetailModal[2],您正在将字典分配给文本,这就是它在此处崩溃的原因。使用崩溃日志更新了我的问题@MarcoPaceNot work:(我收到错误:“没有可见的@interface for'NSDictionary'声明选择器'objectAtIndex:'”。我使用崩溃日志更新了我的问题。Thanx用于帮助:)@JohanGrip我已更新了答案。请试试这个。不幸的是,仍然不起作用。错误消失了,但它像以前一样崩溃了。在我尝试插入分段分隔符之前它就工作了,所以我想我必须追溯到它在没有分段分隔符的情况下工作的时间。无论如何感谢@iOS\u developerI,我只想补充一点,虽然这可能是或可能不是确切的问题(太多的代码让我读不懂!),但问题很明显,字典被分配给了
text
属性,因为崩溃就是这么说的:-[\uu NSDictionaryI length]:无法识别的选择器发送到实例0x7fc813f07e10'
(在IsequalString
中,在setText
中)。所以这可能是完全正确的,或者至少应该足够近,让你到达那里。