0

I guess this is basic, but I can't get my head around this.

I used to have only one ViewController in which all my variables were defined, e.g. an UITextView named myTextView. I also had methods in this ViewController for handling events that relate to myTextView, such as - ()hideKeyboard { // do something with myTextView or - (void)keyboardWillShow:(NSNotification *)notification { // do something with myTextView.

As my program became bigger and bigger, I thought about using subclasses, especially for other views. So I started a subclass, eg. mySubClass.h and mySubClass.m, in which I had another UITextView (for argument's sake myOtherTextView). In order to incorporate mySubClass, I #imported it into my ViewController and added a @class mySubClass; and could then produce instances of this class so as to use it in my App.

So far so good. As you can imagine, all the nice methods I defined in my ViewController for what should happen when an UITextView is edited (such as hiding keyboard etc.) didn't work for the UITextView in mySubClass.

It was then suggested to me that I should make another class in which I had all the keyboard events and subclass my ViewController and mySubView to it:

@interface ViewController : MyKeyboardEventsViewController

Now, the problem I am seeing is that I won't be able to access all the views, textviews, textfields etc. that I have created in my ViewController (e.g. myTextView which I mentioned earlier).

How can I achieve that all the variables that I have defined in my ViewController will also be available for MyKeyboardEventsViewController? Or is there another way to handle this?

Basically, I don't get how MyKeyboardEventsViewController will be able to access variables in my ViewController which it will need (e.g. the UITextView in question, or the accessoryView which will pop up etc. etc.).

Any suggestions would be very much welcome.

2 Answers 2

1

Example:

Class A contains a ivar UITextField textField

Class B subclasses Class A and thus it already contains ivar textField

Note: it's not the other way around. Class A does not "see" what ever is created in Class B.

When ever you subclass a class you give your new class the same ivars end methods of that subclassed class.

I hope this is what you were asking for.

EDIT

So for your example I would do the follwing:

  • Create a class "MyUIKeybordEventResponder"
  • Implement all the responder methods like - (BOOL)textFieldShouldReturn:(UITextField *)textField
  • Subclass your ViewController from "MyUIKeybordEventResponder"

Note method textFieldSHouldReturn has a parameter UITextField so it knows which textfield was pressed. So in a way it receives your textField from the subclass.

Sign up to request clarification or add additional context in comments.

6 Comments

So in my case, the UITextField would need to be defined in MyKeyboardEventsViewController. My viewController then subclasses MyKeyboardEventsViewController and thus has access to the UITextField. In that case, can't I simply subclass mySubClass to my viewController? Or is this bad programming practice? As I understand it now, it appears that there won't be much code left in my ViewController... is that standard? Right now I have pretty much everything in my ViewController... I guess this is not good.
Concerning your edit: does hideKeyboard know which textfield / view was send? e.g. - (IBAction)hideKeyboard { textView.scrollEnabled = NO; [textView resignFirstResponder];
For your case you should have your UITextField implemented in viewController, then implement all the UITextFieldDelegate methods in the MyKeyboardEventsViewController. Make your viewController a subclass of MyKeyboardEventsViewController. B/c MyKeyboardEventsViewController is a delegate of UITextFieldDelegate it will recieve the object (UITextField) when it needs to respond to it, so you don't have to worry about passing it manually yourself.
@n.evermind regarding class size. If you create the same code more then once in a project you should put it into a separate class. Hope that answers your question about class size.
@Cyprian: but I still have the problem if I need to access other strings which I have defined in my ViewController. E.g. any kind of array in which I stored date in the ViewController and now need to access in mySubView. Will I do this with a simpleton?
|
1

If I'm understanding this correctly, you have a UIViewController with MyKeyboardEventsViewController as an instance variable and you want to communicate between the two? If that is the case, one option would be to create a protocol.

@protocol MyKeyboardDelegate 

- (void)closeAccessoryView;

@end

(Note - make whatever methods in the protocol that you need, this is simply an example)

In your MyKeyboardEventsViewController you then include the protocol file, and create an ivar

id <MyKeyboardDelegate> delegate;

Also make it a property and synthesize it.

Whatever class that is going to create the keyboardviewcontroller should delcare themselves as conforming to the protocol.

@interface MyViewController : UIViewController <MyKeyboardDelegate>
  ...
@end

When you create the MyKeyboardEventsViewController, set the delegate.

MyKeyboardEventsViewController *eventsVC = [[MyKeyboardEventsViewController alloc] init]; 

[eventsVC setDelegate:self];

Now just implement the delegate method and perform whatever action that is necessary.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.