将数据传递回以前的viewcontroller [英] Pass data back to previous viewcontroller

查看:110
本文介绍了将数据传递回以前的viewcontroller的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将数据传递回以前的viewController。

I am trying to pass data BACK TO previous viewController.

有谁知道如何将数据从ViewController B传回ViewController A?所以我想要一个字符串从'BIDAddTypeOfDealViewController转到BIDDCCreateViewController。用户编辑了viewController B,我希望在ViewController A中将编辑后的数据返回到我然后使用它。

Does anyone know how to pass data back from ViewController B to ViewController A? So I want a string to go 'from' BIDAddTypeOfDealViewController to BIDDCCreateViewController. A user edits viewController B and I want that edited data back in ViewController A where I then use it.

我正在使用此答案。我的不同之处:第3点和第6点只是在弹出视图时提及,因此我将该代码放在viewWillDisappear中。我认为这是正确的吗?
同样在Point 6我没有用nib初始化,因为它已经老了。我正在使用故事板。而且我没有添加最后一行,因为我不相信我必须推动它。按下我的故事板上的按钮已经让我前进了。

I am using the 'passing data back' section of this answer. How mine differs: Point 3 and 6 just mentions when views are popped so I have put that code in viewWillDisappear. I think that is correct? Also on Point 6 I did not initialise with nib as that is old. I'm using storyboards. And I did not add that last line as I do not believe I would have to push it. Pressing a button on my storyboard already takes me forward.

我认为BIDDCCreateViewController中可能出现问题,我有方法,但我无法运行它。要运行一个方法,它应该[自我方法]。我无法做到这一点。那就是我猜的。

I think the problem may arise in BIDDCCreateViewController, I have the method but I cannot run it. To run a method it should go [self method]. I am unable to do that. Well that is just what I am guessing.

它编译并运行良好,没有记录任何内容,所以我不知道它是否有效。

It compiles and runs fine just nothing is logged, so I don't know if it works.

更新:我无法获得'sendDataToA'方法来执行。

UPDATE: I am unable to get the 'sendDataToA' method to execute.

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

 @interface BIDDCCreateViewController : UIViewController
 @property (strong, nonatomic) NSString *placeId;
- (IBAction)gotoBViewController:(id)sender;
@end


#import "BIDDCCreateViewController.h"
#import "BIDAddTypeOfDealViewController.h"

@implementation BIDDCCreateViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSLog(@"SUCCESSFULLY PASSED PLACE ID: %@", self.placeId);
}

-(void)sendDataToA:(NSString *)myStringData
{

    NSLog(@"Inside sendDataToA");
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Your string Data Showing" message:myStringData delegate:self cancelButtonTitle:@"Ok " otherButtonTitles:nil];
    [alert show];
}

- (IBAction)gotoBViewController:(id)sender {
    NSLog(@"pressed");
    BIDAddTypeOfDealViewController *bidAddType = [[BIDAddTypeOfDealViewController alloc]init];
    bidAddType.delegate = self;

}
@end


@protocol senddataProtocol <NSObject>
-(void)sendDataToA:(NSString *)myStringData;
@end

#import <UIKit/UIKit.h>
@interface BIDAddTypeOfDealViewController : UIViewController <UITextFieldDelegate>//Using this delegate for data a user inputs
@property(nonatomic,assign)id delegate;
//other textfield outlets not relevant
- (IBAction)chooseDiscountDeal:(id)sender;
@end

#import "BIDAddTypeOfDealViewController.h"

@interface BIDAddTypeOfDealViewController ()

@end

@implementation BIDAddTypeOfDealViewController
@synthesize delegate;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [delegate sendDataToA:@"Apple"];
}
@end


推荐答案

您可以使用委托。因此,在ViewController B中,您需要创建一个将数据发送回ViewController A的协议。您的ViewController A将成为ViewController B的委托。

You can use a delegate. So in your ViewController B you need to create a protocol that sends data back to your ViewController A. Your ViewController A would become a delegate of ViewController B.

如果您是新的对于目标C,请查看什么是代表

If you are new to objective C, please look at What is Delegate.

在ViewControllerB.h中创建协议:

Create protocol in ViewControllerB.h :

#import <UIKit/UIKit.h>

@protocol senddataProtocol <NSObject>

-(void)sendDataToA:(NSArray *)array; //I am thinking my data is NSArray, you can use another object for store your information. 

@end

@interface ViewControllerB : UIViewController

@property(nonatomic,assign)id delegate;

ViewControllerB.m

ViewControllerB.m

@synthesize delegate;
-(void)viewWillDisappear:(BOOL)animated
{
     [delegate sendDataToA:yourdata];

}

:当您转到ViewControllerB时

in your ViewControllerA : when you go to ViewControllerB

ViewControllerA *acontollerobject=[[ViewControllerA alloc] initWithNibName:@"ViewControllerA" bundle:nil];
acontollerobject.delegate=self; // protocol listener
[self.navigationController pushViewController:acontollerobject animated:YES];

并定义你的功能:

-(void)sendDataToA:(NSArray *)array
{
   // data will come here inside of ViewControllerA
}

已编辑:

您可以看到此示例:如何将数据传回到之前的viewcontroller:教程链接

You can See this example : How you can Pass data back to previous viewcontroller: Tutorial link

这篇关于将数据传递回以前的viewcontroller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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