Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

How to create a reusable custom view class?

jemichajemicha Posts: 26Registered Users
I am currently making an app that will have multiple timers, which are basically all the same.

I want to create a custom class that uses all of the code for the timers as well as the layout/animations, so I can have 5 identical timers that operate independently of each other.

I created the layout using IB (xcode 4.2) and all the code for the timers is currently just in the viewcontroller class.

I am having difficulty wrapping my brain around how to encapsulate everything into a custom class and then add it to the viewcontroller, any help would be much appreciated.
Post edited by jemicha on
Watermelon App Works



Gas Fitter

<a href="http://www.watermelonappworks.com/tinytime/" target

Replies

  • Duncan CDuncan C Posts: 8,023Tutorial Authors, Registered Users
    jemicha;408691 said:
    I am currently making an app that will have multiple timers, which are basically all the same.

    I want to create a custom class that uses all of the code for the timers as well as the layout/animations, so I can have 5 identical timers that operate independently of each other.

    I created the layout using IB (xcode 4.2) and all the code for the timers is currently just in the viewcontroller class.

    I am having difficulty wrapping my brain around how to encapsulate everything into a custom class and then add it to the viewcontroller, any help would be much appreciated.
    Sect the group in the files and groups organizer in your project where you want your new view class to live (inside classes somewhere).

    Pull down the Xcode file menu, and select new. Select new file. From there, select iOS, and "Cocoa touch". From the choices selected, select "Objective C class". Click next. In the following screen, you will see a popup "Subclass of". Choose UIView, and name your class. e.g. "MyViewClass". Press enter again.

    Xcode will create MyViewClass.m and MyViewClass.h, with a few standard methods like initWithFrame.

    Now, add the ivars, properties and methods you need to implement your class.

    Once you have your class defined, #import it's header into any view controller where you plan to use it.

    Finally, in interface builder, create an object of type UIView. Then select that view object, go into the "identity inspector", and change the object's class from UIView to your custom class (MyViewClass, in our example.)

    Now, when you create your view controller, and it loads its nib file, it will create an instance of your custom object and put it in your view controller.
    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
  • jemichajemicha Posts: 26Registered Users
    Duncan C;408700 said:

    Now, add the ivars, properties and methods you need to implement your class.
    Thanks for your help once again Duncan.

    How can I ensure that all the buttons will remain wired up correctly and that the layout will remain when I instantiate more of these?

    (I did all the wiring up of the buttons and in storyboard in the ViewController)

    Essentially, how can I use the view that I created as the new class?
    Watermelon App Works



    Gas Fitter

    <a href="http://www.watermelonappworks.com/tinytime/" target
  • jemichajemicha Posts: 26Registered Users
    It looks like I might have to create all of the layout and actions programmatically, is that a correct assumption?

    Or, is there a way to utilize the layout and actions already set up in storyboard?
    Watermelon App Works



    Gas Fitter

    <a href="http://www.watermelonappworks.com/tinytime/" target
  • Duncan CDuncan C Posts: 8,023Tutorial Authors, Registered Users
    jemicha;408704 said:
    Thanks for your help once again Duncan.

    How can I ensure that all the buttons will remain wired up correctly and that the layout will remain when I instantiate more of these?

    (I did all the wiring up of the buttons and in storyboard in the ViewController)

    Essentially, how can I use the view that I created as the new class?
    See my instructions. You create the view objects in IB/Storyboard using their parent class (if you're creating a custom subclass of UIButton, create it in IB as a button). Then you select the "identity inspector" and change the object's type to your custom subclass.

    Since the object inherits from it's parent class, you can do the same wiring that you can with the non-custom version of that class.
    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
  • Duncan CDuncan C Posts: 8,023Tutorial Authors, Registered Users
    jemicha;408716 said:
    It looks like I might have to create all of the layout and actions programmatically, is that a correct assumption?

    Or, is there a way to utilize the layout and actions already set up in storyboard?
    Sure. Just make your custom object a subclass of whatever type it needs to be. If you're creating a custom subclass of UIButton, make sure it's declared as such. Then go in with Storyboard once you've defined your custom type and change the type to your new custom type (as described in my previous posts)
    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
  • jemichajemicha Posts: 26Registered Users
    I got it all working, thanks so much for your help. I ended creating a new .xib file with the custom view class as its file's owner, along with your ideas on how to create the custom classes.
    Watermelon App Works



    Gas Fitter

    <a href="http://www.watermelonappworks.com/tinytime/" target
Sign In or Register to comment.