從BaseViewController類繼承一個按鈕我有兩個類使用故事板
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
你只想訪問TextView的,或者你想顯示ViewControllers屏幕上相同的TextView? –
要在兩個屏幕上使用相同的TextBox(兩個ViewControllers) – Ganesh
如果要在Base和Derived ViewController上顯示相同的textView,則必須在Storyboard的兩個ViewController中添加textView,並創建UITextView的子類並設置所需的屬性設置,並將該類設置爲ViewControllers TextViews Custom Class的自定義類。只是爲了清除事情 - 如果您想在BaseViewControllers上添加TextView,並且繼承ChildViewController中的TextView不會自動顯示TextView,則需要在Storyboard上的兩個ViewController上添加textView。 –