12

I found a question most similar to mine however it was not thoroughly answered. It would be great if anyone could help me out. The place where I found the similar question is here.

I understand clearly how func test() would be "test" and test(object:AnyObject) would be "test:". Thanks to this.

So how about 2 parameters? Can swift not do 2 arguments for 'action:'?

func popoverSelectedCode (code:AnyObject, desc:AnyObject)

I tried testing it out in few different ways below, but to no avail:

action: "popoverSelectedCode:,"

action: "popoverSelectedCode:,:"

action: "popoverSelectedCode: :"

action: "popoverSelectedCode: , :"

Am only about a week's old in Swift so please be kind.

EDITED Here is a short clip of the code

@IBAction func securityQuestButtonClicked (sender:AnyObject)
{
    cellButton = sender as? UIButton;

    var comboDescListArray = TableRoutine.loadCombobox("MobileQuestion")

    var codeObject : NSArray = comboDescListArray[0] as NSArray;
    var descObject : NSArray = comboDescListArray[1] as NSArray;

    var selectionTVC = CPSelectionTVC(style:UITableViewStyle.Plain, codeArray:codeObject, descArray:descObject, target:self, action: "popoverSelectedCode::", widthForViewInPopover:650)

    let navCtl = UINavigationController.init(rootViewController:selectionTVC)
    popoverController = UIPopoverController.init(contentViewController:navCtl)

    var contentHeight : CGFloat = CGFloat (UInt(selectionTVC.navigationTitleHeight) + UInt(selectionTVC.rowCount()) * UInt(selectionTVC.cellHeight))

    popoverController?.popoverContentSize = CGSizeMake(400.0, contentHeight)
    popoverController?.presentPopoverFromRect(sender.bounds, inView:sender as UIView, permittedArrowDirections:UIPopoverArrowDirection.Up, animated:true)
}

The popover appears however upon selecting, it just hangs.

Community
  • 1
  • 1
Rach Ng
  • 123
  • 1
  • 1
  • 10
  • Did you get an answer ? i am dealing with the same problem. If so how did you write your selector with multiple parameters, and how does the action function looks like. thanks. – Koray Birand Apr 27 '15 at 14:49
  • Unfortunately no. I was busy with other things, but I spent the whole day today trying to search for new answers. Do keep me posted if you happen to come across an answer. – Rach Ng Apr 29 '15 at 07:57

7 Answers7

11

Just a note on Swift 2.2. You can now type the selector as

#selector(popoverSelectedCode(_:desc:)
Erik Johansson
  • 1,188
  • 1
  • 8
  • 22
  • TY! still getting used to new selector. Just In case it helps anyone else I was able to use selector to call a delegate method like so in Swift 2.2 `textField.delegate?.performSelector(#selector(textField(_:shouldChangeCharactersInRange:replacementString:)))` – NSGangster May 04 '16 at 13:56
2

check this below code

func switchCard(card: Int, withCard card1: Int) {
    print(card)
}
let singleTap1 = UITapGestureRecognizer(target: self, action: "switchCard:withCard:")
Narendra G
  • 491
  • 4
  • 9
  • While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – JAL Sep 29 '15 at 20:32
2

In Swift 2.2 you can do something like this.

myUISlider.addTarget(self, action:#selector(self.onSliderValChanged(_:withEvent:)), forControlEvents: .ValueChanged)

func onSliderValChanged(slider: UISlider, withEvent event: UIEvent?) {
    }

I hope it helps someone out there.

Hanny
  • 1,322
  • 15
  • 20
1

With two parameters it becomes action::. One colon for one argument.

mustafa
  • 15,254
  • 10
  • 48
  • 57
  • Could you elaborate or perhaps give an example? – Rach Ng Jan 13 '15 at 07:17
  • There is nothing to elaborate. You supplied popoverSelectedCode function as action for some target. In swift you can turn it a selector like this. "popoverSelectedCode::". Two colon says this action takes two parameters. All this inspecting/resolving performed in runtime by the objective-c runtime – mustafa Jan 13 '15 at 07:22
  • I tried as you have suggested but unfortunately it still does not pass into the action function at all. – Rach Ng Jan 13 '15 at 07:26
  • What is the source of this action ? – mustafa Jan 13 '15 at 07:28
1

If the function has two parameters like below.

func clicked(sender:AnyObject,value:AnyObject)
{
}

Then

action = "clicked::"
rakeshbs
  • 24,392
  • 7
  • 73
  • 63
  • hi rakeshbs, yeap! I tried that as shown in my edited part. But problem is, the popover wont move. It just hangs there in the selected state. – Rach Ng Jan 13 '15 at 08:22
  • I have pasted the entire function as you requested. I did not see the need to paste the action function as its not even passing through. – Rach Ng Jan 13 '15 at 08:50
  • var selectionTVC = CPSelectionTVC(style:UITableViewStyle.Plain, codeArray:codeObject, descArray:descObject, target:self, action: "popoverSelectedCode::", widthForViewInPopover:650) – Rach Ng Jan 13 '15 at 09:12
  • is `CPSelectionTVC` a `UITableViewController`? – rakeshbs Jan 13 '15 at 09:45
0

Here is the way you can achieve it in your case:

action: "popoverSelectedCode:desc:"
Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
0

For multiple arguments passes in selector method:

let swipeRecognizer = UISwipeGestureRecognizer.init(target: self, action: #selector(ImageViewController.handleTap(_: sender :)))
myCell.imageViewPhoto .addGestureRecognizer(swipeRecognizer)
Pang
  • 9,564
  • 146
  • 81
  • 122
Parth
  • 634
  • 8
  • 15