2014-06-24 25 views
1

我有所有的子類的單一方法我想提供一個父視圖控制器:IOS的​​UIViewController繼承引發的重複符號錯誤

#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 

這是我收到的錯誤: enter image description here enter image description here

有誰知道我可以修復上面的錯誤?或者使用textFieldShoudlReturn方法正確創建一個父類,以便我不必在所有視圖中都包含該類。

謝謝!

+1

據我所見,我看不到你遇到的錯誤。 –

+0

添加圖像到通信 – Atma

+0

我導入.m,因爲它包含textfieldshouldreturn方法,它也導入.h文件 – Atma

回答

3

我進口的.m,因爲它包含了textfieldshouldreturn方法,它也進口.h文件

它是進口,導致重複的符號.m文件。導入.m文件會導致導入它的文件將包含的.m文件定義爲相同的符號(例如方法實現和具有外部範圍的函數/變量),從而導致重複。

出於同樣的原因,不應該將@implementation塊放到頭文件中。

爲了解決這個問題,請爲GAViewController製作標題,並在其中聲明textFieldShouldReturn:。從頭文件和.m文件中刪除#import "GAViewController.m",替換爲#import "GAViewController.h"。這應該解決這個問題。

+1

正如破折號所說,您不應該導入.m文件以顯示在此定義的符號。相反,向頭中的方法' - (BOOL)textFieldShouldReturn:(UITextField *)textField'添加一個聲明。 –