2016-06-21 171 views
-1

從BaseViewController類繼承一個按鈕我有兩個類使用故事板

  1. ViewController(BaseClass的)2. DerivedViewController(派生類)

我有ViewController類的文本框。通過繼承可以在DerivedViewController類中使用該文本框嗎?

如果是,如何繼承該文本框?

我試着繼承下面的代碼,我通過導入ViewController.h成功繼承,如下所示。

#import <UIKit/UIKit.h> 
#import "ViewController.h> 

@interface DerivedViewController : ViewController 

@end 

現在我的問題是,如何使用textField在此DerivedViewController。我正試圖實現可重用性的繼承概念。當我使用故事板時,如何獲得textField並將其置於故事板中的此ViewController框中非常混亂。

感謝您的時間:)

ViewController.m

#import "ViewController.h" 

@interface ViewController() 
@property (weak, nonatomic) IBOutlet UITextField *firstTB; 
@property (weak, nonatomic) IBOutlet UITextField *secondTB; 
@property (weak, nonatomic) IBOutlet UILabel *symbolLabel; 
@property (weak, nonatomic) IBOutlet UITextField *find; 


@end 

@implementation ViewController 

int resultValue=0; 



- (IBAction)saveResult:(id)sender { 
    if((self.firstTB.text.length == 0) || self.secondTB.text.length == 0){ 
     [self showError]; 
     NSLog(@"Error"); 
    }else{ 
     int number1 = [[[self firstTB] text] intValue]; 
     int number2 = [[[self secondTB] text] intValue]; 
     resultValue=number1+number2; 
     NSLog(@"%d",resultValue); 
     [self saveData]; 
     [self removeShadow]; 
    } 

} 
- (IBAction)listResults:(id)sender { 
    // [self findData]; 
} 



//Save Data......... 

-(void) saveData{ 
    BOOL success = NO; 
    NSString *alertString = @"Data Insertion failed"; 
    success = [[DBManager getSharedInstance]saveData: 
       resultValue]; 


    if (success == NO) { 
     UIAlertView *alert = [[UIAlertView alloc]initWithTitle: 
           alertString message:nil 
                 delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
     [alert show]; 
    } 
    if(success == YES){ 
     NSLog(@"FY %d",resultValue); 
    } 
} 

    //Find data....... 
-(void) findData { 

    int n = [[DBManager getSharedInstance]findResult:[[[self find] text] intValue]]; 
    if (n == 0) { 
     UIAlertView *alert = [[UIAlertView alloc]initWithTitle: 
           @"Data not found" message:nil delegate:nil cancelButtonTitle: 
           @"OK" otherButtonTitles:nil]; 
     [alert show]; 

    } 
    else{ 
     /* regNoTextField.text = findByRegisterNumberTextField.text; 
     nameTextField.text =[data objectAtIndex:0]; 
     departmentTextField.text = [data objectAtIndex:1]; 
     yearTextField.text =[data objectAtIndex:2]; */ 
     NSLog(@"Data Found %d",n); 

    } 
} 




// For Shadow..... 

-(void)showError{ 
    [self addShadow]; 
} 


-(void)addShadow{ 
    if(self.firstTB.text.length == 0 && self.secondTB.text.length == 0){ 

     [self glowfirstTB:YES]; 

     [self glowsecondTB:YES]; 

    }else if(self.firstTB.text.length == 0){ 

     [self glowfirstTB:YES]; 

     [self glowsecondTB:NO]; 

    }else if(self.secondTB.text.length == 0){ 

     [self glowsecondTB:YES]; 

     [self glowfirstTB:NO]; 
    } 


} 

-(void)removeShadow{ 
    [self glowfirstTB:NO]; 
    [self glowsecondTB:NO]; 
} 

-(void)glowfirstTB:(BOOL) shall_I_Glow{ 
    if(shall_I_Glow){ 
     self.firstTB.layer.masksToBounds = NO; 
     self.firstTB.layer.shadowColor = [[UIColor blueColor] CGColor]; 
     self.firstTB.layer.shadowOffset = CGSizeZero; 
     self.firstTB.layer.shadowRadius = 10.0f; 
     self.firstTB.layer.shadowOpacity = 1.0; 
    }else{ 
     self.firstTB.layer.shadowRadius = 0.0f; 
     self.firstTB.layer.shadowOpacity = 0.0; 
    } 
} 


-(void)glowsecondTB:(BOOL) shall_I_Glow{ 
    if(shall_I_Glow){ 
     self.secondTB.layer.masksToBounds = NO; 
     self.secondTB.layer.shadowColor = [[UIColor blueColor] CGColor]; 
     self.secondTB.layer.shadowOffset = CGSizeZero; 
     self.secondTB.layer.shadowRadius = 10.0f; 
     self.secondTB.layer.shadowOpacity = 1.0; 

     // [self.num setBorderStyle:UITextBorderStyleRoundedRect]; 
     // self.passwordTextField.layer.cornerRadius = 15.0; 
    }else{ 
     self.secondTB.layer.shadowRadius = 0.0f; 
     self.secondTB.layer.shadowOpacity = 0.0; 
    } 
} 


- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end 

DerivedViewController.m(這是命名爲SearchPage.h這裏)

#import "SearchPage.h" 

@interface SearchPage() 

@property (weak, nonatomic) IBOutlet UITextField *searchText; 

@end 

@implementation SearchPage 

- (IBAction)findButton:(id)sender { 
    self.firstTB.text = @"hello"; 
    //Error message : Property 'firstTB' not found on object of type "SearchPage" 
} 


- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

/* 
#pragma mark - Navigation 

// In a storyboard-based application, you will often want to do a little preparation before navigation 
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    // Get the new view controller using [segue destinationViewController]. 
    // Pass the selected object to the new view controller. 
} 
*/ 

@end 
+0

你只想訪問TextView的,或者你想顯示ViewControllers屏幕上相同的TextView? –

+0

要在兩個屏幕上使用相同的TextBox(兩個ViewControllers) – Ganesh

+0

如果要在Base和Derived ViewController上顯示相同的textView,則必須在Storyboard的兩個ViewController中添加textView,並創建UITextView的子類並設置所需的屬性設置,並將該類設置爲ViewControllers TextViews Custom Class的自定義類。只是爲了清除事情 - 如果您想在BaseViewControllers上添加TextView,並且繼承ChildViewController中的TextView不會自動顯示TextView,則需要在Storyboard上的兩個ViewController上添加textView。 –

回答

0

做什麼你想從該文本框中重用?如果您只是簡單地使用文本值,只需將其作爲參數從BaseViewController傳遞給DerivedViewController即可。否則,您需要繼承UITextField以在兩個視圖控制器上使用。

+0

不,我想使用文本框,而不僅僅是文本框的內容。 – Ganesh

1

是,u可以使用在ViewController添加相同的文本框DerivedViewController,爲這款U增加一個textboxDerivedViewController's現場,並連接IBOutletViewController 'stextbox無需在DerivedViewController創建一個新的出路textbox這樣你可以繼承textbook

編輯1:

如果u子類ViewController所有的出口連接在DerivedViewController子類是可用的。例如,在DerivedViewController剛剛從files owner拖動到您的視圖組件將

編輯2顯示了上海華公司(視圖控制器的)的出口名稱: u的聲明私人性質,因此使其可用於其他類也由.h聲明文件,

ViewController.h

#import <UIKit/UIKit.h> 
#import "ViewController.h> 

@interface DerivedViewController : ViewController 
@property (weak, nonatomic) IBOutlet UITextField *firstTB; 
@property (weak, nonatomic) IBOutlet UITextField *secondTB; 
@property (weak, nonatomic) IBOutlet UILabel *symbolLabel; 
@property (weak, nonatomic) IBOutlet UITextField *find; 
@end 

視圖控制器。米

#import "ViewController.h" 

@interface ViewController() 
@end 

@implementation ViewController 

int resultValue=0; 

- (IBAction)saveResult:(id)sender { 
    //other codes 
} 

//other methods 
@end 
+0

你的回答很有意義。你說過「將IBOutlet連接到ViewController的文本框」。你能否更詳細地解釋一下如何實現它。 當我點擊CMD並拖動,它不會顯示像你說的任何選項 – Ganesh

+0

@Ganesh看到編輯 –