2010-02-05 18 views
0

我對於由apple提供的MoviePlayer示例代碼有疑問。
我不明白overlayViewTouch通知如何工作。當我觸摸視圖(而不是按鈕)時,我添加到它的NSlog消息不會被髮送。overlayViewTouched通知如何在MoviePlayer示例代碼中工作

// 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... 
} 

我可以,但是,得到的NSLog通知,如果我把它放在 - (空)的touchesBegan在 「MyOverlayView.m」。這讓我覺得它識別觸摸,但不是發送通知。

// 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]; 



    }  
} 

任何人都可以闡明我失蹤或做錯了什麼?

謝謝。

回答

0

在我看來,示例代碼缺少addObserver選擇器調用通知。登記的一個例子可以在AppDelegate中找到:

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

如NSNotificationCenter文檔 當一個對象(被稱爲通知發送方)張貼的通知時,它發送一個NSNotification對象到通知中心。然後,通知中心通過向他們發送指定的通知消息,將通知作爲唯一參數傳遞給通知符合註冊指定標準的任何觀察者。

如果沒有觀察者,NSNotificationCenter不會通知任何人。

只需在init中添加相應的寄存器即可。

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

這是因爲覆蓋視圖很小。您可以通過更改疊加視圖的背景顏色來查看疊加視圖覆蓋的區域。通知將在您觸摸該區域時發送。

相關問題