1

I am very new to iOS. I am trying to show a pop up menu,but some how I am unable to display menu. Please help. Here is my code:

//DidLoad function to initialize UILongPRessGestureRecognizer

override func viewDidLoad()  
{     
        super.viewDidLoad()  
        var longprss : UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: Selector("display:"))   
        self.tableView.addGestureRecognizer(longprss)   
}



   //Bool function Can become first responder set to true

     override func canBecomeFirstResponder() -> Bool {
           return true
    }

 //become first responser set to true
   override func becomeFirstResponder() -> Bool {
        return true
    }
    //action handler of longPressGesture

    func display(gesture: UILongPressGestureRecognizer)
    {

        self.becomeFirstResponder()
        println("Is first responder")

        var menu = UIMenuController.sharedMenuController()
        var deleteItem = UIMenuItem(title: "Delete", action: Selector("deleteLine"))
        var editItems = UIMenuItem(title: "Edit", action: Selector("editrow"))
        menu.menuItems = [deleteItem ,editItems]

        menu.setTargetRect(CGRect(x: 100, y: 80, width: 100, height: 50), inView: UIView())
        menu.setMenuVisible(true, animated: true)

    }

//perform action method
override func tableView(tableView: UITableView, performAction action: Selector, forRowAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject!) {

}
//can perform action method.

    override func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool {
        if action == Selector("Delete")
        {
            return true
        }
        return false
    }

Please help me to display popup menu.

Thank you in advance.

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
Diamond King
  • 676
  • 10
  • 17

3 Answers3

2
//View controller class which is implementing TableViewCellDelegate
//Make sure you do followng
//subclass UITableViewCell using class name TableViewCell which has delegate
//you link UITableViewCell in ViewController class with TableViewCell in Custom Class column in interface builder(storyboard)
//In TableViewCell cell identifier you give name as Cell.


class ViewController: UITableViewController,TableViewCellDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.



    }

// tableview delegates
  override  func numberOfSectionsInTableView(tableView: UITableView) -> Int // Default is 1 if not
    {

        return 1;
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
     override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
     {

        var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as? TableViewCell


        if cell == nil {
            cell = TableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "Cell")
        }
        cell?.delegate=self;
        cell?.textLabel?.text="Cell text"
        return cell!

    }

     func tableCellSelected(tableCell: UITableViewCell)
     {

        var longprss : UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: Selector("display:"))
        tableCell.addGestureRecognizer(longprss)

     }
    //end tableview delegate


    // menu delegates
    override func canBecomeFirstResponder() -> Bool {
        return true
    }



    override func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool
    {
        println(action);
        if(action == Selector("deleteLine:") || action == Selector("editRow:"))
        {
            return true;

        }
        else
        {
            return false;
        }
    }

    func deleteLine(sender: AnyObject?) {

        println("delete line")

    }

    func editRow(sender: AnyObject?) {

        println("edit row");
    }

    //end menu delegate

    //action handler of longPressGesture

    func display(gesture: UILongPressGestureRecognizer)
    {

        gesture.view?.becomeFirstResponder()

        println("Is first responder")
        var menu = UIMenuController.sharedMenuController()
        var deleteItem = UIMenuItem(title: "Delete", action: Selector("deleteLine:"))
        var editItems = UIMenuItem(title: "Edit", action: Selector("editRow:"))
        menu.menuItems = [deleteItem ,editItems]

        menu.setTargetRect(CGRect(x: 30, y: 8, width: 100, height: 50), inView: gesture.view!)
        menu.setMenuVisible(true, animated: true)

    }



    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }




}

//This is subclass of UITableViewCell

protocol TableViewCellDelegate
{
    func tableCellSelected( var tableCell : UITableViewCell)
}


class TableViewCell: UITableViewCell {

    var delegate : TableViewCellDelegate?

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        if((self.delegate) != nil)
        {
            delegate?.tableCellSelected(self);
        }
        // Configure the view for the selected state
    }

}
Shashi3456643
  • 2,021
  • 17
  • 21
0
I have tested code for displaying menu in UIView Its working. You can do for UItablecell or uitableView
Do exactly the way I have done it will work
you don't need to add this delegate, remove it from your code
   override func becomeFirstResponder


example


 override func viewDidLoad()
{
    super.viewDidLoad()
    var longprss : UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: Selector("display:"))
    self.view.addGestureRecognizer(longprss)
}

override func canBecomeFirstResponder() -> Bool {
    return true
}



override func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool
{
    println(action);
    if(action == Selector("deleteLine:") || action == Selector("editRow:"))
    {
        return true;

    }
    else
    {
        return false;
    }
}

func deleteLine(sender: AnyObject?) {

    println("delete line")

}

func editRow(sender: AnyObject?) {

    println("edit row");
}


//action handler of longPressGesture

func display(gesture: UILongPressGestureRecognizer)
{

    gesture.view?.becomeFirstResponder()

    println("Is first responder")
    var menu = UIMenuController.sharedMenuController()
    var deleteItem = UIMenuItem(title: "Delete", action: Selector("deleteLine:"))
    var editItems = UIMenuItem(title: "Edit", action: Selector("editRow:"))
    menu.menuItems = [deleteItem ,editItems]

    menu.setTargetRect(CGRect(x: 100, y: 600, width: 100, height: 50), inView: self.view)
    menu.setMenuVisible(true, animated: true)

}
Shashi3456643
  • 2,021
  • 17
  • 21
  • Yes it is working, but the menu is displaying at fixed position not on the cell which is selected. Well, Thank you so much for the solution. – Diamond King Jan 31 '15 at 06:18
  • Thats why I asked you where would you like to display the menu. I understand you want to display in tablecellview. user will long press on tablecellview and menu will popup. I am going to post another answer which will have complete code. – Shashi3456643 Jan 31 '15 at 18:12
0

You override -[UIResponder becomeFirstResponder] but don't call its super implementation. This basically turns the call into a no-op, and fails to install you in the responder chain. Unless you need to perform specific actions when becoming the first responder you don't need to override this method.

axiixc
  • 1,962
  • 18
  • 25