So I am trying to learn how to send data from one view controller to another.
所以我试图学习如何将数据从一个视图控制器发送到另一个视图控制器。
My code thus far takes the data entered from one VC (DelegateVC
) and displays it in a label on another VC(ViewController
).
到目前为止,我的代码从一个VC(DelegateVC)输入数据并将其显示在另一个VC(ViewController)的标签中。
Here the delegate is ViewController
and the delegated is DelegateVC
.
这里的委托是ViewController,委托的是DelegateVC。
===========================================================================
Main Question
But what if I want to pass the data of the label from ViewController
to DelegateVC
? How can I accomplish this? I tried engineering the same concept of delegates but with the DelegateVC
as the delegate of ViewController
. Not sure if this is the write approach.
但是如果我想将标签的数据从ViewController传递给DelegateVC怎么办?我怎么能做到这一点?我尝试使用相同的委托概念,但使用DelegateVC作为ViewController的委托。不确定这是否是写入方法。
TL;DR:
In VC
, if I hit Next View
, I go to DelegateVC
and enter "test", it will show up in VC's receiving label
as "test".
在VC中,如果我点击Next View,我会转到DelegateVC并输入“test”,它将在VC的接收标签中显示为“test”。
Now the next time I hit Next View
, I want the existing label's value to show up in the TextField
of DelegateVC
. Instead of a blank TextField
, there will be the value "test".
现在,下次我点击Next View时,我希望现有标签的值显示在DelegateVC的TextField中。而不是空白的TextField,将有值“test”。
=========================================================================== Let's say I have a Main.storyboard
like so:
================================================== =========================假设我有一个Main.storyboard,如下所示:
I have a ViewController.h
file as:
我有一个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
3 个解决方案
#1
1
A delegate allows one object to send messages to another object when an event happens.
委托允许一个对象在事件发生时将消息发送到另一个对象。
In your case to pass data of label from ViewController
to DelegateVC
, you do not need to use delegate. But since you want to update receivingLabel
when changing happens on sendDataTF
textfield , in this case you need a delegate and I think you have done it well.
在您的情况下,要将标签的数据从ViewController传递到DelegateVC,您不需要使用委托。但是,因为你想在sendDataTF文本字段上进行更改时更新receiveLabel,在这种情况下你需要一个委托,我认为你做得很好。
Solution: You need to declare a property in DelegateVC
to store passing string from ViewController
and set that text in sendDataTF
on viewDidLoad
.
解决方案:您需要在DelegateVC中声明一个属性以存储来自ViewController的传递字符串,并在viewDidLoad上的sendDataTF中设置该文本。
Declare a property in DelegateVC.h:
在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
Pass receivingLabel text in ViewController.m:
在ViewController.m中传递receiveLabel文本:
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
DelegatedVC *secondVC = segue.destinationViewController;
secondVC.delegate = self;
secondVC.labelString = self.receivingLabel.text;
}
Set labelString in DelegateVC.m:
在DelegateVC.m中设置labelString:
- (void)viewDidLoad {
[super viewDidLoad];
self.sendDataTF.text = self.labelString;
}
Hope it will work.
希望它会奏效。
#2
3
But what if I want to pass the data of the label from ViewController to DelegateVC? How can I accomplish this?
但是如果我想将标签的数据从ViewController传递给DelegateVC怎么办?我怎么能做到这一点?
So you're doing the delegation right as I can see in your code. Back to your main question, you just need to declare a public NSString
object in your DelegateVC.h
, and then access that and pass the data to that from your ViewController
's prepareForSegue
.
所以你正在我的代码中看到正确的代表团。回到主要问题,您只需要在DelegateVC.h中声明一个公共NSString对象,然后访问它并将数据传递给ViewController的prepareForSegue中的数据。
Next step, in your DelegateVC.m
, get your NSString
object's value and pass it to your UITextField
.
下一步,在DelegateVC.m中,获取NSString对象的值并将其传递给UITextField。
Let me know if this helps :)
如果这有帮助,请告诉我:)
#3
1
Here is proper working code you can have a look at :
以下是您可以查看的正确工作代码:
DelegateVc.h File with Label and button
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
DelegateVC.m文件使用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
DelagatedVc.h与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
DelagatedVc.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;
}
}
My storyBoard Outlook
我的故事董事会展望
#1
1
A delegate allows one object to send messages to another object when an event happens.
委托允许一个对象在事件发生时将消息发送到另一个对象。
In your case to pass data of label from ViewController
to DelegateVC
, you do not need to use delegate. But since you want to update receivingLabel
when changing happens on sendDataTF
textfield , in this case you need a delegate and I think you have done it well.
在您的情况下,要将标签的数据从ViewController传递到DelegateVC,您不需要使用委托。但是,因为你想在sendDataTF文本字段上进行更改时更新receiveLabel,在这种情况下你需要一个委托,我认为你做得很好。
Solution: You need to declare a property in DelegateVC
to store passing string from ViewController
and set that text in sendDataTF
on viewDidLoad
.
解决方案:您需要在DelegateVC中声明一个属性以存储来自ViewController的传递字符串,并在viewDidLoad上的sendDataTF中设置该文本。
Declare a property in DelegateVC.h:
在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
Pass receivingLabel text in ViewController.m:
在ViewController.m中传递receiveLabel文本:
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
DelegatedVC *secondVC = segue.destinationViewController;
secondVC.delegate = self;
secondVC.labelString = self.receivingLabel.text;
}
Set labelString in DelegateVC.m:
在DelegateVC.m中设置labelString:
- (void)viewDidLoad {
[super viewDidLoad];
self.sendDataTF.text = self.labelString;
}
Hope it will work.
希望它会奏效。
#2
3
But what if I want to pass the data of the label from ViewController to DelegateVC? How can I accomplish this?
但是如果我想将标签的数据从ViewController传递给DelegateVC怎么办?我怎么能做到这一点?
So you're doing the delegation right as I can see in your code. Back to your main question, you just need to declare a public NSString
object in your DelegateVC.h
, and then access that and pass the data to that from your ViewController
's prepareForSegue
.
所以你正在我的代码中看到正确的代表团。回到主要问题,您只需要在DelegateVC.h中声明一个公共NSString对象,然后访问它并将数据传递给ViewController的prepareForSegue中的数据。
Next step, in your DelegateVC.m
, get your NSString
object's value and pass it to your UITextField
.
下一步,在DelegateVC.m中,获取NSString对象的值并将其传递给UITextField。
Let me know if this helps :)
如果这有帮助,请告诉我:)
#3
1
Here is proper working code you can have a look at :
以下是您可以查看的正确工作代码:
DelegateVc.h File with Label and button
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
DelegateVC.m文件使用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
DelagatedVc.h与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
DelagatedVc.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;
}
}
My storyBoard Outlook
我的故事董事会展望