【发布时间】:2014-02-26 11:31:49
【问题描述】:
我知道有很多关于在不同视图控制器之间传递消息的问题。我都检查过了,但我无法正常工作。
我已按照本教程进行操作:http://www.youtube.com/watch?v=XZWT0IV8FrI 用导航控制器替换情节提要,但我一遍又一遍地遇到以下问题:“找不到...的协议声明”
代码如下:
FirstViewController.h
#import "SecondViewController.h"
@interface FirstViewController : UIViewController <SecondViewControllerDelegate>{
//In this line above is where I get the error 'Cannot find protocol declaration for SecondViewControllerDelegate'
IBOutlet UITextField *userNameTextField;
}
@property (nonatomic, strong) UITextField *userNameTextField;
-(IBAction)goNext:(id)sender;
@end
FirstViewController.m
#import "FirstViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
@synthesize userNameTextField;
-(void)goNext:(id)sender{
SecondViewController *secondVC = [[SecondViewController alloc]init];
secondVC.delegate = self;
[self.navigationController pushViewController:secondVC animated:YES];
}
-(void)done:(NSString*)name{
NSLog(@"BACK in firstVC");
userNameTextField.text = name;
}
@end
SecondViewController.h
#import "FirstViewController.h"
@protocol SecondViewControllerDelegate <NSObject>
-(void)done:(NSString*)someText;
@end
@interface SecondViewController : UIViewController{
IBOutlet UITextField *someText;
IBOutlet UIButton *returnButton;
id delegate;
}
@property (assign, nonatomic) id <SecondViewControllerDelegate> delegate;
@property (strong, nonatomic) UITextField *someText;
-(IBAction)goBack:(id)sender;
@end
SecondViewController.m
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
@synthesize someText;
@synthesize delegate = _delegate;
-(void)goBack:(id)sender{
[self.delegate done:someText.text];
FirstViewController *firstVC = [[FirstViewController alloc]init];
[self.navigationController pushViewController:firstVC animated:YES];
}
@end
【问题讨论】:
标签: ios objective-c uiviewcontroller delegates protocols