views:

308

answers:

2

Hi,

I have a question regarding the MoviePlayer sample code provided by apple.
I don't understand how the overlayViewTouch notification works. The NSlog message I added to it does not get sent when I touch the view (not button).

// post the "overlayViewTouch" notification and will send
// the overlayViewTouches: message
- (void)overlayViewTouches:(NSNotification *)notification
{
    NSLog(@"overlay view touched");
    // Handle touches to the overlay view (MyOverlayView) here... 
}

I can, however, get the NSlog notification if I place it in -(void)touchesBegan in "MyOverlayView.m". Which makes me think it is recognizing touches but not sending a notification.

 // Handle any touches to the overlay view
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch* touch = [touches anyObject];
        if (touch.phase == UITouchPhaseBegan)
        {
            NSLog(@"overlay touched(from touchesBegan")
            // IMPORTANT:
            // Touches to the overlay view are being handled using
            // two different techniques as described here:
            //
            // 1. Touches to the overlay view (not in the button)
            //
            // On touches to the view we will post a notification
            // "overlayViewTouch". MyMovieViewController is registered 
            // as an observer for this notification, and the 
            // overlayViewTouches: method in MyMovieViewController
            // will be called. 
            //
            // 2. Touches to the button 
            //
            // Touches to the button in this same view will 
            // trigger the MyMovieViewController overlayViewButtonPress:
            // action method instead.

            NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
            [nc postNotificationName:OverlayViewTouchNotification object:nil];



  }    
}

Can anyone shed light on what I am missing or doing wrong?

Thank you.

A: 

As it seems to me the sample code is missing the addObserver selector call to the Notification. An example of the registration can be found in the AppDelegate:

[[NSNotificationCenter defaultCenter] addObserver:self 
                 selector:@selector(moviePreloadDidFinish:) 
                 name:MPMoviePlayerContentPreloadDidFinishNotification 
                 object:nil];

As in NSNotificationCenter Documentation When an object (known as the notification sender) posts a notification, it sends an NSNotification object to the notification center. The notification center then notifies any observers for which the notification meets the criteria specified on registration by sending them the specified notification message, passing the notification as the sole argument.

If there are no observers no one will be informed by NSNotificationCenter.

Just add the appropriate register in init for example.

[[NSNotificationCenter defaultCenter] addObserver:self 
                 selector:@selector(overlayViewTouches:) 
                 name:OverlayViewTouchNotification 
                 object:nil];
maxbareis
A: 

It's because the overlay view is small. You can see the area covered by the overlay view by changing the background color of the overlay view. The notification will be delivered when you touch the area.

y5h