NOTE:
UIMenuController will only work if it becomes the first responder
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *addJobButton = [[UIBarButtonItem alloc] initWithTitle:@"Add Contacts" style:UIBarButtonSystemItemAdd
target:self action:@selector(setAddContactTapped:event:)];
self.navigationItem.rightBarButtonItem = addJobButton;
[addJobButton release];
}
-(void)setAddContactTapped:(id)sender event:(UIEvent*)event{
[self becomeFirstResponder];
/*get the view from the UIBarButtonItem*/
UIView *buttonView=[[event.allTouches anyObject] view];
CGRect buttonFrame=[buttonView convertRect:buttonView.frame toView:self.view];
UIMenuController *menuController = [UIMenuController sharedMenuController];
UIMenuItem *personMenuItem = [[UIMenuItem alloc] initWithTitle:@"Person" action:@selector(newPerson:)];
UIMenuItem *companyMenuItem = [[UIMenuItem alloc] initWithTitle:@"Company" action:@selector(newCompany:)];
// NSAssert([self becomeFirstResponder], @"Sorry, UIMenuController will not work with %@ since it cannot become first responder", self);
menuController.menuItems = [NSArray arrayWithObjects: personMenuItem, companyMenuItem, nil];
[menuController setTargetRect:buttonFrame inView:self.view];
[menuController setMenuVisible:YES animated:YES];
[personMenuItem release];
[companyMenuItem release];
}
- (void) newPerson: (UIMenuController*) sender
{
//Add you logic
}
- (void) newCompany: (UIMenuController*) sender
{
//Add your logic
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
BOOL can = [super canPerformAction:action withSender:sender];
if (action == @selector(newPerson:) || action == @selector(newCompany:))
{
can = YES;
}
if (action == @selector(copy:))
{
can = NO;
}
return can;
}
- (BOOL) canBecomeFirstResponder {
return YES;
}
UIMenuController will only work if it becomes the first responder
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *addJobButton = [[UIBarButtonItem alloc] initWithTitle:@"Add Contacts" style:UIBarButtonSystemItemAdd
target:self action:@selector(setAddContactTapped:event:)];
self.navigationItem.rightBarButtonItem = addJobButton;
[addJobButton release];
}
-(void)setAddContactTapped:(id)sender event:(UIEvent*)event{
[self becomeFirstResponder];
/*get the view from the UIBarButtonItem*/
UIView *buttonView=[[event.allTouches anyObject] view];
CGRect buttonFrame=[buttonView convertRect:buttonView.frame toView:self.view];
UIMenuController *menuController = [UIMenuController sharedMenuController];
UIMenuItem *personMenuItem = [[UIMenuItem alloc] initWithTitle:@"Person" action:@selector(newPerson:)];
UIMenuItem *companyMenuItem = [[UIMenuItem alloc] initWithTitle:@"Company" action:@selector(newCompany:)];
// NSAssert([self becomeFirstResponder], @"Sorry, UIMenuController will not work with %@ since it cannot become first responder", self);
menuController.menuItems = [NSArray arrayWithObjects: personMenuItem, companyMenuItem, nil];
[menuController setTargetRect:buttonFrame inView:self.view];
[menuController setMenuVisible:YES animated:YES];
[personMenuItem release];
[companyMenuItem release];
}
- (void) newPerson: (UIMenuController*) sender
{
//Add you logic
}
- (void) newCompany: (UIMenuController*) sender
{
//Add your logic
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
BOOL can = [super canPerformAction:action withSender:sender];
if (action == @selector(newPerson:) || action == @selector(newCompany:))
{
can = YES;
}
if (action == @selector(copy:))
{
can = NO;
}
return can;
}
- (BOOL) canBecomeFirstResponder {
return YES;
}
No comments:
Post a Comment