3

私はまだ学んでいて理解できないので、これは主に委任に関する質問です。必要なデリゲートを作成する方法がわかりません。

同様の質問が寄せられていることは知っていますが、解決策は役に立ちません。ビュー 1 のラベルのテキストをビュー 2 の UITextField の内容に切り替えるにはどうすればよいですか?

ありがとう!

4

1 に答える 1

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;
}
于 2012-08-04T11:03:46.867 に答える