1

我正在尝试将数据从表视图控制器传递到详细视图控制器。我的表中的每个条目都可以正常工作,除了一个。这是 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

任何帮助将不胜感激。

4

3 回答 3

7

看起来像:

[destViewController setIden:[notification objectForKey:@"identifier"]];

正在返回 NSNumber 而不是 NSString。您可以尝试将该行替换为:

[destViewController setIden:[(NSNumber*)[notification objectForKey:@"identifier"] stringValue]];
于 2013-08-07T20:41:12.320 回答
2

某处你正在传递 aNSNumber而不是 a NSString

于 2013-08-07T20:40:56.717 回答
1

要设置文本,iden需要是NSString. 当您从 获取对象时NSDictionary,它是一个NSNumber.

试试这个:

[destViewController setIden:(NSString *)[notification objectForKey:@"identifier"]];

或将iden属性更改为 aNSNumber然后

[[self idenField] setText:[NSString stringWithFormat:@"%d", [self iden]]];    
于 2013-08-07T20:49:34.037 回答