gpt4 book ai didi

iphone - 将数据传回前一个 View Controller

转载 作者:IT老高 更新时间:2023-10-28 11:33:43 25 4
gpt4 key购买 nike

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

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

我正在使用 this answer 的“传回数据”部分.我的不同之处:第 3 点和第 6 点只提到了何时弹出 View ,所以我将该代码放在 viewWillDisappear 中。我认为这是正确的?同样在第 6 点上,我没有使用 nib 进行初始化,因为它是旧的。我正在使用 Storyboard。我没有添加最后一行,因为我不相信我必须插入它。在我的 Storyboard上按下一个按钮已经让我前进了。

我认为问题可能出现在 BIDDCCreateViewController 中,我有方法但无法运行。要运行一个方法,它应该去 [self 方法]。我无法做到这一点。嗯,这正是我的猜测。

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

更新:我无法执行“sendDataToA”方法。

#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

最佳答案

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

如果您不熟悉 Objective-C ,请查看 What is Delegate .

在 ViewControllerB.h 中创建协议(protocol):

#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

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

}

在您的 ViewControllerA 中:当您转到 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
}

已编辑:

您可以查看此示例:如何将数据传递回之前的 View Controller :Tutorial link

关于iphone - 将数据传回前一个 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19343519/

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