Domanda

I am using the following to create a reference to a view controller in my code like this:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  if([segue.identifier isEqualToString:@"detailSegue"]){
    NSIndexPath *indexPath=[self.tableView indexPathForSelectedRow];
    TRDetailViewController *vc=segue.destinationViewController;
    vc.item=self.items[indexPath.row];
    [vc setValue:self forKeyPath:@"delegate"];
    [vc setValue:indexPath.row forKeyPath:@"itemIndex"];
  }
}

and this is declared as in TRDetailViewController

@interface TRDetailViewController : UIViewController
  @property (weak, nonatomic) IBOutlet UILabel *detailLabel;
  @property NSString *item;
  @property (nonatomic, weak)id delegate;
  @property NSInteger itemIndex;
  - (IBAction)deleteItem:(id)sender;
@end

but I get the following error

enter image description here

and I'm not sure why. I am trying to pass the location in an index of the cell selected in a UITableView. If a better way, please let me know. Why am I getting this error?

thx

È stato utile?

Soluzione

setValue:forKey: is expecting an object for its value argument and you are passing it a primitive (in this case, an integer). You can work around this by amending your method to read as follows:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  if([segue.identifier isEqualToString:@"detailSegue"]){
    NSIndexPath *indexPath=[self.tableView indexPathForSelectedRow];
    TRDetailViewController *vc=segue.destinationViewController;
    vc.item=self.items[indexPath.row];
    [vc setValue:self forKeyPath:@"delegate"];
    [vc setValue:[NSNumber numberWithInt:indexPath.row] forKeyPath:@"itemIndex"];
  }
}

And then access it like:

[[vc valueForKey:@"itemIndex"] intValue];

Altri suggerimenti

setValue:forKeyPath: requires an object (i.e. id) as first argument.

You are instead passing a NSInteger.

You will have to use an object type, which means to turn your itemIndex into a NSNumber.

Then you can store it using

[vc setValue:@(indexPath.row) forKeyPath:@"itemIndex"];

and retrieve it by

NSInteger myIndex = [vc valueForKey:@"itemIndex"].integerValue;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top