所以我正在尝试学习如何将数据从一个 View Controller 发送到另一个 View Controller 。
到目前为止,我的代码获取从一个 VC (DelegateVC
) 输入的数据,并将其显示在另一个 VC (ViewController
) 的标签中。
这里的委托(delegate)是ViewController
并且委托(delegate)是DelegateVC
.
==================================================== ==========================
主要问题
但是如果我想从 ViewController
传递标签的数据怎么办?至DelegateVC
?我怎样才能做到这一点?我尝试设计相同的委托(delegate)概念,但使用 DelegateVC
作为ViewController
的委托(delegate).不确定这是否是写入方法。
TL;博士:
在 VC
, 如果我点击 Next View
, 我去 DelegateVC
并输入“test”,它将显示在VC's receiving label
作为“测试”。
现在下次我点击 Next View
,我希望现有标签的值显示在 TextField
的 DelegateVC
.而不是空白 TextField
,会有值“test”。
==================================================== ==========================
假设我有一个 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
最佳答案
But what if I want to pass the data of the label from ViewController to DelegateVC? How can I accomplish this?
因此,正如我在您的代码中看到的那样,您正在正确地进行委派。回到你的主要问题,你只需要声明一个公共(public)
NSString
您的 DelegateVC.h
中的对象, 然后访问它并将数据从您的 ViewController
传递给的prepareForSegue
.下一步,在您的
DelegateVC.m
中,获取您的NSString
对象的值并将其传递给您的 UITextField
.让我知道这是否有帮助:)
关于ios - 将数据从委托(delegate) View 发送到委托(delegate) View [Objective-C],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46922927/