使用Storyboard从BaseViewController类继承按钮

时间:2016-06-21 06:21:26

标签: ios objective-c inheritance

我有两个班级

  1. ViewController(BaseClass)2。DerivedViewController(派生类)
  2. 我在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
    

2 个答案:

答案 0 :(得分:1)

是的,您可以使用ViewController DerivedViewController中添加的相同文本框,为此,请在textbox场景中再添加一个DerivedViewController's并连接{ {1}} IBOutlet ViewController 's无需textboxtextboxDerivedViewController创建新的来源,您可以继承textbook

编辑1:

如果你是ViewController的子类,DerivedViewController子类中的所有插座连接都可用。例如,在DerivedViewController中只需从files owner拖动到您的视图组件,它就会显示超级视图(ViewController&#39; s)出口名称

编辑2: 你正在声明私有属性,所以通过在.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

<强> ViewController.m

#import "ViewController.h"

@interface ViewController ()
@end

@implementation ViewController

int resultValue=0;

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

//other methods
@end

答案 1 :(得分:0)

您想从该文本框中重用什么?如果您只是想使用文本值,只需将其作为参数从BaseViewController传递给DerivedViewController。否则,您需要将UITextField子类化以在两个视图控制器上使用。