将数据传递给parentViewController [iOS]

时间:2012-12-19 21:32:17

标签: objective-c ios uitableview parent-child segue

我有这个故事板: enter image description here

当我按下第一个视图控制器中的“Insegnante”按钮时(它被称为 newCourseViewController ),它会显示一个包含教师列表的表格视图。当我按下教师(并调用方法tableView:canEditRowAtIndexPath:)时,我希望UITableViewController“传递”按下的对象到第一个视图控制器。

这是我的第一个视图控制器的代码 newCourseViewController.h

#import <UIKit/UIKit.h>
#import "Teacher.h"

@interface newCourseViewController : UIViewController

@property (nonatomic , strong) Teacher *teacher;

@end

这是我的第一个视图控制器的代码 newCourseViewController.m (只有重要的代码)

#import "newCourseViewController.h"
#import "Courses.h"
#import "Teacher.h"
#import "addTeacherToCourseViewController.h"

@interface newCourseViewController ()
@property (weak, nonatomic) IBOutlet UITextField *textField;
@end

@implementation newCourseViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)setTeacher:(Teacher *)teacher
{
    self.teacher = teacher;
    NSLog(@"Maestro settato!");
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"addTeacherToCourse"]) {
        [segue.destinationViewController setPreviousViewController:self];
    }
}

现在第二个视图控制器的代码 addTeacherToCourseViewController-h

@interface addTeacherToCourseViewController : UITableViewController

@property (nonatomic , weak) id previousViewController;

@end

addTeacherToCourseViewController.m (只有重要方法)

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Teacher *teacher = [self.teachers getTeacherInPosition:indexPath.row];
    [self.previousViewController setTeacher:teacher];
    [self.navigationController popViewControllerAnimated:YES];

}

prepareForSegue方法的第一个视图控制器中,我将自己设置为第二个视图中的previousViewController。然后我“通过”选择的老师,而不是我解雇第二个视图控制器。 当应用程序执行[self.navigationController popViewControllerAnimated:YES]; Xcode崩溃并且模拟器崩溃时。 我无法弄清楚问题是什么。你能救我吗?

1 个答案:

答案 0 :(得分:5)

要将值发送到父控制器,您必须使用协议。我将提供您应采取的适当步骤,以使您所需的功能正常工作。

1。 为AddTeacherToCourseController创建协议。 在AddTeacherToCourseController.h中,在导入下方添加以下内容:

@protocol AddTeacherToCourseControllerProtocol <NSObject>
    - (void)yourDelegateMethod:(Teacher *)insegnante;
@end

在接口标记下方添加:

@property (strong, nonatomic) id <AddTeacherToCourseControllerProtocol> delegate;

2。 在AddTeacherToCourseController.m中:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // I would use the existing array you are using to display the teachers in order to select the correct one you want to send back like this:
    // Teacher *teacher = [self.teachers getTeacherInPosition:indexPath.row];
    [self.delegate yourDelegateMethod:[yourTeacherArray objectAtIndex:indexPath.row]];
}

[此方法将通过协议调用您的委托方法,并将您选择的教授传递给父控制器]

3。 在你的父控制器中,你的newCourseViewController.h就在接口行之后添加:

<AddTeacherToCourseControllerProtocol>

4。 如果您没有Insegnante按钮操作,请在界面构建器[拖动和命名]中创建一个。然后将以下内容添加到此操作:

// assuming your storyboard is named MainStoryboard. here you create your segue programmatically:
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
addTeacherToCourseViewController *addTeacherController = (addTeacherToCourseViewController *)[storyBoard instantiateViewControllerWithIdentifier:@"addTeacherToCourseViewController"];
addTeacherController.delegate = self;
[self.navigationController pushViewController:addTeacherController animated:YES];

5。 在Interface Builder中:

  • 从Insegnante按钮中移除您的segue。
  • 将'addTeacherToCourseViewController'的故事板ID编辑为'addTeacherToCourseViewController'

6。 在newCourseViewController.h中编写你的委托方法:

- (void)yourDelegateMethod:(Teacher *)insegnante{
    // Do whatever you want with your Insegnante
    // and be sure to pop the second controller from the view stack:
    [self.navigationController popViewControllerAnimated:YES];
}

如果您有疑问并且我的回答对任何人都有帮助,请告诉我。