ios - 将数据从委托(delegate) View 发送到委托(delegate) View [Objective-C]

标签 ios objective-c iphone uiviewcontroller delegates

所以我正在尝试学习如何将数据从一个 View Controller 发送到另一个 View Controller 。

到目前为止,我的代码获取从一个 VC (DelegateVC) 输入的数据,并将其显示在另一个 VC (ViewController) 的标签中。

这里的委托(delegate)是ViewController并且委托(delegate)是DelegateVC .

==================================================== ==========================

主要问题

但是如果我想从 ViewController 传递标签的数据怎么办?至DelegateVC ?我怎样才能做到这一点?我尝试设计相同的委托(delegate)概念,但使用 DelegateVC作为ViewController的委托(delegate).不确定这是否是写入方法。

TL;博士:

VC , 如果我点击 Next View , 我去 DelegateVC并输入“test”,它将显示在VC's receiving label作为“测试”。

现在下次我点击 Next View ,我希望现有标签的值显示在 TextFieldDelegateVC .而不是空白 TextField ,会有值“test”。

==================================================== ==========================
假设我有一个 Main.storyboard像这样:

description
我有一个 ViewController.h文件为:

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController


@end

ViewController.m :
#import "ViewController.h"
#import "DelegatedVC.h"

@interface ViewController () <MainVCDelegate>
@property (strong, nonatomic) IBOutlet UILabel *receivingLabel;
- (IBAction)nextView:(id)sender;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)nextView:(id)sender {

}

-(void) sendBackData:(DelegatedVC *)delegatedVC :(NSString *)textField {
    [delegatedVC dismissViewControllerAnimated:YES completion:nil];
    self.receivingLabel.text = textField;
}
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    DelegatedVC *secondVC = segue.destinationViewController;
    secondVC.delegate = self;
}
@end

DelegateVC.h:
#import <UIKit/UIKit.h>
@protocol MainVCDelegate;

@interface DelegatedVC : UIViewController
@property (strong, nonatomic) IBOutlet UITextField *sendDataTF;
- (IBAction)sendData:(id)sender;
@property (weak, nonatomic) id<MainVCDelegate> delegate;
@end

@protocol MainVCDelegate <NSObject>
-(void) sendBackData: (DelegatedVC *) delegatedVC : (NSString *) textField;
@end

DelegateVC.m:
#import "DelegatedVC.h"
@protocol MainVCDelegate;

@interface DelegatedVC ()

@end

@implementation DelegatedVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)sendData:(id)sender {
    [self.delegate sendBackData: self : _sendDataTF.text];
}
@end

最佳答案

But what if I want to pass the data of the label from ViewController to DelegateVC? How can I accomplish this?



因此,正如我在您的代码中看到的那样,您正在正确地进行委派。回到你的主要问题,你只需要声明一个公共(public) NSString您的 DelegateVC.h 中的对象, 然后访问它并将数据从您的 ViewController 传递给的prepareForSegue .

下一步,在您的 DelegateVC.m 中,获取您的NSString对象的值并将其传递给您的 UITextField .

让我知道这是否有帮助:)

关于ios - 将数据从委托(delegate) View 发送到委托(delegate) View [Objective-C],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46922927/

相关文章:

objective-c - 如何将自定义 NSView 添加到使用 Interface Builder 构建的 UI?

ios - 在ios中对textField设置多重验证

iphone - 如何: Save order of tabs when customizing tabs in UITabBarController

iOS - 通过 segue 发送发送者链接变量

javascript - 如何使用 Javascript 从 Chrome iOS 下载 blob 文件?

ios - react-native 添加新字体在 IOS 模拟器上给出 'Unrecognized font family'

javascript - 将数组传递给 Flurry 的 setAge (Cordova)

ios - 我是否需要在 AFNetworking Reachability block 中引用 self with weak

ios - 在 UITabBarController View 之间传递数据

javascript - 让 div 在 ios safari 上跟随手指