Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Draw This
($0.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum

View Single Post
Old 07-12-2008, 01:17 PM   #1 (permalink)
lightforce
New Member
 
Join Date: Jul 2008
Posts: 2
lightforce is an unknown quantity at this point
Default UIButton action no more working with iPhone SDK 2.0

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). This means when i press the «Generate» Button in my application, nothing happens. But the associated method itself would work.
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
lightforce is offline   Reply With Quote
 

» Advertisements
» Online Users: 594
16 members and 578 guests
akiraliu, baja_yu, chenpo, Domele, GVD, hemcii, ilmman, ITCreative, jerrywenwu, mediaspree, nanijoe, Newbie123, sanlows, soydesu, yuonneru49
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 174,544
Threads: 93,725
Posts: 401,261
Top Poster: BrianSlick (7,954)
Welcome to our newest member, soydesu
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:27 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.