从不同的视图更改标签
这主要是一个代表团的问题,因为我还在学习,不明白。 我不知道如何去创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; }
- 如何使用委托将数据从自定义单元格传递到父视图中的标签
- 从UIPopover发送委托消息到主UIViewController
- iOS Multipeer连接框架invitationHandler似乎不接受?
- UITableView委托在NSURL委托之后没有被调用
- 在“更多”部分中,didSelectViewController不会被调用
- 未声明的委托,并应startStartLoadWithRequest
- iOS:使用UIView的“drawRect:”与其图层的delagate“drawLayer:inContext:”
- 使用KVO与NSNotificationCenter观察对可变arrays的更改
- 将在Interface Builder中创build的UITabBarController设置为委托