2012-08-04 57 views
3

这主要是一个授权问题,因为我仍然在学习,并没有得到它。我不知道如何去创建我需要的委托。从不同的角度更改标签

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

谢谢!

+0

你试过什么? – Jalpesh 2012-08-04 10:00:51

回答

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; 
}