我正在尝试将数据从表视图控制器传递到详细视图控制器。我的表的每个条目都可以正常工作,除了一个。 这是prepareForSegue方法:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier] isEqualToString:@"showDetails"]) {
NSIndexPath *indexPath = [[self tableView] indexPathForSelectedRow];
NSDictionary *notification = [[self notifications] objectAtIndex:[indexPath row]];
NRDetailViewController *destViewController = [segue destinationViewController];
[destViewController setDate:[notification objectForKey:@"date"]];
[destViewController setFrom:[notification objectForKey:@"from"]];
[destViewController setIden:[notification objectForKey:@"identifier"]];
[destViewController setPriority:[notification objectForKey:@"priority"]];
[destViewController setSubject:[notification objectForKey:@"subject"]];
[destViewController setMessage:[notification objectForKey:@"text"]];
}
}
这是我的详细视图界面:
#import <UIKit/UIKit.h>
@interface NRDetailViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *dateField;
@property (weak, nonatomic) IBOutlet UITextField *fromField;
@property (weak, nonatomic) IBOutlet UITextField *idenField;
@property (weak, nonatomic) IBOutlet UITextField *prioField;
@property (weak, nonatomic) IBOutlet UITextField *subjectField;
@property (weak, nonatomic) IBOutlet UITextView *messageView;
@property (copy, nonatomic) NSString *date;
@property (copy, nonatomic) NSString *from;
@property (copy, nonatomic) NSString *iden;
@property (copy, nonatomic) NSString *priority;
@property (copy, nonatomic) NSString *subject;
@property (copy, nonatomic) NSString *message;
@end
这是详细视图的实现文件:
#import "NRDetailViewController.h"
@interface NRDetailViewController ()
@end
@implementation NRDetailViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[[self dateField] setText:[self date]];
[[self fromField] setText:[self from]];
[[self idenField] setText:[self iden]];
[[self prioField] setText:[self priority]];
[[self subjectField] setText:[self subject]];
[[self messageView] setText:[self message]];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
我在将idenField的文本设置为iden的文本时遇到问题。我添加了一个异常断点,并且确实在此行发生了异常:
[[self idenField] setText:[self iden]];
此时,我已经打印了[self iden]的值,它甚至包含了我想传递的内容,所以我完全不知道问题是什么,因为正如我所说的,所有其他字段都在工作他们应该这样做。
抛出的异常是:
2013-08-07 22:28:20.539 NotificationReader[1214:11303] -[__NSCFNumber length]: unrecognized selector sent to instance 0x7587a00
非常感谢任何帮助。
答案 0 :(得分:7)
看起来像:
[destViewController setIden:[notification objectForKey:@"identifier"]];
返回NSNumber而不是NSString。您可以尝试用以下代码替换该行:
[destViewController setIden:[(NSNumber*)[notification objectForKey:@"identifier"] stringValue]];
答案 1 :(得分:3)
您正在某处传递NSNumber
而不是NSString
。
答案 2 :(得分:1)
要设置文字,iden
必须为NSString
。当您从NSDictionary
获取对象时,它是NSNumber
。
试试这个:
[destViewController setIden:(NSString *)[notification objectForKey:@"identifier"]];
或将iden
属性更改为NSNumber
,然后
[[self idenField] setText:[NSString stringWithFormat:@"%d", [self iden]]];