如何将值传递给父视图控制器? [英] How to pass a value to parent view controller?

查看:78
本文介绍了如何将值传递给父视图控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

dismissModalViewController并传回数据

我是新来的ios开发并坚持这个问题:

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

我正在使用故事板并且有一个导航控制器, vcA ,一个 TableView ,其中显示来自 MutableArray 的一些数据(在 viewdidload <中初始化同一类)。选择任何单元格后,第二个视图控制器 vcB 会显示一个 TextField ,并显示一个名为添加到列表。

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

我想要的是当我在 TextField 中输入一些文本并按下添加到列表按钮文本应添加到上一个视图的数组(显示在 TableView 中),当我点击<$ c上的后退按钮时$ c> vcB 的导航栏, vcA 应该显示更新的 TableView 其中的新条目(在列表的顶部)。基本上我想将 vcB TextField 中的文本添加到 vcA <的数组中/ code>并在单击BACK按钮后显示新数组。

What I want is that when I enter some text in the TextField and 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, vcA should show the updated TableView with the new entry in it (on the top of list). Basically I want to add the text from vcB's TextField to the array of vcA and show the new array after clicking the BACK button.

我已经搜索了很多关于这个问题并且似乎发现委托和协议是达到预期结果的方法,但我无法理解授权。

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.

推荐答案

我让第二个视图控制器呈现为模态在这个例子中:

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

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

In the second view controllers h file:

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

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

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

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

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

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

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

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

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

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

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

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

[self.tableView reloadData]; 

它应该有效。

这篇关于如何将值传递给父视图控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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