简体   繁体   中英

Display selected CoreData value from UITableView in UITextView

I've got a TableViewController which fetches the CoreData . Furthermore if I click on a cell it segues to the DetailViewController . But I don't know how I can display the Value in a UITextView of the selected row in said DetailViewController .

AppDelegate *delegate = [[UIApplication sharedApplication]delegate];
_managedObjectContext  = delegate.managedObjectContext;

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]initWithEntityName:@"Note"];
_fetchedResultsController = [[NSFetchedResultsController alloc]initWithFetchRequest:fetchRequest managedObjectContext:_managedObjectContext sectionNameKeyPath:nil cacheName:nil];

[_fetchedResultsController setDelegate:self];
NSError *error = nil;
[_fetchedResultsController performFetch:&error];
if (error) {
    NSLog(@"Unable to perform Fetch");
    NSLog(@"%@, %@", error, error.localizedDescription);
    
    
    NSArray *objects = [_managedObjectContext executeFetchRequest:fetchRequest error:&error];

How can I display the value and how do I ensure that the displayed value is the one which I did select in the TableViewController ?

You don't have to fetch anything in the detailViewController, as the tableViewController already has the object. Simply pass the text (or the object) to the detail view controller.

Add a property to your detailViewController. (It could either be an NSString or an NSManagedObject entity.)

@property (nonatomic, strong) NSString *detailText

In prepareForSegue:sender: , set the detailViewController's property to the (text string of) the NSManagedObject associated with the selected row:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)__unused sender
{
    if ([[segue identifier] isEqualToString:@"showDetail"])
    {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        NSManagedObject *object = [[self fetchedResultsController] objectAtIndexPath:indexPath];
        DetailViewController *controller = (DetailViewController *)[[segue destinationViewController] topViewController];
        controller.detailText = [object valueForKey:@"myTextProperty"];
    }
}

In the detail view controller, assign the text to the UITextView .

myTextview.text = self.detailText;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM