2016-06-21 155 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看到编辑 –