Create a new class and load some copies of it into the view
Ok, i made a small application with a small ball bouncing on the screen. That w开发者_JAVA百科as esay. Now i'd like to have more than one ball... so i think the best way is to create a new class : the ball.
How can i do it?
Thanks!
encapsulate the information about the ball from your first app into variables, and create methods for any actions you think the ball performs. This sounds a lot like an object-oriented design problem..perhaps you should take a look at an Object Oriented Programming guide, such as this one OOP Guide
but if you are asking how to create a custom object class.. put this in your Ball.h:
#import <Foundation/Foundation.h>
@interface Ball : NSObject {
IBOutlet UIButton *button; //sample instance variable
}
@property (retain) UIButton *button;
@end
then in your .m file:
#import "Ball.h"
@implementation Ball
@synthesize button;
-(void)dealloc
{
[button release];
[super dealloc];
}
@end
thank you very much for your answer. I'll try to be more precise about my problem: This "ball" should be associated to an image of a ball. I made this in Interface Builder, doing like this:
- Loaded a png file in Xcode * Created an IBOutlet UIImageView named "ball"
- In Interface Builder, I have added a UIImageView on the main view and linked it to "ball".
- Added some methods
- Everything working fine.
But now, with the class, how can I do the same things? I'd like to pick my png image and associate it to every instance of Ball, and have it on my view. Thanks!
精彩评论