Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

UIButton not working

TheMirxTheMirx Posts: 8New Users
edited December 2009 in iPhone SDK Development
Hello Everyone,

Let me first say I am new to these forums, and that I'm glad to be a part of this community as I have seen many, many good posts that helped me out a lot. I am also new to iPhone development in general (so bear with me).

I have searched everywhere for help on this issue, but I cannot seem to solve the problem. I am trying to add buttons to my UIView, and while that should be straightforward, I am at a loss why the buttons don't respond at all to "touches" (in the sim).

Here is the code:

AppDelegate.m
- (void)applicationDidFinishLaunching:(UIApplication *)application {    
    
    // Override point for customization after app launch    
    [application setStatusBarHidden:YES animated:NO];
	viewController = [[MenuController alloc] init];
	[window addSubview:viewController.view];
    [window makeKeyAndVisible];
	application.statusBarOrientation = UIInterfaceOrientationLandscapeRight;
	//application.statusBarHidden = YES;
	CGAffineTransform transform = CGAffineTransformMakeRotation(3.14159/2);
	viewController.view.transform = transform;
	CGRect contentRect = CGRectMake(0, 0, 480, 320);
	viewController.view.bounds = contentRect;
}

MenuController.m
        CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
	UIView *view = [[UIView alloc] initWithFrame:appFrame];
	view.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
	self.view = view;
	[view release];
	
	UIImage *image = [UIImage imageWithContentsOfFile:[self getPathFromFilename:@"startScreen.Background.png"]];
	menuBackground = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,480,320)];

	menuBackground.image = image;
	[self.view addSubview:menuBackground];
	
	// ==== TUTORIAL IMAGE ====
	UIImage *imgTutorial = [UIImage imageWithContentsOfFile:[self getPathFromFilename:@"startScreen.tutorial.png"]];
	UIImage *imgTutorialTouch = [UIImage imageWithContentsOfFile:[self getPathFromFilename:@"startScreen.tutorial.touch.png"]];
	
	UIButton *btnTutorial = [[UIButton alloc] initWithFrame:CGRectMake(-16, 215, imgTutorial.size.width, imgTutorial.size.height)];
	
	[btnTutorial setImage:imgTutorial forState:UIControlStateNormal];
	[btnTutorial setImage:imgTutorialTouch forState:UIControlEventTouchDown];
	[btnTutorial addTarget:self action:@selector(eventShowTutorial) forControlEvents:UIControlEventTouchUpInside];
	//[self.view addSubview:btnTutorial];
	[menuBackground addSubview:btnTutorial];
	[btnTutorial release];


Basically, the UIButton is added to the display, but I cannot click it or get it to respond.

Another note is that I tried adding the button to self.view (as you can see from the comments), and that works just fine. HOWEVER, when I load a new screen, somehow the buttons get activated when I click on that area of the screen even though they are not visible.

This issue is slowly driving me crazy, and is one of the last pieces of the puzzle before we start testing on our devices, so I would really appreciate if someone could help me out.

By the way, I have seen this post but it does not seem to help me.

Thanks in advance,

TheMirx
Post edited by TheMirx on

Replies

  • ketanketan Posts: 19Registered Users
    edited July 2009
    And you have a method named "eventShowTutorial" which is your IBAction for the button click event?
  • TheMirxTheMirx Posts: 8New Users
    edited July 2009
    ketan wrote: »
    And you have a method named "eventShowTutorial" which is your IBAction for the button click event?

    Yes, that's correct. The buttons all work when I add them via [self.view addSubview:btnTutorial]. Here's the code for eventShowTutorial:
    - (void) eventShowTutorial {
    	TutorialController *tutorialController = [[TutorialController alloc] initWithNibName:@"TutorialController" bundle:[NSBundle mainBundle]];
    	tutorialController.view.alpha = 1.0;
    	[self.view addSubview:tutorialController.view];
    }
    

    It's nothing fancy, and I have other buttons with methods just like this, some of which perform more complex things.
  • ketanketan Posts: 19Registered Users
    edited July 2009
    I am not sure if this will work, but try this -

    Instead of -
    - (void) eventShowTutorial {
    

    Put this -
    - (IBAction) eventShowTutorial:(id)sender {
    
  • TheMirxTheMirx Posts: 8New Users
    edited July 2009
    ketan wrote: »
    I am not sure if this will work, but try this -

    Instead of -
    - (void) eventShowTutorial {
    

    Put this -
    - (IBAction) eventShowTutorial:(id)sender {
    

    No luck. I should also mention that it's not just the event that doesn't fire but also the image doesn't change when I click on the button.
  • umopumop Posts: 5New Users
    edited November 2009
    Just curious. Did you ever get this figured out? I'm dealing with pretty much the same issue over here.

    Thanks!
    TheMirx wrote: »
    No luck. I should also mention that it's not just the event that doesn't fire but also the image doesn't change when I click on the button.
  • TheMirxTheMirx Posts: 8New Users
    edited December 2009
    umop wrote: »
    Just curious. Did you ever get this figured out? I'm dealing with pretty much the same issue over here.

    Thanks!

    Hey, sorry for the late response. The solution that seemed to work for me was to add another UIView layer between the "base" layer and the buttons, like this:
    UIImage *image = [UIImage imageWithContentsOfFile:[self getPathFromFilename:@"startScreen.Background.png"]];
    	menuBackground = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,480,320)];
    
    	menuBackground.image = image;
    	[self.view addSubview:menuBackground];
    	
    	menuButtonsView = [[UIView alloc] initWithFrame:CGRectMake(0,0,480, 300)];
    	[self.view addSubview:menuButtonsView];
    	
    	// ==== TUTORIAL IMAGE ====
    	UIImage *imgTutorial = [UIImage imageWithContentsOfFile:[self getPathFromFilename:@"startScreen.tutorial.png"]];
    	UIImage *imgTutorialTouch = [UIImage imageWithContentsOfFile:[self getPathFromFilename:@"startScreen.tutorial.touch.png"]];
    	
    	UIButton *btnTutorial = [[UIButton alloc] initWithFrame:CGRectMake(-16, 215, imgTutorial.size.width, imgTutorial.size.height)];
    	
    	[btnTutorial setImage:imgTutorial forState:UIControlStateNormal];
    	[btnTutorial setImage:imgTutorialTouch forState:UIControlEventTouchDown];
    	[btnTutorial addTarget:self action:@selector(eventShowTutorial) forControlEvents:UIControlEventTouchUpInside];
    	//[self.view addSubview:btnTutorial];
    	[menuButtonsView addSubview:btnTutorial];
    	[btnTutorial release];
    

    Hope this helps!
Sign In or Register to comment.