gpt4 book ai didi

objective-c - 将数据传递给 parentViewController [iOS]

转载 作者:可可西里 更新时间:2023-11-01 05:37:20 27 4
gpt4 key购买 nike

我有这个 Storyboard: enter image description here

当我在第一个 View Controller (称为 newCourseViewController)中按下“Insegnante”按钮时,它会显示一个带有教师列表的表格 View 。当我按下老师(并调用方法 tableView:canEditRowAtIndexPath:)时,我希望 UITableViewController 将按下的对象“传递”到第一个 View Controller 。

这是我的第一个 View Controller newCourseViewController.h

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

@interface newCourseViewController : UIViewController

@property (nonatomic , strong) Teacher *teacher;

@end

这是我第一个 View Controller 的代码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];
}
}

现在是第二个 View Controller 的代码 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 方法的第一个 View Controller 中,我将自己设置为第二个 View 中的 previousViewController。然后我“通过”老师选择的,然后我解散第二个 View Controller 。当应用程序执行 [self.navigationController popViewControllerAnimated:YES]; Xcode 崩溃和模拟器崩溃。我不知道是什么问题。你能帮帮我吗?

最佳答案

要将值发送到父 Controller ,您必须使用协议(protocol)。我将提供您应该采取的正确步骤,以使您想要的功能正常工作。

1.为您的 AddTeacherToCourseController 创建协议(protocol)。在您的 AddTeacherToCourseController.h 中,在导入的正下方添加以下内容:

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

在接口(interface)标签下面添加:

@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]];
}

[此方法将通过协议(protocol)调用您的委托(delegate)方法并将您选择的教授传递给父 Controller ]

3。在你的父 Controller 中,你的 newCourseViewController.h 在接口(interface)行之后添加:

<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.在界面生成器中:

  • 从 Insegnante 按钮中删除您的 segue。
  • 将“addTeacherToCourseViewController”的 Storyboard Id 修改为“addTeacherToCourseViewController”

6.在 newCourseViewController.h 中编写您的委托(delegate)方法:

- (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];
}

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

关于objective-c - 将数据传递给 parentViewController [iOS],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13961557/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com