2010-06-25 22 views
0

我想開發一個iphone應用程序,通過使用tcp/ip與硬件進行通信。 現在,應用程序發送到硬件是好的。 爲了方便開發,我想用fire事件來開發接收器。 有沒有人有任何想法?如何使用Xcode觸發事件?

回答

6

引發一個事件:

[[NSNotificationCenter defaultCenter] postNotificationName:notificationName object:someObj]; 

要監聽的事件:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationSelector:) name:notificationName object:notificationSender]; 

要停止監聽事件:

[[NSNotificationCenter defaultCenter] removeObserver:self name:notificationName object:notificationSender]; 

More here

1
add 2 frameworks 
1.EventKitUI.framework 
2.EventKit.framework 

.m 
#import <EventKitUI/EventKitUI.h> 

- (IBAction)CreateEvent:(id)sender { 

    //Get the even store object 
    //EKEventStore *eventStore = [[EKEventStore alloc] init]; 
    EKEventStore *eventStore = [[EKEventStore alloc] init]; 

    /* iOS 6 requires the user grant your application access to the Event Stores */ 


    if([[UIDevice currentDevice].systemVersion floatValue] >= 6.0)// checks if device is ios6 
     { 
    [eventStore requestAccessToEntityType:EKEntityTypeReminder completion:^(BOOL granted, NSError *error) { 
     // handle access here 
    }]; 





    if ([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)]) 
    { 
     // iOS Settings > Privacy > Calendars > MY APP > ENABLE | DISABLE 
     [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) 
     { 
      if (granted) 
      { 
       NSLog(@"User has granted permission!"); 
      } 
      else 
      { 
       NSLog(@"User has not granted permission!"); 
      } 
     }]; 
    } 



      EKEventEditViewController *controller=[[EKEventEditViewController alloc]init]; 


      controller.eventStore=eventStore; 
      controller.editViewDelegate=self; 
      controller.wantsFullScreenLayout=YES; 
      [self presentModalViewController:controller animated:YES]; 

    } 


    else 
    { 


    EKEventEditViewController *controller=[[EKEventEditViewController alloc]init]; 


    controller.eventStore=eventStore; 
    controller.editViewDelegate=self; 
    controller.wantsFullScreenLayout=YES; 
    [self presentModalViewController:controller animated:YES]; 

    } 
} 



- (void)eventEditViewController:(EKEventEditViewController *)controller didCompleteWithAction:(EKEventEditViewAction)action 
{ 
    // [Bw alert:@"Done"]; 


    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@" Done" message:@"Event Sucessfully Saved " delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil]; 
    [alert show]; 

    [self dismissModalViewControllerAnimated:YES]; 

} 





@end 
+0

NSNotificationCenter更容易 – Jonesopolis 2013-04-16 17:13:32