我正在學習目標c並需要幫助來了解委託。與委託不起作用的目標c文本字段
我在FirstViewController中有一個UITextField,我想在SecondViewController中顯示inputed文本。
但它不工作。我究竟做錯了什麼?
這裏是我的代碼:
FirstViewController.h
#import <UIKit/UIKit.h>
@interface FirstViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIButton *accessButton;
@property (weak, nonatomic) IBOutlet UITextField *codeField;
@property (nonatomic, retain) NSString *getCodeString;
@end
SecondViewController.h
#import <UIKit/UIKit.h>
@class CodeField;
@protocol CodeFieldHandlerDelegate <NSObject>
@property NSString *saveCodeString;
@end
@interface SecondViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *candidateLabel;
@property (weak, nonatomic) id <CodeFieldHandlerDelegate> myDelegate;
@end
FirstViewController.m
#import "FirstViewController.h"
#import "SecondViewController.h"
@interface FirstViewController()
<CodeFieldHandlerDelegate>
@property (nonatomic, strong) SecondViewController *secondViewController;
@end
@implementation FirstViewController
@synthesize getCodeString;
@synthesize secondViewController;
@synthesize saveCodeString;
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)accessButton:(UIButton *)sender {
//get the input data from text field and store into string
getCodeString = self.codeField.text;
//go keypad back when button clicked from textfield
[self.codeField resignFirstResponder];
secondViewController = [[SecondViewController alloc] init];
secondViewController.myDelegate = self;
[self.navigationController pushViewController:secondViewController animated:YES];
}
// name of this string is the same of the NSString in the delegate
- (NSString *) saveCodeString {
return getCodeString;
}
@end
而且SecondViewContro ller.m
#import "SecondViewController.h"
@interface SecondViewController()
@end
@implementation SecondViewController
@synthesize candidateLabel;
@synthesize myDelegate;
- (void)viewDidLoad {
[super viewDidLoad];
self.candidateLabel.text = [myDelegate saveCodeString];
}
@end
做一些調試。是否調用了saveCodeString方法? 'getCodeString'設置爲期望值嗎? 'self.candidateLabel'設置爲非零值嗎? – rmaddy