所以我试图学习如何将数据从一个视图控制器发送到另一个视图控制器。
到目前为止,我的代码从一个VC(DelegateVC
)输入数据并将其显示在另一个VC(ViewController
)的标签中。
此处代理人是ViewController
,委托人是DelegateVC
。
=============================================== ============================
但是如果我想将标签的数据从ViewController
传递到DelegateVC
怎么办?我怎么能做到这一点?我尝试使用相同的委托概念,但DelegateVC
作为ViewController
的委托。不确定这是否是写入方法。
在VC
中,如果我点击Next View
,我会转到DelegateVC
并输入“test”,它会在VC's receiving label
中显示为“test”。
现在,当我下次Next View
时,我希望现有标签的值显示在TextField
的{{1}}中。而不是空白DelegateVC
,将有值“test”。
=============================================== ============================
假设我有TextField
这样:
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:
答案 0 :(得分:3)
但是如果我想从ViewController传递标签的数据呢? 委托VC?我怎么能做到这一点?
所以你正在我的代码中看到正确的代表团。回到您的主要问题,您只需要在NSString
中声明一个公开的DelegateVC.h
对象,然后访问该对象并将数据传递给ViewController
&#39; { {1}}。
下一步,在prepareForSegue
中,获取DelegateVC.m
对象的值,并将其传递给NSString
。
请告诉我这是否有帮助:)
答案 1 :(得分:1)
委托允许一个对象在事件发生时将消息发送到另一个对象。
如果要将标签数据从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:
中传递receivingLabel文本-(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;
}
希望它能奏效。
答案 2 :(得分: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;
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;
}
}
我的故事董事会