xcode 如何将值传递给父视图控制器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14106176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:25:47  来源:igfitidea点击:

How to pass a value to parent view controller?

iphoneobjective-ciosxcodestoryboard

提问by hyd00

Possible Duplicate:
dismissModalViewController AND pass data back

可能的重复:
dismissModalViewController 并传回数据

I'm new to ios development and stuck on this problem:

我是 ios 开发的新手并被困在这个问题上:

I'm using storyboarding and have a navigation controller, vcA, with a TableViewin it which shows some data from a MutableArray(which is initialized in viewdidloadof the same class). After selecting any cell, a second view controller, vcB, is shown with a TextFieldin it and a button called "Add to list".

我正在使用情节提要并有一个导航控制器 ,vcA其中带有 aTableView显示来自 a 的一些数据MutableArrayviewdidload在同一个类中初始化)。选择任何单元格后,将显示第二个视图控制器 ,vcB其中包含一个TextField和一个名为“添加到列表”的按钮。

What I want is that when I enter some text in the TextFieldand press the "Add to list" button the text should be added to the array of previous view (which gets shown in the TableView) and when i tap the "Back" button on vcB's navigation bar, vcAshould show the updated TableViewwith the new entry in it (on the top of list). Basically I want to add the text from vcB's TextFieldto the array of vcAand show the new array after clicking the BACK button.

我想要的是,当我在 中输入一些文本TextField并按下“添加到列表”按钮时,文本应该添加到上一个视图的数组中(显示在 中TableView),当我点击vcB'上的“返回”按钮时s 导航栏,vcA应显示其中TableView包含新条目的更新内容(在列表顶部)。基本上,我想将vcB's 中的文本添加TextField到 of 的数组中,vcA并在单击 BACK 按钮后显示新数组。

I have searched a lot about this issue and seem to find that delegate and protocols is the way to achieve the desired result but I'm having trouble understanding delegation.

我对这个问题进行了大量搜索,似乎发现委托和协议是实现预期结果的方法,但我无法理解委托。

回答by gg13

I have the second view controller presenting itself as modal in this example:

在此示例中,我将第二个视图控制器显示为模态:

In the second view controllers h file:

在第二个视图控制器 h 文件中:

@protocol SecondViewControllerDelegate <NSObject>
- (void)addItemViewController:(id)controller didFinishEnteringItem:(NSString *)item;
@end

@interface SecondPageViewController : UIViewController <UITextViewDelegate>
{
     NSString *previouslyTypedInformation;
}

@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (nonatomic) NSString *previouslyTypedInformation;
@property (nonatomic, weak) id <SecondViewControllerDelegate> delegate;

In the second view controllers m file make sure to synthesize properties and add then add this:

在第二个视图控制器 m 文件中,确保合成属性并添加然后添加:

- (IBAction)done:(id)sender
{
     NSString *itemToPassBack = self.textView.text;
     NSLog(@"returning: %@",itemToPassBack);
     [self.delegate addItemViewController:self didFinishEnteringItem:itemToPassBack];
     //dismiss modal view controller here
}

Then in the first view controllers h file set it as delegate:

然后在第一个视图控制器 h 文件中将其设置为委托:

 @interface FirstPageViewController: UIViewController <SecondViewControllerDelegate>
 @property (nonatomic) NSString *returnedItem;

Then in the first view controller's m file synthesize and add the method:

然后在第一个视图控制器的 m 文件中合成并添加方法:

- (void)addItemViewController:(SecondPageViewController *)controller didFinishEnteringItem:    (NSString *)item
{
    //using delegate method, get data back from second page view controller and set it to property declared in here
    NSLog(@"This was returned from secondPageViewController: %@",item);
    self.returnedItem=item;

    //add item to array here and call reload
}

Now you have the text of what was returned! You can add the string to your array in the first view controller's viewDidLoad and call

现在你有返回的文本了!您可以在第一个视图控制器的 viewDidLoad 中将字符串添加到数组中并调用

[self.tableView reloadData]; 

and it should work.

它应该工作。