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
).
Here the delegate is ViewController
and the delegated is DelegateVC
.
===========================================================================
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.
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".
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".
=========================================================================== Let's say I have a Main.storyboard
like so:
I have a
ViewController.h
file as:
#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
But what if I want to pass the data of the label from ViewController to 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
.
Next step, in your DelegateVC.m
, get your NSString
object's value and pass it to your 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. 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.
Solution : You need to declare a property in DelegateVC
to store passing string from ViewController
and set that text in sendDataTF
on viewDidLoad
.
Declare a property in 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:
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
DelegatedVC *secondVC = segue.destinationViewController;
secondVC.delegate = self;
secondVC.labelString = self.receivingLabel.text;
}
Set labelString in DelegateVC.m:
- (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
#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;
}
}
My storyBoard Outlook
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.