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

Mockup & CodeGen, iPhone & iPad
($9.99)

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

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 07-12-2008, 02:17 PM   #1 (permalink)
New Member
 
Join Date: Jul 2008
Posts: 2
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
Old 07-12-2008, 05:45 PM   #2 (permalink)
Registered Member
 
tawpie's Avatar
 
Join Date: Jul 2008
Posts: 347
Default Re: UIButton action no more working with iPhone SDK 2.0

Make sure to enable user interaction for your view, either in IB or explicitly. Programmatically created UI and UIImage views default to no user interaction.

// enable interaction, the default is no interaction
self.view.userInteractionEnabled = YES;
__________________
"Hardware will break. Software comes broken" Unknown
Calc-12E <-- ditch your old calculator.
CPR•Choking <-- Review your training, just in case. (Free)
All of Nature NW <-- scrolls in x and y, with pinch zoom AND scrollable text to boot. It can be done ('taint easy tho)
tawpie is offline   Reply With Quote
Old 07-13-2008, 01:47 AM   #3 (permalink)
New Member
 
Join Date: Jul 2008
Posts: 2
Default Re: UIButton action no more working with iPhone SDK 2.0

Wow, you are great! That did the trick. Obviously they changed the default behaviour of userInteraction to NO.
Thank you very much!
lightforce is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Subclassing UIButton gloubibou iPhone SDK Development 3 09-07-2008 10:01 AM
UIWindow working right?? cdwiegand iPhone SDK Development 1 06-29-2008 11:23 PM
Action Handling jashsayani iPhone SDK Development 3 06-15-2008 09:01 AM
UIButton with an image ddavtian iPhone SDK Development 1 04-30-2008 03:53 PM
UIButton toggle brianr iPhone SDK Development 2 04-29-2008 10:43 AM


» Advertisements
» Online Users: 239
15 members and 224 guests
ADY, AragornSG, CKAmike, Dani77, Dattee, Duncan C, HDshot, HemiMG, Promo Dispenser, Punkjumper, Rudy, sacha1996, sneaky, spiderguy84, theone8one
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,231
Posts: 380,768
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:18 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0