2012-08-04 46 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; 
}