私はiPhone開発の初心者であり、いくつかの基本的な質問がprotocols
ありdelegates
ます。私は2つのビューコントローラーを持っています:ビューコントローラーとviewcontroller2nd。そのうちの1つにUITextFieldがあり、そこに何か(名前など)を入力したいと思います。viewcontroller2ndにUILabelがあり、UITextFieldが変更されたときに名前をHelloとして表示したいと思います。
私はこのビデオをフォローしています:http ://www.youtube.com/watch?v = odk- rr_mzUoは、基本的なデリゲートを単一のビューコントローラーで動作させるためのものです。
私はこれを実装するためにプロトコルを使用しています:
SampleDelegate.h
#import <Foundation/Foundation.h>
@protocol ProcessDelegate <UITextFieldDelegate>
@optional
- (BOOL)textFieldShouldReturn:(UITextField *)textField;
@end
@interface SampleDelegate : NSObject
{
id <ProcessDelegate> delegate;
}
@property (retain) id delegate;
@end
SampleDelegate.m
#import "SampleDelegate.h"
@implementation SampleDelegate
@synthesize delegate;
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
lbl.text = [NSString stringWithFormat:@"Hello, %@",txtField.text];
[txtField resignFirstResponder];
}
@end
ViewController.h
#import <UIKit/UIKit.h>
#import "SampleDelegate.h"
@interface ViewController : UIViewController <ProcessDelegate>
{
IBOutlet UITextField *txtField;
}
@end
Viewcontroller.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
ViewController2nd.h
#import <UIKit/UIKit.h>
@interface ViewController2nd : UIViewController <ProcessDelegate> {
IBOutlet UILabel *lbl;
}
@end
ViewController2nd.mはXcodeの標準コードです。
私の質問は、デリゲート関数をviewcontrollerとviewcontroller2ndにリンクして、それを機能させるにはどうすればよいですか?
質問がばかげているなら、私を許してください。いくつかのガイダンスが必要です。私がしている他の間違いを私に指摘してください..ありがとう..