Pass data back to previous 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.
我正在使用 this answer 的传回数据"部分.我的不同之处:第 3 点和第 6 点只提到了何时弹出视图,所以我将该代码放在 viewWillDisappear 中.我认为这是正确的?同样在第 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 中,我有方法但无法运行.要运行一个方法,它应该去 [self 方法].我无法做到这一点.嗯,这正是我的猜测.
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];
}
在您的 ViewControllerA 中:当您转到 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
}
已
您可以查看此示例:如何将数据传递回以前的视图控制器:教程链接
You can See this example : How you can Pass data back to previous viewcontroller: Tutorial link
这篇关于将数据传回前一个视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!