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.
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.
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
Animated GIF created with Face Dancer, available for free in the app store.
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
Animated GIF created with Face Dancer, available for free in the app store.
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
Animated GIF created with Face Dancer, available for free in the app store.
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.
Replies
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.
Duncan C
WareTo
Animated GIF created with Face Dancer, available for free in the app store.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeHow 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?
Gas Fitter
<a href="http://www.watermelonappworks.com/tinytime/" target
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeOr, is there a way to utilize the layout and actions already set up in storyboard?
Gas Fitter
<a href="http://www.watermelonappworks.com/tinytime/" target
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeSince 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.
Duncan C
WareTo
Animated GIF created with Face Dancer, available for free in the app store.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeDuncan C
WareTo
Animated GIF created with Face Dancer, available for free in the app store.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeGas Fitter
<a href="http://www.watermelonappworks.com/tinytime/" target
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome