One Date Picker UIViewController for all parent controllers

Couldn't think of an intuitive way to rephrase the topic for this question and I apologize for that. My question is the following:

I have several UIViewControllers that need to be called on the UIDatePicker. I didn't want to subclass Date Picker multiple times as it is the same interface. The problem I am running into is figuring out what Cocoa / Objective-C tools provide so that I can abstract my child controller so it doesn't need to know who the parent controller is. The parent controller just has to tell the child controller who they are by passing self in to some instance variable of the child controller. The problem is that the child controller cannot access the methods / variables of the parent instance without importing the class interface file. This again means that the child knows every single parent who names him.

Looking for the best way to solve this problem seems to be very common.

+1


source to share


2 answers


It is not obvious from the question if you are creating a custom UIDatePicker subclass, or if you are simply using the UIDatePicker delegation methods in several different custom UIViewController subclasses.

Anyway, I think some combination of delegate and protocol is what you are looking for.

The parent controller just has to tell the child controller who they are by passing themselves to one child the controller instance variable

This is exactly what the delegate is doing. Declare it in your Objective-C class as a type identifier. You can do this as a property if you like to use common simmatics.

id delegate;

      

Now, to solve the second problem:

The problem with this is the child controller cannot access the parent instance methods / variables without importing the class file interface

You definitely don't want to access ivars directly, and in Objective-C you can always send any message to any object (you will get a compiler warning, but this is not an error).

There is a way to get rid of these warnings, although it is called Protocols. The protocol is similar to the Java interface; it allows you to define a list of methods, and classes can declare that they implement a protocol.



The syntax for creating a protocol is as follows:

@protocol ProtocolName

- (void)sometMethod;

@end

      

When you declare your class, you say that it implements a protocol like this:

@interface MyClass <MyProtocol>

      

Use a comma delimited list to implement multiple protocols.

The last trick you want to know is to define a pointer that requires asignee to implement a specific protocol. Going back to our delegate implementation earlier, we can add the same angle bracket syntax that tells the compiler that we expect an object that we are assigning to implement this protocol.

id<MyProtocol> delegate;

      

You can declare a protocol in your own file, and you only need to import this protocol file into any class that uses the protocol. You can also use this in conjunction with August's suggestion above and implement common functionality in a generic base controller object, having each subclass a custom controller.

(another overview of protocols: http://en.wikipedia.org/wiki/Objective-C#Protocols )

+2


source


You can have a "base" view controller like BaseViewController. This controller will have some specific date code in it. Then all associated view controllers will then be subclasses of that base controller. Adjust the behavior as needed for each controller, but in each of them you will have basic processing code. If you are using a single loopback for all of these controllers, make sure the File Owner object class is set to your BaseViewController. All subclasses will still be able to access the nose very well.



0


source







All Articles