UIScrolllView y botón de información

I have custom UIViewController which looks like this.

@implementation UIViewControllerCustom


- (id)init
{
    self = [super init];
    if (self) {
        NSLog(@"init");
    }

    return self;
}



-(void)viewDidLoad
{
    [super viewDidLoad];
    UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
    [infoButton setFrame:CGRectMake(290, 10, 16, 16)];
    [infoButton addTarget:self action:@selector(showInfo) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:infoButton];
}

-(void)showInfo
{
    About *about = [[About alloc]init];
    NSLog(@"touched");
    [about setModalTransitionStyle:UIModalPresentationFullScreen];
    [self presentModalViewController:about animated:YES];
}

@end

Any view controller in my app inherits this controller. When info button is touched the modal window is present. The problem is that one of my view controllers use UIScrollView. In this controller info button is visible and it reacts on touches. The problem is that when I touch the button showInfo method of UIViewControllerCustom is not called. I have no such a problem in the other controller. What can be wrong here ?

preguntado el 10 de marzo de 12 a las 10:03

1 Respuestas

Because the UIScrollView handles the message being sent. In your case the touch on the button will be handled by the UIScrollView.

I would suggest you to subclass the UIScrollView and implement: - (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view to determine if the UIScrollView should handle the event or not.

The implementation could be something along the lines of:

- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view {
    if ([view isKindOfClass:[UIButton class]]) {
        return NO;
    }

    return YES;
}

respondido 10 mar '12, 11:03

When I use the code you suggest I get control only of button within the scrollView. I still can't get control over info button. - Oh, Doh

Ah, I figured the view property of your UIViewController was the UIScrollView. Is your UIScrollView a subview of self.view in your UIViewController? If that is the case isn't it just the order of the subviews that stops you from interacting with your UIButton? What happens if you do: [self.view sendSubviewToBack:scrollView]; in your UIViewController? - Markus Persson

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.