UIButton inside UIContainerView triggered in ParentView - ios

In my containerView I placed 2 Buttons (check and cross)
I would like to access them from my ParentViewController.
The Container also got a gestureRecognizer on it.
I got the outlets going and the buttons perusable, but nothing seems to happen. Any ideas?

Got the answer myself by now:
Made two outlets of the buttons + added the Target after that
checkButton.addTarget(singleModeVC(), action: "checkButton_click:", forControlEvents: UIControlEvents.TouchUpInside)
closeButton.addTarget(singleModeVC(), action: "closeButton_click:", forControlEvents: UIControlEvents.TouchUpInside)

Related

iOS11 UIBarButtonItem action not get called

I used Xcode9 Beta6 to build the project, the action was called correctly on iOS10 device, however it is not work on iOS11 device.
In My project, there are some viewControllers have a UIToolBar on the top, and the toolBar contains some UIBarButtonItems.
There is one this kind of viewController, whose UIBarButtonItem action is not called when I tap the UIBarButtonItem. I can see the tapping animation (the icon become dim first and back to normal after finger released)
At the end of viewDidLoad, I print the info of toolbar.items to indicate that target action are set properly.
Debug Output
In my case I was setting up the button and instantiating it as a property of the vc
class myVC: UIViewController {
let closeBarButtonItem = UIBarButtonItem(image: image, style: .plain, target: self, action: #selector(close(_:)))
}
If I moved this to ViewDidLoad it resolved the problem
class myVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let closeBarButtonItem = UIBarButtonItem(image: image, style: .plain, target: self, action: #selector(close(_:)))
}
}
I solved this problem by removing a current gesture recognizer from my view and adding a new one. Than I opened the connections inspector of my view and add gestureRecognizer connection to my gesture recognizer.
Apple has confirmed this bug.
My temporary solution is changing the gesture recognizer area by removing the overlap area, so that the tap gesture won't block the tap event on UIBarButtonItem.
It is happening only for iOS 11 and when custom UIView used for rightBarButtonItem (or left also). If you are using UIBarButtonItem then it will work fine.
There is 0 width of this custom bar item, so we need to set it to something.
In viewDidLoad of this controller you can add code, you can replace 100 to something what will work for you:
if let view = self.navigationItem.rightBarButtonItem?.customView {
view.widthAnchor.constraint(equalToConstant: 100).isActive = true
}
At least as an easy temporary solution it is fine.
In my case the problem was a gestureRecognizer added to the whole main view (in order to close the keyboard) like this:
UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(closeKeyboard)];
[self.view addGestureRecognizer:gesture];
That gesture recognizer overrides the tap on the UIBarButtonItem, thus I solved by creating a new subview placed immediately below the navigation bar and assigning the gesture recognizer to that view and not to the whole main view.
Mark the method you are targeting at with #objc.

Custom cell in UITableView (iOS 9)

I have simple tableview with custom cell that contains uibutton. After update to iOS 9 GM or 9.1 Beta 1(it doesn't matter) button on cell stopped to response on touch. I have also create IBAction method inside cell class and it's also doesn't work at all
cell.username.setAttributedTitle(attrNameString, forState: .Normal)
cell.username.tag = notify.likeObject.likedUserId.integerValue
cell.username.addTarget(self, action: "showProfile:", forControlEvents: .TouchUpInside)
OK, I found a fix. The issue when I am creating cell from XIB. So I added self.contentView.userInteractionEnabled = false to cell class and it's solved the issue

How to keep UIButton highlighted until a second touch?

I want to keep a button in highlighted state image until a second touch to release it to the normal state.
I've try the dispatch_async method, but it simply couldn't be back to normal state after another click.
(I'm coding in Swift so performSelector:WithObject method doesn't work either.)
I will use selected state instead of highlighted. UIButton has already the property so you don't need to create any other property.
button.setImage(image, forState: UIControlState.Normal)
button.setImage(selectedImage, forState: UIControlState.Selected)
button.addTarget(self, action: "buttonTapped:", forControlEvents: UIControlEvents.TouchUpInside)
func buttonTapped(sender:UIButton)
{
sender.selected = !sender.selected;
}
The best solution is extend the UIButton class, add the "highlited" BOOL flag. After each click just update this flag and set different image.

Image clicked button issue in Swift

How actually to make sure my button change background image when user click on it?
My code
btn1.setImage(UIImage(named:"dialpad1.png"),forState:UIControlState.Normal)
btn1.setImage(UIImage(named:"dialpad2.png"),forState:UIControlState.Highlighted)
My background image
dialpad1.png
dialpad2.png
My setting
My screen
Try using a "Custom" type in your UIButton instead of "System".
You can change it from IB:
Register your button for touchdown
myButton.addTarget(self, action: "buttonTouchDownAction:", forControlEvents: UIControlEvents.TouchDown)
Register your button for touchupinside
myButton.addTarget(self, action: "buttonTouchUpInsideAction:", forControlEvents: UIControlEvents.TouchUpInside)
func buttonTouchDownAction(sender:UIButton!)
{
//set highlighted image
}
func buttonTouchUpInsideAction(sender:UIButton!)
{
//set unhighlighted image
}
Thanks Midhun MP, your answer is most correct. I already follow what you suggested. I change State Config to Highlighted and select image in Highlight Background Image. No coding at all..

How can i navigate to mainviewcontroller from subview

I have just started working with swift, i have created a subview which has a button on it, i would like to use that button to take me to my mainviewcontroller.
i have used same functionality for a different button however having a function in same file allows that button to work the code is below
var playAgainButton = UIButton(frame: CGRectMake(0, 150, 320, 40))
playAgainButton.setTitle("Play Again", forState: UIControlState.Normal)
playAgainButton.addTarget(self, action: Selector("startGame"), forControlEvents: UIControlEvents.TouchUpInside)
playAgainButton.backgroundColor = UIColor.redColor()
gameOver.addSubview(playAgainButton)
is it possible to use similar code to navigate to a different viewcontroller?
Thanks
Are you using storyboards, or is this all in code? If it's in code, create a method like
func goToMainVC() {
if let navController = self.navigationController {
navController.popToRootViewControllerAnimated(true)
}
}
and set the button's target to a selector that calls it.
If you're using storyboards, you have three options:
ctrl+drag a connection from your button back to the main view controller (easy, but bad form because it just pushes the main VC back onto the nav controller);
Add #IBAction before the fun goToMainVC() method above, then ctrl+drag a connection from your button to the view controller in which it's contained, and then select that outlet method (this is how most people would do it); or
The best option is to use an unwind segue, as described here.

Resources