Apologies for this really basic question, but in my books and online tutorials I cannot find an example of how to subclass a custom view controller - what exactly has to be written down and what we can get from inheritance. Based on experience in other languages, I thought I'd get everything from the parent class in my subclassed UIViewController without having to re-code it. I thought I could just modify the functions I wanted to modify, but this appears not to be the case. Description of what I tried below:
Already I have a customer UIViewController
called SignupViewController. Now I want to add a view where the user can update her info, and I realized it would basically be a remake of SignupViewController except with a few UITextFields
hidden and all UITextFields
prepopulated with existing information. I thought I could subclass the way I would subclass a normal UIViewController
, but I find that when I try to override methods in SignupViewController in the new subclass of SignupViewController called UpdateViewController, I am told that the properties are not found. It almost feels as though I am working with a blank file and that the properties of SignupViewcontroller have not been referenced. For example, I'm trying to cull back the fields so I'm modifying this code in UpdateViewController:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
CGFloat lineHeight = .05*_height;
if (textField == self.firstName) {
[textField resignFirstResponder];
[self.lastName becomeFirstResponder];
} else if (textField == self.lastName) {
[textField resignFirstResponder];
[self.emailAddress becomeFirstResponder];
[self.scrollView setContentOffset: CGPointMake(0, lineHeight)animated:YES];
} ...etc...
}
return YES;
}
However I am getting errors that UpdateViewController (inheriting from SignupViewController) has no scrollView, emailAddress, firstName, lastName, etc property. Do I have to redeclare all these properties/write everything out? If so, what does subclassing really mean?
Here's what the .h and .m files of the subclass of the SignupViewController look like: .h #import "SignupViewController.h"
@interface UpdateViewController : SignupViewController
@end
.m
#import "UpdateViewController.h"
@interface UpdateViewController ()
@end
@implementation UpdateViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
CGFloat lineHeight = .05*_height;
if (textField == self.firstName) {
[textField resignFirstResponder];
[self.lastName becomeFirstResponder];
} else if (textField == self.lastName) {
[textField resignFirstResponder];
[self.emailAddress becomeFirstResponder];
[self.scrollView setContentOffset: CGPointMake(0, lineHeight)animated:YES];
} ...
return YES;
}
@end
If someone can point me to a resource that spells this all out I'd really appreciate it. I'm sure it's in the docs, but I have not been able to find it.
I am getting errors that UpdateViewController (inheriting from SignupViewController) has no scrollView, emailAddress, firstName, lastName, etc property. Do I have to redeclare all these properties/write everything out?
If the properties are declared in SignupViewController.m, then they will be invisible to all code outside SignupViewController.m. This is intentional and he most common way to declare properties.
If you want them to be public, you must declare the properties in SignupViewController.h, and remove them from the .m file.
Anything in a .m file is only available from within that file, if you want it available elsewhere you must put it in a .h file and import the .h file. It's not a common programming language feature today but in older programming languages (and Obj-C is aproaching 30 years) that is generally how all of them work.