I’ve got a simple problem but I don’t know why it doesn’t work.
I created a button programatically and I’d like to add the method “backLogin” to the action.
Strange enough, I don’t get any errors, but the button just won’t respond in any way. Anywhere I look on the internet, this code should definitely work.
Any ideas what I’m doing wrong?
-(void) loginAppear{
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(20, 20, 75, 37); // position in the parent view and set the size of the button
[myButton setTitle:@"Back" forState:UIControlStateNormal];
// add targets and actions
[myButton addTarget:self action:@selector(backLogin) forControlEvents:UIControlEventTouchUpInside];
// add to a view
[myButton setAlpha:1];
[loginImage addSubview:myButton];
}
The code behind “backLogin” has already worked in other circumstances:
-(void) backLogin{
CALayer *layer = loginImage.layer;
//spin frame
CABasicAnimation *anim1 =
[CABasicAnimation animationWithKeyPath: @"transform" ];
anim1.toValue = [ NSValue valueWithCATransform3D:
CATransform3DMakeRotation(180 * M_PI, 0.0, 1.0, 0) ];
anim1.duration = .2;
anim1.cumulative = YES;
anim1.repeatCount = 1;
[layer addAnimation: anim1 forKey: @"transformAnimation" ];
}
Instead of adding myButton to loginImage, try adding it to the main view (e.g. [self.view addSubview:myButton]). You may need to change the frame coordinates to position the button properly.