Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's not obvious from the question if you're creating a custom UIDatePicker subclass, or if you are just implementing UIDatePicker delegate methods in several different custom UIViewController subclasses.</p> <p>In either case, I think some combination of a delegate and a protocol is what you're looking for. </p> <blockquote> <p>The parent controller just needs to tell the child controller who they are, by passing self in to some child controller instance variable</p> </blockquote> <p>This is exactly what a delegate is useful for. Declare it in your Objective-C class as type id. You can make it a property if you like using the usual symantics.</p> <pre><code>id delegate; </code></pre> <p>Now, to address the second concern:</p> <blockquote> <p>The problem with that is, the child controller cannot access methods/instance variables of the parent without importing the class's interface file</p> </blockquote> <p>You definitely don't want to access ivars directly, and in Objective-C, you can always send any message to any object (you'll get a compiler warning, but it is not an error).</p> <p>There is a way to get rid of those warnings though, called Protocols. A protocol is like a Java interface; it lets you define a list of methods, and classes can declare that they implement the protocol.</p> <p>The syntax for creating a protocol looks like this:</p> <pre><code>@protocol ProtocolName - (void)sometMethod; @end </code></pre> <p>When you declare your class, you say it implements a protocol like this:</p> <pre><code>@interface MyClass &lt;MyProtocol&gt; </code></pre> <p>Use a comma separated list to implement multiple protocols.</p> <p>The last trick you'll want to know is how to define a pointer that requires the asignee implement a specific protocol. Going back to our delegate implementation before, we can add the same angle bracket syntax, which tells the compiler that we expect the object we're assigning to implement this protocol.</p> <pre><code>id&lt;MyProtocol&gt; delegate; </code></pre> <p>You can declare the protocol in its own file, and you need only to import that protocol file in any class that uses the protocol. You can also use this in combination with August's suggestion above, and implement the common functionality in a common base controller object, having each custom controller subclass that base. </p> <p>(another overview of protocols: <a href="http://en.wikipedia.org/wiki/Objective-C#Protocols" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/Objective-C#Protocols</a>)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload