使用故事板
从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
做什么你想从该文本框中重用?如果您只是简单地使用文本值,只需将其作为参数从BaseViewController传递给DerivedViewController即可。否则,您需要继承UITextField以在两个视图控制器上使用。
不,我想使用文本框,而不仅仅是文本框的内容。 – Ganesh
是,u可以使用在ViewController
添加相同的文本框DerivedViewController
,为这款U增加一个textbox
在DerivedViewController's
现场,并连接IBOutlet
到ViewController 's
textbox
无需在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
你的回答很有意义。你说过“将IBOutlet连接到ViewController的文本框”。你能否更详细地解释一下如何实现它。 当我点击CMD并拖动,它不会显示像你说的任何选项 – Ganesh
@Ganesh看到编辑 –
你只想访问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。 –