这主要是一个代表团问题,因为我还在学习并且没有得到它。我不知道如何创建我需要的委托。
我知道有类似的问题已被问到,但解决方案并没有帮助我。如何使用View 2中的UITextField内容切换View 1上标签的文本?
谢谢!
答案 0 :(得分:3)
在这段代码片段中,ChildViewController是您的View2,而ParentViewController是您的View1。
在ChildViewController.h文件中:
@protocol ChildViewControllerDelegate <NSObject>
- (void)parentMethodThatChildCanCall:(NSString *)string; //pass back the string value from your textfield
@end
@interface ChildViewController : UIViewController
{
@property (weak, nonatomic) IBOutlet UITextField *myTextField;
}
@property (assign) id <ChildViewControllerDelegate> delegate;
在您的ChildViewController.m文件中:
@implementation ChildViewController
@synthesize delegate;
// Some where in your ChildViewController.m file
// to call parent method:
// [self.delegate parentMethodThatChildCanCall:myTextField.text];
在ParentViewController.h文件中:
@interface parentViewController <ChildViewControllerDelegate>
{
@property (strong, nonatomic) IBOutlet UILabel *myLabel;
}
在ParentViewController.m文件中:
//after create instant of your ChildViewController
childViewController.delegate = self;
- (void) parentMethodThatChildCanCall:(NSString *)string
{
// assign your passed back textfield value to your label here
myLabel.text = string;
}