John John - 2 months ago 10
iOS Question

How show popup menu list when clicking on UITextField iOS? (Kxmenu)

Sorry I am a little bit new on iOS Development , for a project i need to use popup list menu library ( i know iOS support PickerView , ... for the purpose of choosing from a list menu) but i need to use that popup list , so after some search i find this library :
https://github.com/kolyvan/kxmenu

Which i have seen the example on that project. I added same as kxmenu Example to my project and it works , but I need a slight change. Instead of clicking on Button (in the example) to open the popup menu , i want whenever user click inside UITextField , would open the popup menu.
The buttons are added as subview by this example , but i need to connect an action to the UITextField which is not written programatically (it's not added by adding subview...)

here is the code (of Example) that I really Appreciate if someone tell me what changes do i need to apply to the following code , to make this changes:


  1. instead of using the button , Use UITextfield , I mean when user click on The UITextField it would open the kxmenu popup

  2. please consider that in the kxmenu example , Button are added programatically , but the UITextField that i have is a property.



here is the part of the source code:

//#part 1 {

_btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
_btn1.frame = CGRectMake(5, 5, 100, 50);
[_btn1 setTitle:@"click Me" forState:UIControlStateNormal];
[_btn1 addTarget:self action:@selector(showMenu:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_btn1];


//#part 1 }

- (void) pushMenuItem:(id)sender
{
NSLog(@"%@", sender);
}


//#part 2 {

- (void)showMenu:(UIButton *)sender


//#part 2 }
{

NSArray *menuItems =

@[

[KxMenuItem menuItem:@"ACTION MENU 1234456"
image:nil
target:nil
action:NULL],


[KxMenuItem menuItem:@"Reload page"
image:[UIImage imageNamed:@"reload"]
target:self
action:@selector(pushMenuItem:)],

[KxMenuItem menuItem:@"Search"
image:[UIImage imageNamed:@"search_icon"]
target:self
action:@selector(pushMenuItem:)],

];

KxMenuItem *first = menuItems[0];
first.foreColor = [UIColor colorWithRed:47/255.0f green:112/255.0f blue:225/255.0f alpha:1.0];
first.alignment = NSTextAlignmentCenter;

[KxMenu showMenuInView:self.view
fromRect:sender.frame
menuItems:menuItems];
}


With My Slight iOS Knowledge i think these changes might apply (but i have tried that and it won't work with UITextField


  1. instead of //#part 1 shwon in above code , i need to replace it with :

    [currentTextField addTarget:self action:@selector(showMenu:) forControlEvents:UIControlEventTouchUpInside];

  2. instead of UIButton as declaration of argument of sender in //#part 2 shwon in above code , i need to change it UITextField as argument of sender.



I would appreciate if someone show me by writing some code what changes do i need to apply to above source code for the following purposes. Thanks!

Answer

you can use "UITextFieldDelegate", for example:

- ( BOOL )textFieldShouldBeginEditing:( UITextField *)textField
  [self taggle];
)

-(void)taggle{
    NSArray *menuItems =
    @[
      [KxMenuItem menuItem:@"开启闪光灯"
                     image:nil
                    target:self
                    action:@selector(clickTorch)],
      [KxMenuItem menuItem:@"翻转"
                     image:nil
                    target:self
                    action:@selector(clickCamera)],
      [KxMenuItem menuItem:@"美颜"
                     image:nil
                    target:self
                    action:@selector(clickBeautyFace)],

      ];
    [KxMenu showMenuInView:self.view
                  fromRect:textField.frame // UITextField *textField = [[UITextField alloc] initWithFrame:....] // you can write any frame and change it.
                 menuItems:menuItems];
}
Comments