Reputation: 29767
I have a parent view controller with a single method I want available to all child classes:
#import "GAViewController.h"
@interface GAViewController ()<UITextFieldDelegate>
@end
@implementation GAViewController
#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
@end
I have a register view controller which looks like this:
//.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
Here is the error I receive:
Does anyone know I can either fix the error above? Or create a parent class correctly with the textFieldShoudlReturn
method so that I don't have to include in all my views.
Thanks!
Upvotes: 1
Views: 187
Reputation: 726489
I am importing .m because it contains the textfieldshouldreturn method, it also imports the .h file
It is importing .m file that causes duplicate symbols. Importing a .m file causes the file from which it is imported to define the same symbols (such as method implementations and functions / variables with external scope) as the .m file being included, causing the duplication.
For the same reason one should never place @implementation
blocks into header files.
In order to fix this, make a header for GAViewController
, and declare textFieldShouldReturn:
inside it. Remove #import "GAViewController.m"
from your headers and .m files, replacing with #import "GAViewController.h"
. This should fix the problem.
Upvotes: 3