2012-10-03 3 views
7

私は目的Cのブロックにはかなり新しいです。私はドキュメントを読んだので、かなり基本的な理解ができました。ブロックステートメントにUIAlertViewを割り当てる/表示する

なぜこれは機能しませんか?これは、カレンダーへのアクセスを要求するためのフレームワークコールバックです。これは引数としてブロックを取ります。私がしたいのは、ブロック内にUIAlertViewを割り当てて表示するだけですが、表示しようとするとクラッシュします。

これは愚かな質問ではないことを願っています...ブロックを使用してネット上のすべてのイントロの例は、カウンターで簡単な例を示しています。

//Request access 
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) { 

      if (granted == FALSE) { 
       UIAlertView *myAlert = [[[UIAlertView alloc]initWithTitle:@"Calendar Access Denied" 
                      message:@"<InfoText>" 
                     delegate:nil 
                   cancelButtonTitle:@"OK" 
                   otherButtonTitles:nil] autorelease]; 
       [myAlert show]; 
      } 
      else { 
       [self addToCalendar]; 
      } 
     }]; 

答えて

24

試しましたか?

if (granted == FALSE) 
{ 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     UIAlertView *myAlert = [[[UIAlertView alloc]initWithTitle:@"Calendar Access Denied" 
                 message:@ <InfoText>" 
                 delegate:nil 
               cancelButtonTitle:@"OK" 
               otherButtonTitles:nil] autorelease]; 
     [myAlert show]; 
    }); 
} 

このブロックとのUIKit

を混合するのに有用な、バックメインスレッドに呼び出しを行います