Tapping UIButton doesn't call its action although visually it receives the event - ios

I have a view in a xib file that contains a button and other 2 views and they don't overlap. The view file's owner is a custom class derived from UIViewController. The custom view controller is created programmatically by this code:
let sfvc = SelezioneFascicoloViewController(nibName: "SelezioneFascicoloView", bundle: nil)
viewObject.addSubview(sfvc.view)
sfvc.updateWithAttributes(attrs, inFascicolo: self.fascicolo!)
viewObject is a view contained in a custom UICollectionViewCell.
The button has a handler connected to the touchDown event (but any other event produce the same problem) and it is defined in the custom UIViewController (SelezioneFascicoloViewController)
I have flagged showsTouchOnHighlight for the button just to see if it receive the event.
Well, the view shows up correctly, if I tap the button I can see the glow but the handler isn't called.
All the views in the hierarchy have the UserInteractionEnabled set to true, and their sizes are correct so the button isn't outside the superview frame.
Also I have a UITapGestureRecognizer in the UICollectionViewController derived class that contains the UICollectionViewCell, and its handler is fired if I tap outside the button, but not when I tap inside the button, and this is as it should be.
Can someone give me some advice for this kind of problem?
Edit: added a screenshot
Interface Builder screen shot

Thank beyowulf.
Everything works if I add SelezioneFascicoloViewController as a child view controller, in this way:
let sfvc = SelezioneFascicoloViewController(nibName: "SelezioneFascicoloView", bundle: nil)
self.addChildViewController(sfvc)
viewObject.addSubview(sfvc.view)
sfvc.didMoveToParentViewController(self)
sfvc.updateWithAttributes(attrs, inFascicolo: self.fascicolo!)

Related

Switch to another ContainerView from inside a ContainerView

I made an iOS app with Xcode and Swift.
One ViewController contains two ContainerViews. The user can switch between them with SegmentedControl.
But, how can I switch to the other ContainerView without the SegmentedControl, e.g. with a button?
Actually I have this code:
let vc : AnyObject! = self.storyboard!.instantiateViewControllerWithIdentifier("login")
self.showViewController(vc as! UIViewController, sender: vc)
This opens the other ContainerView, but as single VC, not inside the parent ViewController.
What can I do to reach the other ContainerView shown in the parent VC?
You must have to set the IBoutlets from ContainerViews viewControllers too to call them separately using uibuttons action.
assign same class to both parentViewcontroller and containerView using identity inspector see image.
connect IBOutlet as well like parentViewController see image
Now can access from class using uibutton action.
Iterate through your parent view controllers's child's like
parentViewController.childViewControllers.indexOf(self)
you got the index, means you can get the Controller then you can show it

How can I create a segue between a custom UITableViewCell button and a second view

I created a custom UITableViewCell in a separate nib, and inside it I have a button. I want the tableViewCell to segue to one view controller when the cell is tapped, but a different one when the button is tapped. The cell tap segue works fine, but I can't figure out how to setup a segue between the button in the cell and the next view. I have added a target to the button with
cell.imagePreview.addTarget(self, action: "segueToImageView:", forControlEvents: .TouchUpInside)
Then in that method
#IBAction func segueToImageView(sender: UIButton) {
performSegueWithIdentifier("ImageViewSegue", sender: self)
}
But obviously I end up with a "has no segue with identifier 'ImageViewSegue'" error. I tried connecting the button to the new view in my main.storyboard, but it won't let me drag between the nib and the storyboard. I also tried creating a segue in main.storyboard between the two views and calling it "ImageViewSegue" but I get the same error.
Is there a way to create a segue with identifier through code? If not, how should I go about segueing to the new view via the button?
Instead of a segue, just do this the old-fashioned way (from before there were storyboards and segues): set the button's action–target pair (in code, when you load) so that when the button is tapped, your action handler is called. In the action handler, do whatever the segue would do, i.e. instantiate the view controller from the storyboard and push or present it.

UITextField inputView - IBActions not fired

I want to replace the default keyboard of a UITextField with a custom keyboard. So I created a new subclass of a UIViewController with a xib-file (the other way like creating both files seperately and setting the File's Owner doesn't work either).
Then I added a button to the KeyboardView and connected it to an IBAction. After that I set the textfields inputView to the new view like this:
override func viewDidLoad() {
let keyboardVC = KeyboardViewController()
textField.inputView = keyboardVC.view
keyboardVC.view.autoresizingMask = .FlexibleHeight
keyboardVC.delegate = textField
}
It's working and the custom keyboard shows up, but if I touch the button, the IBAction is not called. What's the problem in my setup? (I checked some examples and they all do it the same way).
UPDATE:
I now removed the ViewController and subclassed the UIView. Now the actions are working. Why isn't it working with a ViewController?
Since no one holds the UIViewController- there is no reference to it after the viewDidLoad() ended, it is released from the memory.
When the button is pressed, the view controller that should response to the action is not exist -> you are holding only the view of the view controller as the textField.inputView.

Get a UIButton's tag from the UIButtons which are in the popover presented by this UIButton

The headline seems lengthy but what I'm trying to do is quite simple.
I have a couple of identical buttons lined in a row and set their tags to 0...n. Clicking on any of them (the 2nd for example) would bring up a popover view in which there are several buttons representing different options (A, B, C, D). What I want to do is to turn the 2nd Button's title to B if we click on option B.
The problem is that the popover view does not know which Button presented it, since all popoverViews are instances of the same UIViewController class. So I am thinking of distinguishing the n buttons by setting their tags to different values. However, I don't know how to get the UIButton's tag from a button inside the popover this UIButton presented.
Many thanks in advance!
This is how I will solve this in swift. I will declare a delegate in the popoverViewController and have a method e.g
protocol popOverViewControllerDelegate: class {
func popOverViewController(controller: PopOverViewController,
didSelectItem buttonTitle: String)
}
then I will add a target action to the UIButton in the popOver
button.addTarget(self, action: "selected:",forControlEvents:.TouchUpInside)
the select method will have sender passed to it
func selected(sender:UIButton){
delegate?.popOverViewController(self, didSelectItem: sender.currentTitle)
//dismiss viewcontroller
}
remember to declare
weak var delegate: popOverViewControllerDelegate!
now have the viewcontroller that called the popOver, subclass to this delegate and implement it's method. Whenever a button in the popOver is selected, this method will be called and the currentTitle of the method will be passed. You can now use that to set the currentTitle of the button that called the popOver.
In case you need further help with the last part, please ask.
I've fixed the problem by adding a property tagNumberso that after instantiating the popoverViewController's class, I set the instance's tagNumber to the sender's tag. Then I send the tagNumber back together with sender.currentTitle. This way, the presenter of the popover could know both the title and tag number of the UIButton.

iOS storyboards

I have a iOS single-view application for the iPad and using storyboards.
I have the default viewcontroller created by xcode (vc1). I
place a button on its view.
I then drag a new view controller (screen2VC) onto the
storyboard.
I also create a new view controller subclass (vc2SubClass)
I associate screen2VC to this subclass via the identity inspector.
On screen2VC's view, I have a label. I control-drag the label onto
vc2SubClass to create an outlet.
I create a segue between vc1's button and vc2's button.
In performSegue override i have the following code:
if ([segue.identifier isEqualToString:#"segue1"])
{
screen2VC* vc = [segue destinationViewController];
vc.label1.text = #"screen 2";
}
When I run the code and press the button, the segue works and transitions between the two views just fine, but screen2VC's label is never set to 'screen 2'. As a test, I placed the label1.text = #"screen 2"; code into vc2SubClass, but that made no difference. It seems there's still no association between screen2VC and vc2SubClass, even though the former is subclassing the latter and the label is pointing to a UILabel outlet.
Any ideas?
When you're in performSegue: of vc1, vc2's view hierarchy has not yet been set up. The normal way of doing this is to have a property in vc2 that temporarily is given the string during performSegue:. This property is then used to update the label in vc2's viewWillAppear:.
(When you say "As a test, i placed the 'label1.text = #"screen 2";' code in vc2SubClass but that makes no difference" I'm not sure what's going on there. What method was that code put into?)

Resources