0

I'm trying to pass data to detail page upon clicking a cell inside UITabelViewCell using NSCoding Protocol

NSCoder *coder =[[NSCoder alloc] init];

[coder encodeObject:@"value" forKey:@"title"];

[coder encodeObject:cell.remainderContentLabel.text forKey:@"content"];

[coder encodeObject:cell.remainderDateTimeLabel.text forKey:@"datetime"];

[coder encodeObject:cell.remainterImageView.image forKey:@"image"];

RemainderDetailViewController *detailVC = [[RemainderDetailViewController alloc] initWithCoder:coder];

[self.navigationController pushViewController:detailVC animated:YES];

But it is crashing msg is

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -encodeObject:forKey: cannot be sent to an abstract object of class NSCoder: Create a concrete instance!'

In detail page decoding is done by

- (id) initWithCoder:(NSCoder *) aDecoder {

    if(self = [super initWithCoder:aDecoder]) {

      _remainderTitle.text =  [aDecoder decodeObjectForKey:@"title"];

      _remainderDetails.text =  [aDecoder decodeObjectForKey:@"content"];

      _dateAndTime.text =   [aDecoder decodeObjectForKey:@"datetime"];

      _remainderImage.image =  [aDecoder decodeObjectForKey:@"image"];

    }

    return self;
}

How to use

- (void)encodeWithCoder:(NSCoder *)encoder

What is wrong im doing? Class is confirmed to NSCopying protocol

Saranjith
  • 11,242
  • 5
  • 69
  • 122
  • https://stackoverflow.com/questions/22168753/unrecognized-selector-sent-to-instance-while-archiving-data-nscoding ? But to pass data inside a UITableViewCell, I don't see why use NSCoding. – Larme Jul 31 '17 at 12:23
  • when this encodeWithCoder getting called? how can i push the VC – Saranjith Jul 31 '17 at 12:24
  • The detail page is needed to be reused in many places. So i can simply write initWithCoder from anywhere – Saranjith Jul 31 '17 at 12:26
  • I read more carefully your code, and the `RemainderDetailViewController` code doesn't make really sense. `RemainderDetailViewController` has a xib? A storyboard ? – Larme Jul 31 '17 at 12:26
  • Its is storyborad @Larme – Saranjith Jul 31 '17 at 12:27
  • Then use Segue instead ? – Larme Jul 31 '17 at 12:27
  • Then how can i give initWithCoder if im using segue. It will be performSegue – Saranjith Jul 31 '17 at 12:28
  • Do not use `initWithCoder:`. I don't think it's usefull in your code. It's quite an "advance" knowledge to have, but if you are missing the `performSegue:`/Storyboard uses to pass data or reuses sames ViewController with different data/options, then study them instead. – Larme Jul 31 '17 at 12:30
  • Good. All thus i have tried. Okey can you please give an example where i can implement the NSCoding. Because i want to go in some deep. – Saranjith Jul 31 '17 at 12:35
  • http://nshipster.com/nscoding/ Use it to save data, not really UIViewController (for 99% of the cases). – Larme Jul 31 '17 at 12:36
  • Yes thank you .. Hope i ll use former methods for now – Saranjith Jul 31 '17 at 12:38

2 Answers2

1

AS I know - (id) initWithCoder:(NSCoder *) aDecoder is called when you load a ViewController from storyboard. If you want to push to a ViewController in the storyboard using code. You can

RemainderDetailViewController *detailVC = [[UIStoryboard storyboardWithName:@"XXX" bundle:nil] instantiateViewControllerWithIdentifier:@"yourControllerIdentifier"];
detailVC.paramDic = @{...};
[self.navigationController pushViewController:detailVC animated:YES];

The "yourControllerIdentifier" is defined in the storyboard.You can't initialize a ViewController by using the method directly. And in the destination ViewController - (id) initWithCoder:(NSCoder *) aDecoder can be fired. You can do some initial work in it.

Ax1le
  • 6,563
  • 2
  • 14
  • 61
0

Use a dictionary to pass a bunch of objects.

But if you want to use an archive, read the documentation of NSCoder, it says:

The NSCoder abstract class declares the interface used by concrete subclasses to transfer objects and other values between memory and some other format.

The concrete subclasses provided by Foundation for these purposes are NSArchiver, NSUnarchiver, NSKeyedArchiver, NSKeyedUnarchiver, and NSPortCoder.

Use NSKeyedArchiver.

Community
  • 1
  • 1
Willeke
  • 14,578
  • 4
  • 19
  • 47