从不同的视图更改标签 [英] Change label from a different view
问题描述
这主要是一个代表团的问题,因为我还在学习,没有得到它。我不知道如何创建我需要的代理人。
This is mainly a delegation question because I'm still learning and don't get it. I don't know how to go about creating the delegate I need.
我知道类似的问题已经被问到,但解决方案不能帮助我。如何在View 1中使用UITextField的内容切换标签的文本?
I know similar questions have been asked but the solutions don't help me out. How can I switch the text of a label on View 1 with the contents of a UITextField from View 2?
谢谢!
推荐答案
在此代码片段中,ChildViewController是您的View2,ParentViewController是您的View1。
In this code snippet, ChildViewController is your View2 and ParentViewController is your View1.
在您的ChildViewController.h文件中:
In your ChildViewController.h file:
@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文件中:
In your ChildViewController.m file:
@implementation ChildViewController
@synthesize delegate;
// Some where in your ChildViewController.m file
// to call parent method:
// [self.delegate parentMethodThatChildCanCall:myTextField.text];
在ParentViewController.h文件中:
In ParentViewController.h file:
@interface parentViewController <ChildViewControllerDelegate>
{
@property (strong, nonatomic) IBOutlet UILabel *myLabel;
}
在ParentViewController.m文件中:
In ParentViewController.m file:
//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;
}
这篇关于从不同的视图更改标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!