【问题标题】:Sending Data From Delegate View To Delegated View [Objective-C]将数据从委托视图发送到委托视图 [Objective-C]
【发布时间】:2018-04-05 23:35:36
【问题描述】:

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

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

这里的委托是ViewController,委托是DelegateVC

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

主要问题

但是如果我想将标签的数据从ViewController 传递给DelegateVC 怎么办?我怎样才能做到这一点?我尝试设计相同的代表概念,但将DelegateVC 作为ViewController 的代表。不确定这是否是写方法。

TL;DR:

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

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

================================================ ============================== 假设我有一个像这样的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:

#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

【问题讨论】:

    标签: ios objective-c iphone uiviewcontroller delegates


    【解决方案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;
    

    带segues的DelegateVC.m文件

    -(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

    #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
    

    DelegatedVc.m 与 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;
    
    
        }
    }
    

    我的故事板展望

    【讨论】:

    【解决方案2】:

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

    在您将标签数据从ViewController 传递到DelegateVC 的情况下,您不需要使用委托。但是,由于您想在 sendDataTF 文本字段发生更改时更新 receivingLabel,因此在这种情况下,您需要一个委托,我认为您做得很好。

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

    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:

    中传递接收标签文本
    -(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;
    }
    

    希望它会起作用。

    【讨论】:

      【解决方案3】:

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

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

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

      如果这有帮助,请告诉我:)

      【讨论】:

      • 我试过这个但没有用。我所做的是在DelegateVC.h 声明static NSString *globalStr。在ViewControllerprepareForSegue 中,我添加了globalStr = _receivingLabel.text。最后,在DelegateVC.mviewDidLoad 中我添加了_sendDataTF.text = globalStr。想法?谢谢!
      • 这就是我在上面的回答中告诉你的:)
      猜你喜欢
      • 2017-12-11
      • 2021-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-27
      相关资源
      最近更新 更多