在视图控制器segue之间传递数据 [英] Pass data between view controller segue

查看:87
本文介绍了在视图控制器segue之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的第一个视图控制器, AllAthletes 是所有我的核心数据实体的一个合适的视图。它显示实体运动员,它的属性在字幕样式表单元格中,如名字等。当你点击一个视图单元格,我想让这个视图控制器传递被选择的实体的信息, m不确定如何将此信息(管理对象,实体,索引路径的属性等)传递到详细视图控制器。

 <$ c $ 

c> - (void)viewWillAppear:(BOOL)animated {
AppDelegate * appDelegate = [[UIApplication sharedApplication] delegate];
_managedObjectContext = [appDelegate managedObjectContext];

NSFetchRequest * request = [[NSFetchRequest alloc] init];
NSEntityDescription * athlete = [NSEntityDescription entityForName:@AthleteinManagedObjectContext:_managedObjectContext];
[request setEntity:athlete];
NSSortDescriptor * sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@firstascending:YES];
NSArray * sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor,nil];
[request setSortDescriptors:sortDescriptors];

NSError * error = nil;
NSMutableArray * mutableFetchResults = [[_managedObjectContext executeFetchRequest:request error:& error] mutableCopy];
if(mutableFetchResults == nil){
//句柄错误
}
[self setAthleteArray:mutableFetchResults];
[self.tableView reloadData];
}



//这是我到目前为止
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id) sender
{
if([segue.identifier isEqualToString:@setAthlete]){
NSIndexPath * indexPath = [self.tableView indexPathForSelectedRow];
AthleteDetail * destViewController = segue.destinationViewController;
}
}

//我也在想这样吗?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIViewController * detailViewController = [self.storyboard instantiateViewControllerWithIdentifier:@DetailViewController];
NSMutableString * object = thisArray [indexPath.row];
detailViewController.passedData = object;
[self.navigationController pushViewController:detailViewController animated:YES];
}

它推送你的第二个控制器是AthleteDetail。谢谢您的时间。

解决方案

您正在混合两个不同的概念。而不是

   - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
UIViewController * detailViewController = [self.storyboard instantiateViewControllerWithIdentifier:@DetailViewController];
NSMutableString * object = thisArray [indexPath.row];
detailViewController.passedData = object;
[self.navigationController pushViewController:detailviewController animated:YES];
}

您应该执行

   - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
[self performSegueWithIdentifier:YOUR_SEGUE_IDENTIFIER sender:self];
}

,然后:

   - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{
if([segue.identifier isEqualToString:YOUR_SEGUE_IDENTIFIER]){
NSIndexPath * indexPath = [self.tableView indexPathForSelectedRow];
AthleteDetail * destViewController = segue.destinationViewController;
//在你的目标视图控制器中设置任何你想要的东西
destViewController.passedData = object;
}
}

并确保您的Segue是Push Segue。


My first view controller, AllAthletes, is a uitableview of all my core data entities. It displays the entity "athlete" and it's properties in a subtitle style table cell such as first name, etc.. When you click on a view cell, I want this view controller to pass the information of the entity that was selected but i'm unsure on how to pass this information (managedobject, entity, properties, etc of indexpath) along to the detail view controller. Could someone please point me in the right direction?

allathletes.m

-(void)viewWillAppear:(BOOL)animated{
    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    _managedObjectContext = [appDelegate managedObjectContext];

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *athlete = [NSEntityDescription entityForName:@"Athlete" inManagedObjectContext:_managedObjectContext];
    [request setEntity:athlete];
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"first" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc]initWithObjects:sortDescriptor, nil];
    [request setSortDescriptors:sortDescriptors];

    NSError *error = nil;
    NSMutableArray *mutableFetchResults = [[_managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
    if (mutableFetchResults == nil){
        //handle error
    }
    [self setAthleteArray:mutableFetchResults];
    [self.tableView reloadData];
}



 //this is what I have so far
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
     if ([segue.identifier isEqualToString:@"setAthlete"]) {
         NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
         AthleteDetail *destViewController = segue.destinationViewController;
     }
 }

//i was also thinking something like this?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIViewController* detailViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"];
    NSMutableString *object = thisArray[indexPath.row];
    detailViewController.passedData = object;
    [self.navigationController pushViewController:detailViewController animated:YES];
}

The second controller that it pushes you to is AthleteDetail. Thank you for your time.

解决方案

You are mixing two different concepts. Instead of

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIViewController* detailViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"];
    NSMutableString *object = thisArray[indexPath.row];
    detailViewController.passedData = object;
    [self.navigationController pushViewController:detailViewController animated:YES];
}

You should do something like

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self performSegueWithIdentifier:YOUR_SEGUE_IDENTIFIER sender:self];        
}

and then:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
     if ([segue.identifier isEqualToString:YOUR_SEGUE_IDENTIFIER]) {
         NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
         AthleteDetail *destViewController = segue.destinationViewController;
         // set whatever you want here in your destination view controller
         destViewController.passedData = object;
     }
 }

And make sure that your Segue is a Push Segue.

这篇关于在视图控制器segue之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆