ios uiviewcontroller继承引发重复符号错误
我有一个父视图控制器,它有一个我希望所有子类都可用的方法:ios uiviewcontroller继承引发重复符号错误,ios,objective-c,cocoa-touch,inheritance,Ios,Objective C,Cocoa Touch,Inheritance,我有一个父视图控制器,它有一个我希望所有子类都可用的方法: #import "GAViewController.h" @interface GAViewController ()<UITextFieldDelegate> @end @implementation GAViewController #pragma mark - UITextFieldDelegate - (BOOL)textFieldShouldReturn:(UITextField *)textField
#import "GAViewController.h"
@interface GAViewController ()<UITextFieldDelegate>
@end
@implementation GAViewController
#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
@end
#导入“GAViewController.h”
@接口GAViewController()
@结束
@实现GAViewController
#pragma标记-UITextFieldDelegate
-(BOOL)textField应返回:(UITextField*)textField
{
[textField resignFirstResponder];
返回YES;
}
@结束
我有一个寄存器视图控制器,如下所示:
//.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
/.h
#进口
#导入“GAViewController.h”
#导入“GAViewController.m”
@接口GARegisterViewController:GAViewController
@结束
//m
#导入“GARegisterViewController.h”
@接口GARegisterViewController()
@属性(弱,非原子)IBUIButton*registerButton;
@属性(弱,非原子)IBOutlet UITextField*userNameTextField;
@属性(弱,非原子)IBOutlet UITextField*passwordTextField;
@属性(弱,非原子)IBOutlet UITextField*firstNameTextField;
@属性(弱,非原子)IBOutlet UITextField*lastNameTextField;
@属性(弱,非原子)IBOutlet UITextField*phoneNumberTextField;
@属性(弱、非原子)IBUISegmentedControl*GenderSegmentedControl;
@结束
@实现GARegisterViewController
-(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
如果(自我){
//自定义初始化
}
回归自我;
}
-(无效)viewDidLoad
{
[超级视图下载];
[自制文本字段代理];
}
-(void)makeTextFieldDelegates{
[self.userNameTextField setDelegate:self];
[self.passwordTextField setDelegate:self];
[self.firstNameTextField setDelegate:self];
[self.lastNameTextField setDelegate:self];
[self.phoneNumberTextField setDelegate:self];
}
-(无效)未收到记忆警告
{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
@结束
以下是我收到的错误:
有人知道我可以修复上面的错误吗?或者使用textFieldShoudlReturn
方法正确创建父类,这样我就不必在所有视图中都包含父类
谢谢
我之所以导入.m,是因为它包含textfieldshouldreturn方法,它还导入.h文件 正在导入导致符号重复的.m文件。导入.m文件会导致导入该文件的文件定义与所包含的.m文件相同的符号(例如方法实现和具有外部作用域的函数/变量),从而导致重复 出于同样的原因,永远不要将
@implementation
块放入头文件中
要解决此问题,请为
GAViewController
创建一个标题,并在其内部声明textFieldShouldReturn:
。从头文件和.m文件中删除#导入“GAViewController.m”
,替换为#导入“GAViewController.h”
。这应该可以解决问题。据我所知,我看不到您遇到的错误。我导入.m是因为它包含textfieldshouldreturn方法,它还导入了.h文件。正如dash所说,您不应该导入.m文件以暴露其中定义的符号。相反,将声明添加到头中的方法-(BOOL)textField shouldReturn:(UITextField*)textField
。