我有一个父视图控制器,它有一个我希望所有子类可用的方法:
#import "GAViewController.h"
@interface GAViewController ()<UITextFieldDelegate>
@end
@implementation GAViewController
#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
@end
我有一个寄存器视图控制器,如下所示:
//.h
#import <UIKit/UIKit.h>
#import "GAViewController.h"
#import "GAViewController.m"
@interface GARegisterViewController : GAViewController
@end
//.m
#import "GARegisterViewController.h"
@interface GARegisterViewController ()<UIActionSheetDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UIButton *registerButton;
@property (weak, nonatomic) IBOutlet UITextField *userNameTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@property (weak, nonatomic) IBOutlet UITextField *firstNameTextField;
@property (weak, nonatomic) IBOutlet UITextField *lastNameTextField;
@property (weak, nonatomic) IBOutlet UITextField *phoneNumberTextField;
@property (weak, nonatomic) IBOutlet UISegmentedControl *genderSegmentedContoller;
@end
@implementation GARegisterViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self makeTextFieldDelegates];
}
- (void)makeTextFieldDelegates{
[self.userNameTextField setDelegate:self];
[self.passwordTextField setDelegate:self];
[self.firstNameTextField setDelegate:self];
[self.lastNameTextField setDelegate:self];
[self.phoneNumberTextField setDelegate:self];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
以下是我收到的错误:
有谁知道我可以解决上面的错误吗?或者使用textFieldShoudlReturn
方法正确创建父类,这样我就不必包含在我的所有视图中。
谢谢!
答案 0 :(得分:3)
我正在导入.m,因为它包含textfieldshouldreturn方法,它还会导入.h文件
导入.m文件会导致重复的符号。导入.m文件会导致导入它的文件定义相同的符号(例如方法实现和带外部作用域的函数/变量)作为包含的.m文件,从而导致重复。
出于同样的原因,不应该将@implementation
块放入头文件中。
要解决此问题,请为GAViewController
制作标题,并在其中声明textFieldShouldReturn:
。从标头和.m文件中删除#import "GAViewController.m"
,替换为#import "GAViewController.h"
。这应该可以解决问题。