[英]Sending Data From Delegate View To Delegated View [Objective-C]
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
。
=========================================================================== ================================================== ========================
But what if I want to pass the data of the label from ViewController
to DelegateVC
? 但是,如果我想将标签的数据从
ViewController
传递给DelegateVC
怎么办? How can I accomplish this? 我该怎么做? I tried engineering the same concept of delegates but with the
DelegateVC
as the delegate of ViewController
. 我尝试设计相同概念的委托,但使用
DelegateVC
作为ViewController
的委托。 Not sure if this is the write approach. 不知道这是否是写方法。
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's receiving label
为“ test”。
Now the next time I hit Next View
, I want the existing label's value to show up in the TextField
of DelegateVC
. 现在,下次我单击“
Next View
,我希望现有标签的值显示在DelegateVC
的TextField
中。 Instead of a blank TextField
, there will be the value "test". 将使用值“ test”代替空白的
TextField
。
=========================================================================== 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
: 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?
但是,如果我想将标签的数据从ViewController传递到DelegateVC,该怎么办? How can I accomplish this?
我该怎么做?
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 :) 让我知道这是否有帮助:)
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. 在您将标签数据从
ViewController
传递到DelegateVC
,您无需使用委托。 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. 但是由于由于要在
sendDataTF
文本字段发生更改时更新receivingLabel
sendDataTF
,因此在这种情况下,您需要一个委托,我认为您做得很好。
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中传递接收标签文本:
-(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. 希望它能工作。
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 带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 with textfield 带有文本字段的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
DelagatedVc.m with Segue 与Segue的DelagatedVc.m
-(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 我的故事董事会展望
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.