因此,我正在尝试学习如何将数据从一个视图控制器发送到另一个。
到目前为止,我的代码接收从一个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
的DelegateVC
中。将使用值“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
最佳答案
委托允许一个对象在事件发生时向另一个对象发送消息。
在您将标签数据从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中传递接收标签文本:
-(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;
}
希望它能工作。
关于ios - 从委托(delegate) View 向委托(delegate) View 发送数据[Objective-C],我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46922927/