Hi there!
I've written my first application in objective-c for my iphone. It's an lottery number generator. It worked perfectly under iPhone SDK beta 7, but since i've upgraded to the newest version of iPhone SDK the UIButton is no more working (the method for UIControlEventTouchUpInside doesn't get invoked). Can anyone help me? Thanks!
Source:
http://www.lightforce.ch/Lotto.zip
I've written my first application in objective-c for my iphone. It's an lottery number generator. It worked perfectly under iPhone SDK beta 7, but since i've upgraded to the newest version of iPhone SDK the UIButton is no more working (the method for UIControlEventTouchUpInside doesn't get invoked). Can anyone help me? Thanks!
Source:
http://www.lightforce.ch/Lotto.zip
Code:
#import <UIKit/UIKit.h>
@interface LottoGeneratorAppDelegate : NSObject <UITextFieldDelegate> {
UIWindow *window;
UIImageView *contentView;
UILabel *label;
}
@property (nonatomic, retain) UIWindow *window;
@property (nonatomic, retain) UIImageView *contentView;
@property (nonatomic, retain) UILabel *label;
@end
Code:
#import "LottoGeneratorAppDelegate.h"
#define BUTTON_FONT_SIZE 14.0
#define TEXT_LABEL_FONT_SIZE 12.0
#define MARGIN_TOP 110
#define MARGIN_LEFT 104
#define LABEL_BG_ALPHA 0.2
@implementation LottoGeneratorAppDelegate
//Generates accessor methods
@synthesize window;
@synthesize contentView;
@synthesize label;
//Declare a array with pointers to UILabel
NSMutableArray *mylabels;
NSMutableArray *mystarlabels;
- (void)addControlsToContentView:(UIImageView *) aContentView {
//Allocate memory for mylabels array
mylabels = [[NSMutableArray alloc] init];
mystarlabels = [[NSMutableArray alloc] init];
//CGRect contentFrame = aContentView.frame;
// Create a button using an image as the background.
// Use the desired background image's size as the button's size
UIImage *buttonBackground = [UIImage imageNamed:@"Button.png"];
CGRect buttonFrame = CGRectMake(0.0, 0.0, buttonBackground.size.width, buttonBackground.size.height);
UIButton *button = [[UIButton alloc] initWithFrame:buttonFrame];
[button setTitle:@"Generate" forState:UIControlStateNormal];
button.font = [UIFont boldSystemFontOfSize:BUTTON_FONT_SIZE];
// Center the text on the button, considering the button's shadow
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
button.titleEdgeInsets = UIEdgeInsetsMake(-2.0, 0.0, 2.0, 0.0); //Create inset for the shadow below
// Set the background image on the button
[button setBackgroundImage:buttonBackground forState:UIControlStateNormal];
[buttonBackground release];
[button addTarget:self action:@selector(generate:) forControlEvents:UIControlEventTouchUpInside]; //generate: is sent when the button is touched
// Position the button centered horizontally in the contentView
button.center = CGPointMake(aContentView.center.x, aContentView.center.y+175);
[aContentView addSubview:button];
[button release];
CGFloat posx = MARGIN_LEFT;
CGFloat posy = MARGIN_TOP;
CGRect labelFrame;
UILabel *aLabel;
// Create labels for Numbers
int i;
for(i=1;i<=50;i++){
posx += 23;
if((i % 5)==1){
posx = MARGIN_LEFT;
posy += 18;
}
labelFrame = CGRectMake(posx, posy, 22, TEXT_LABEL_FONT_SIZE +5 );
aLabel = [[UILabel alloc] initWithFrame:labelFrame];
aLabel.font = [UIFont systemFontOfSize:TEXT_LABEL_FONT_SIZE];
aLabel.textColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
aLabel.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:LABEL_BG_ALPHA];
aLabel.textAlignment = UITextAlignmentCenter;
aLabel.text = [NSString stringWithFormat: @"%d", i];
//Register label in array
[mylabels addObject: aLabel];
self.label = aLabel;
[aLabel release];
[aContentView addSubview: self.label];
}
posy += 1;
for(i=1;i<=9;i++){
posx += 38;
if((i % 3)==1){
posx = MARGIN_LEFT;
posy += 24;
}
///place the stars
CGRect starFrame = CGRectMake(posx+6, posy, 25, 23);
UIImageView *anImageView = [[UIImageView alloc] initWithFrame:starFrame];
anImageView.image = [UIImage imageNamed:@"star.png"];
self.contentView = anImageView;
[anImageView release];
[aContentView addSubview: self.contentView];
//Place the Labels
labelFrame = CGRectMake(posx, posy, 37, TEXT_LABEL_FONT_SIZE +12 );
aLabel = [[UILabel alloc] initWithFrame:labelFrame];
aLabel.font = [UIFont systemFontOfSize:TEXT_LABEL_FONT_SIZE];
aLabel.textColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:1.0];
aLabel.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.0];
aLabel.textAlignment = UITextAlignmentCenter;
aLabel.text = [NSString stringWithFormat: @"%d", i];
//Register label in array
[mystarlabels addObject: aLabel];
self.label = aLabel;
[aLabel release];
[aContentView addSubview: self.label];
}
}
// This method is invoked when the generate button or return key is touched
- (void)generate:(id)sender {
NSLog(@"Lotto numbers generated.");
NSMutableArray *zufallsArrayNumbers;
NSMutableArray *zufallsArrayStars;
zufallsArrayNumbers = [[NSMutableArray alloc] init];
zufallsArrayStars = [[NSMutableArray alloc] init];
NSNumber *zufallszahlNumbers;
NSNumber *zufallszahlStars;
//Shuffles Randomizer
srandom(time(NULL));
//Variables for for-loops
int a,x,i;
BOOL valid;
for (a = 0; a < 6; a++){
valid = false;
// Solange Zufallszahlen erzeugen bis eine gültige, noch nicht
// vorhandene Zahl ermittelt wurde.
while (valid == false){
// Neue Zufallszahl erzeugen
zufallszahlNumbers = [[NSNumber alloc] initWithInt:(random() % 50)];
// Diese Zufallszahl ist einmalig bist das Gegenteil bewiesen ist
valid = true;
// Prüfen ob diese Zahl mit einem Elemente
// im Array übereinstimmt
for (x = 0; x < [zufallsArrayNumbers count]; x++){
if (zufallszahlNumbers == [zufallsArrayNumbers objectAtIndex:x]){
// Übereinstimmung gefunden,diese Zahl gibt es schon
valid = false;
// Zahl aus dem Speicher entfernen
[zufallszahlNumbers release];
break;
}
}
}
// Neue Zahl ermittelt. Zahl zum Array hinzufügen
[zufallsArrayNumbers addObject: zufallszahlNumbers];
[zufallszahlNumbers release];
}
for (a = 0; a < 2; a++){
valid = false;
// Solange Zufallszahlen erzeugen bis eine gültige, noch nicht
// vorhandene Zahl ermittelt wurde.
while (valid == false){
// Neue Zufallszahl erzeugen
zufallszahlStars = [[NSNumber alloc] initWithInt:(random() % 9)];
// Diese Zufallszahl ist einmalig bist das Gegenteil bewiesen ist
valid = true;
// Prüfen ob diese Zahl mit einem Elemente
// im Array übereinstimmt
for (x = 0; x < [zufallsArrayStars count]; x++){
if (zufallszahlStars == [zufallsArrayStars objectAtIndex:x]){
// Übereinstimmung gefunden,diese Zahl gibt es schon
valid = false;
// Zahl aus dem Speicher entfernen
[zufallszahlStars release];
break;
}
}
}
// Neue Zahl ermittelt. Zahl zum Array hinzufügen
[zufallsArrayStars addObject: zufallszahlStars];
[zufallszahlStars release];
}
UILabel *tmp;
//clean the labels
for(i=0;i<[mylabels count];i++){
tmp = [mylabels objectAtIndex:i];
//tmp.textColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
//tmp.font = [UIFont systemFontOfSize:12.00];
tmp.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:LABEL_BG_ALPHA];
}
//clean the starlabels
for(i=0;i<[mystarlabels count];i++){
tmp = [mystarlabels objectAtIndex:i];
tmp.textColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:1.0];
tmp.font = [UIFont systemFontOfSize:12.00];
}
//Set 6 random labels // O(1)
for(i=0;i<[zufallsArrayNumbers count];i++){
tmp = [mylabels objectAtIndex:[[zufallsArrayNumbers objectAtIndex:i] intValue]];
//tmp.textColor = [UIColor redColor];
//tmp.font = [UIFont boldSystemFontOfSize:12.00];
tmp.backgroundColor = [UIColor colorWithRed:1.0 green:0.3 blue:0.3 alpha:0.6];
}
//Set 2 random starlabels // O(1)
for(i=0;i<[zufallsArrayStars count];i++){
tmp = [mystarlabels objectAtIndex:[[zufallsArrayStars objectAtIndex:i] intValue]];
tmp.textColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0];
tmp.font = [UIFont boldSystemFontOfSize:12.00];
}
//Reset pointer
tmp = nil;
[zufallsArrayStars release];
[zufallsArrayNumbers release];
}
- (void)dealloc {
[contentView release];
[window release];
[label release];
[mylabels release];
[mystarlabels release];
[super dealloc];
}
#pragma mark -
#pragma mark UIApplication delegate method
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];
// Set up the window and content view
CGRect screenRect = [[UIScreen mainScreen] bounds];
UIWindow *aWindow = [[UIWindow alloc] initWithFrame:screenRect];
self.window = aWindow;
[aWindow release];
// Add the image as the background view
UIImageView *anImageView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
anImageView.image = [UIImage imageNamed:@"Background.png"];
self.contentView = anImageView;
[anImageView release];
[window addSubview: self.contentView];
[self addControlsToContentView: self.contentView];
// Show the window
[window makeKeyAndVisible];
//Generate numbers
[self generate: self.contentView];
NSLog(@"Lotto generator started.");
}
@end