将代理视图中的数据发送到委派视图[Objective-C]

时间:2017-10-25 02:23:49

标签: ios objective-c iphone uiviewcontroller delegates

所以我试图学习如何将数据从一个视图控制器发送到另一个视图控制器。

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

此处代理人是ViewController,委托人是DelegateVC

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

主要问题

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

TL; DR:

VC中,如果我点击Next View,我会转到DelegateVC并输入“test”,它会在VC's receiving label中显示为“test”。

现在,当我下次Next View时,我希望现有标签的值显示在TextField的{​​{1}}中。而不是空白DelegateVC,将有值“test”。

=============================================== ============================ 假设我有TextField这样:

description 我有一个Main.storyboard文件:

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:

3 个答案:

答案 0 :(得分:3)

  

但是如果我想从ViewController传递标签的数据呢?   委托VC?我怎么能做到这一点?

所以你正在我的代码中看到正确的代表团。回到您的主要问题,您只需要在NSString中声明一个公开的DelegateVC.h对象,然后访问该对象并将数据传递给ViewController&#39; { {1}}。

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

请告诉我这是否有帮助:)

答案 1 :(得分:1)

委托允许一个对象在事件发生时将消息发送到另一个对象。

如果要将标签数据从ViewController传递到DelegateVC,则无需使用委托。但是,由于您要在receivingLabel文本字段进行更改时更新sendDataTF,因此在这种情况下您需要一名代表,我认为您已做得很好。

解决方案:您需要在DelegateVC中声明一个属性,以存储来自ViewController的传递字符串,并在sendDataTF viewDidLoad上设置该文字

DelegateVC.h中声明属性:

@interface DelegatedVC : UIViewController

@property (weak, nonatomic) id<MainVCDelegate> delegate;
@property (strong, nonatomic) NSString *labelString;

@property (strong, nonatomic) IBOutlet UITextField *sendDataTF;

- (IBAction)sendData:(id)sender;

@end

ViewController.m:

中传递receivingLabel文本
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender           
{
   DelegatedVC *secondVC = segue.destinationViewController;
   secondVC.delegate = self;
   secondVC.labelString = self.receivingLabel.text;
}

DelegateVC.m:

中设置labelString
- (void)viewDidLoad {
  [super viewDidLoad];
  self.sendDataTF.text = self.labelString;
}

希望它能奏效。

答案 2 :(得分:1)

以下是您可以查看的正确工作代码:

带标签和按钮的DelegateVc.h文件

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

@interface DelegateVC : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *ReceiveLabel;
@property (weak, nonatomic) IBOutlet UIButton *NextView;
@property (strong, nonatomic) NSString *TextFeildText;

DelegateVC.m File with segues

-(void)viewWillAppear:(BOOL)animated{
    _ReceiveLabel.text = _TextFeildText;
}
- (IBAction)NextViewBtnAction:(id)sender {
    [self performSegueWithIdentifier:@"ToDelegated" sender:sender];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Make sure your segue name in storyboard is the same as this line
    if ([[segue identifier] isEqualToString:@"ToDelegated"])
    {
        // Get reference to the destination view controller
        DelegatedVC *vc = [segue destinationViewController];
        vc.LabelText = _ReceiveLabel.text;

        // Pass any objects to the view controller here, like...

    }
}

DelagatedVc.h with textfield

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

@interface DelegatedVC : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *RCTextField;
@property (weak, nonatomic) IBOutlet UIButton *SenddataBtn;
@property (strong, nonatomic) NSString *LabelText;
@end

DelagatedVc.m with Segue

-(void)viewWillAppear:(BOOL)animated{
    _RCTextField.text = _LabelText;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)SendDataBtnAction:(id)sender {
    [self performSegueWithIdentifier:@"ToDelegate" sender:sender];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Make sure your segue name in storyboard is the same as this line
    if ([[segue identifier] isEqualToString:@"ToDelegate"])
    {
        // Get reference to the destination view controller
        DelegateVC *vc = [segue destinationViewController];
        // Pass any objects to the view controller here, like...
        vc.TextFeildText = _RCTextField.text;


    }
}

我的故事董事会

enter image description here