从不同的视图更改标签

这主要是一个代表团的问题,因为我还在学习,不明白。 我不知道如何去创build我需要的委托。

我知道类似的问题已经被问到,但解决scheme并没有帮助我。 如何将视图1上的标签的文本与视图2中的UITextField的内容切换?

谢谢!

在这个代码片段中,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; }