Monday 10 June 2013

Tap Button to Show MenuController in Xcode

.h File:

@interface ViewController : UIViewController
{
    IBOutlet UIButton *btn1;
    IBOutlet UIButton *btn2;
    IBOutlet UIButton *btn3;
    IBOutlet UIButton *btn4;

    int btnnumber;
}

-(IBAction)btn1:(id)sender;
-(IBAction)btn2:(id)sender;
-(IBAction)btn3:(id)sender;
-(IBAction)btn4:(id)sender;

@end



.m File:

-(IBAction)btn1:(id)sender
{
    [self becomeFirstResponder];
    
    btnnumber = 1;
    
    UIMenuItem *menu1=[[UIMenuItem alloc]initWithTitle:@"Detail" action:@selector(Detail:)];
    UIMenuItem *menu2=[[UIMenuItem alloc]initWithTitle:@"Edit" action:@selector(Edit:)];
    UIMenuItem *menu3=[[UIMenuItem alloc]initWithTitle:@"Change Color" action:@selector(Color:)];
    UIMenuItem *menu4=[[UIMenuItem alloc]initWithTitle:@"Delete" action:@selector(Delete:)];
    UIMenuController *menu = [UIMenuController sharedMenuController];
    menu.menuItems=[NSArray arrayWithObjects:menu1,menu3,menu2,menu4, nil];
    [menu setTargetRect:CGRectMake(0, 0, 0, 0) inView:btn1];
    [menu setMenuVisible:YES animated:YES];
}

-(IBAction)btn2:(id)sender
{
    [self becomeFirstResponder];
    
    btnnumber = 2;
    
    UIMenuItem *menu1=[[UIMenuItem alloc]initWithTitle:@"Detail" action:@selector(Detail:)];
    UIMenuItem *menu2=[[UIMenuItem alloc]initWithTitle:@"Edit" action:@selector(Edit:)];
    UIMenuItem *menu3=[[UIMenuItem alloc]initWithTitle:@"Change Color" action:@selector(Color:)];
    UIMenuItem *menu4=[[UIMenuItem alloc]initWithTitle:@"Delete" action:@selector(Delete:)];
    UIMenuController *menu = [UIMenuController sharedMenuController];
    menu.menuItems=[NSArray arrayWithObjects:menu1,menu3,menu2,menu4, nil];
    [menu setTargetRect:CGRectMake(0, 0, 0, 0) inView:btn2];
    [menu setMenuVisible:YES animated:YES];
}

-(IBAction)btn3:(id)sender
{
    [self becomeFirstResponder];
    
    btnnumber = 3;
    
    UIMenuItem *menu1=[[UIMenuItem alloc]initWithTitle:@"Detail" action:@selector(Detail:)];
    UIMenuItem *menu2=[[UIMenuItem alloc]initWithTitle:@"Edit" action:@selector(Edit:)];
    UIMenuItem *menu3=[[UIMenuItem alloc]initWithTitle:@"Change Color" action:@selector(Color:)];
    UIMenuItem *menu4=[[UIMenuItem alloc]initWithTitle:@"Delete" action:@selector(Delete:)];
    UIMenuController *menu = [UIMenuController sharedMenuController];
    menu.menuItems=[NSArray arrayWithObjects:menu1,menu3,menu2,menu4, nil];
    [menu setTargetRect:CGRectMake(0, 0, 0, 0) inView:btn3];
    [menu setMenuVisible:YES animated:YES];
}

-(IBAction)btn4:(id)sender
{
    [self becomeFirstResponder];
    
    btnnumber = 4;
    
    UIMenuItem *menu1=[[UIMenuItem alloc]initWithTitle:@"Detail" action:@selector(Detail:)];
    UIMenuItem *menu2=[[UIMenuItem alloc]initWithTitle:@"Edit" action:@selector(Edit:)];
    UIMenuItem *menu3=[[UIMenuItem alloc]initWithTitle:@"Change Color" action:@selector(Color:)];
    UIMenuItem *menu4=[[UIMenuItem alloc]initWithTitle:@"Delete" action:@selector(Delete:)];
    UIMenuController *menu = [UIMenuController sharedMenuController];
    menu.menuItems=[NSArray arrayWithObjects:menu1,menu3,menu2,menu4, nil];
    [menu setTargetRect:CGRectMake(0, 0, 0, 0) inView:btn4];
    [menu setMenuVisible:YES animated:YES];
}

- (BOOL)canBecomeFirstResponder
{
    return YES;
}

-(void)Detail:(id)sender
{
    NSLog(@"Table Details");
    NSLog(@"%d",btnnumber);
}


-(void)Edit:(id)sender
{
    NSLog(@"Edit Table");
    NSLog(@"%d",btnnumber);
}


-(void)Color:(id)sender
{
    NSLog(@"Change Table Color");
    NSLog(@"%d",btnnumber);
    
    UIMenuItem *menu1=[[UIMenuItem alloc]initWithTitle:@"Default" action:@selector(Default:)];
    UIMenuItem *menu2=[[UIMenuItem alloc]initWithTitle:@"Red" action:@selector(Red:)];
    UIMenuItem *menu3=[[UIMenuItem alloc]initWithTitle:@"White" action:@selector(White:)];
    UIMenuItem *menu4=[[UIMenuItem alloc]initWithTitle:@"Yellow" action:@selector(Yellow:)];
    UIMenuController *menu = [UIMenuController sharedMenuController];
    menu.menuItems=[NSArray arrayWithObjects:menu1,menu3,menu2,menu4, nil];
    [menu setTargetRect:CGRectMake(0, 0, 0, 0) inView:btn1];
    [menu setMenuVisible:YES animated:YES];
}

-(void)Default:(id)sender
{
    NSLog(@"you have chossed default color");
}

-(void)Red:(id)sender
{
    NSLog(@"you have chossed Red color");
}

-(void)White:(id)sender
{
    NSLog(@"you have chossed White color");
}

-(void)Yellow:(id)sender
{
    NSLog(@"you have chossed Yellow color");
}

-(void)Delete:(id)sender
{
    NSLog(@"Delete Table");
    NSLog(@"%d",btnnumber);
    
}


xib File (Design):




OutPut:


Click the Btn1. Show the Options in MenuController

Click the change Color from Menu Controller




Choose the Color is White(Another MenuControler)


Click the Btn3. Show the Options Available in MenuController



No comments: