【发布时间】:2012-08-04 09:56:49
【问题描述】:
这主要是一个委托问题,因为我还在学习,但不明白。我不知道如何创建我需要的委托。
我知道有人问过类似的问题,但解决方案对我没有帮助。如何将视图 1 上的标签文本与视图 2 中的 UITextField 的内容进行切换?
谢谢!
【问题讨论】:
标签: ios uiviewcontroller delegates uitextfield uilabel
这主要是一个委托问题,因为我还在学习,但不明白。我不知道如何创建我需要的委托。
我知道有人问过类似的问题,但解决方案对我没有帮助。如何将视图 1 上的标签文本与视图 2 中的 UITextField 的内容进行切换?
谢谢!
【问题讨论】:
标签: ios uiviewcontroller delegates uitextfield uilabel
在这段代码 sn-p 中,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;
}
【讨论】: