Selector method of UIButton not called during animation - ios

I have dynamically created buttons on a dynamic view and later I add that dynamic view to main view.
Everything is working well except that the selector method of UIButton is not called.
var baseView:UIView?
override func viewDidLoad() {
super.viewDidLoad()
randomizeButtons()}
func randomizeButtons(){
baseView = UIView(frame:CGRectMake(view.bounds.origin.x + 10, view.bounds.origin.y + 50, view.bounds.size.width, view.bounds.size.height-20))
let viewWidth = baseView.bounds.size.width;
let viewHeight = baseView.bounds.size.height;
baseView.userInteractionEnabled = true
let i = GlobalArrayofUsers.count
var loopCount = 0
while loopCount < i{
let userBaseView = UIView(frame: CGRectMake(abs(CGFloat(arc4random()) % viewWidth - 90) , abs(CGFloat(arc4random()) % viewHeight - 120), 90, 90))
print(userBaseView.frame)
userBaseView.backgroundColor = UIColor.redColor()
userBaseView.userInteractionEnabled = true
userBaseView.layer.cornerRadius = 45
userBaseView.clipsToBounds = true
let button = UIButton(frame: userBaseView.bounds)
button.backgroundColor = UIColor.greenColor()
button.showsTouchWhenHighlighted = true
button.addTarget(self, action: #selector(ViewController.handleTapEventofBtn(_:)), forControlEvents: .TouchUpInside)
button.setTitle("hello", forState: .Normal)
button.userInteractionEnabled = true
userBaseView.addSubview(button)
baseView.addSubview(userBaseView)
print("button frame is\(button.frame)")
baseView.bringSubviewToFront(userBaseView)
loopCount += 1
}
}
baseView.subviews.forEach { (users) in
UIView.animateWithDuration(1, delay: 0, options: [.Autoreverse,.Repeat], animations: {
users.center.y += 10
}, completion: nil)
}
view.insertSubview(baseView, atIndex: view.subviews.count)
}
func handleTapEventofBtn(sender: UIButton!) {
// handling code
}
Any idea why my selector method is not called? It is due to animation. How can i handle it.
SOLUTION: Allow userinteraction in UIAnimationOptions

The problem is that you are trying to click an object that is being animated. From the docs:
During an animation, user interactions are temporarily disabled for
the views being animated. (Prior to iOS 5, user interactions are
disabled for the entire application.)
But worry not, the solution is very easy, just call your animation with the option .AllowUserInteraction. For example (with your code):
UIView.animateWithDuration(1, delay: 0, options: [.Autoreverse,.Repeat,.AllowUserInteraction], animations: {
users.center.y += 10
}, completion: nil)

Have you tried this way:
for _ in 0...5{
// generate new view
let userBaseView = UIView(frame: CGRectMake(abs(CGFloat(arc4random()) % viewWidth - 90) , abs(CGFloat(arc4random()) % viewHeight - 120), 90, 90))
userBaseView.backgroundColor = UIColor.redColor()
// add button to view
let button = UIButton(frame: userBaseView.bounds)
button.backgroundColor = UIColor.yellowColor()
button.showsTouchWhenHighlighted = true
button.addTarget(self, action: #selector(self.handleTapEventofBtn(_:)), forControlEvents: .TouchUpInside)
button.setTitle("hello", forState: .Normal)
userBaseView.addSubview(button)
}
self.view.addSubview(userBaseView)
All you need is to create userBaseView before cycle, then create buttons in cycle and add them to userBaseView, after that just add userBaseView to self.view or where you need to add it. In your example you create userBaseView in every iteration of the loop.
I think the problem could be here:
userBaseView.addSubview(button)
// add new view containing button to self.view
self.view.addSubview(userBaseView)
}
userBaseView.addSubview(button)
baseView.addSubview(userBaseView)
First you add userBaseView to self.view in the loop:
// add new view containing button to self.view
self.view.addSubview(userBaseView)
then add it to baseView:
baseView.addSubview(userBaseView)
Can you post how it looks like in IB?

The action has a parameter so the selector signature is actually takeActionBtn:
(note the colon at the end).
You need to change that or the button will try to call a version of the function with no parameter which doesn't exist.
button.addTarget(self, action: "button:",forControlEvents: UIControlEvents.TouchUpInside)
func button(sender: UIButton!) {
// handling code
print("1")
}

// hi. your problem is in the selector line. try this:
for _ in 0...5 {
// generate new view
let userBaseView = UIView(frame: CGRectMake(abs(CGFloat(arc4random()) % self.view.frame.size.width - 90) , abs(CGFloat(arc4random()) % self.view.frame.size.height - 120), 90, 90))
userBaseView.backgroundColor = UIColor.redColor()
// add button to view
let button = UIButton(frame: userBaseView.bounds)
button.backgroundColor = UIColor.yellowColor()
button.showsTouchWhenHighlighted = true
button.addTarget(self, action: "buttonAction", forControlEvents: UIControlEvents.TouchUpInside)
button.setTitle("hello", forState: .Normal)
userBaseView.addSubview(button)
self.view.addSubview(userBaseView)
}
// tested with xcode 7.2
func buttonAction() {
print("working")
}

Related

Trying to call a navigation bar function in another class pragmatically

I created a navigation bar which I'm trying to call in another view controller. I set it up by calling the methods which I separated into left, center and right buttons. In my other controller I call the navbarcontroller and try and call the method for which i setup the navigation toolbar. Nothing happens, however there is no crash.
import UIKit
class NavBarController : UIViewController{
var screenSize: CGRect!
override func viewDidLoad() {
super.viewDidLoad()
setupNavigationBarItems()
setupToolBarItems()
self.navigationController?.isToolbarHidden = false
self.view!.backgroundColor = .white
}
and my method for the navigation bar is this
func setupNavigationBarItems() {
setupCenterNavButton()
setupLeftNavButton()
setupRightNavButton()
}
func showCalendarController() {
let navController = CalendarController()
self.present(navController, animated: true, completion: nil)
} //connect bottom bar buttons to controller
func showEventsController() {
let navController = EventsController()
self.present(navController, animated: true, completion: nil)
} //connect bottom bar buttons to controller
func setupNavigationBarItems() {
setupCenterNavButton()
setupLeftNavButton()
setupRightNavButton()
} // top bar button setup
private func setupCenterNavButton() {
let buttonFrame = UIView(frame: CGRect(x: 0, y: 0, width: 165,
height: 20))
mainFeedButton.frame = CGRect(x: 0,y: 0, width: 80,height: 20) as
CGRect
mainFeedButton.backgroundColor = UIColor.blue
peekFeedButton.frame = CGRect(x: 85,y: 0, width: 80,height: 20) as
CGRect
peekFeedButton.backgroundColor = UIColor.blue
buttonFrame.addSubview(mainFeedButton)
buttonFrame.addSubview(peekFeedButton)
navigationItem.titleView = buttonFrame
} //center bar buttons / action setup
private func setupLeftNavButton() {
navigationItem.leftBarButtonItem = UIBarButtonItem(customView:
favoriteButton)
}// left bar buttons / action setup
private func setupRightNavButton() {
navigationItem.rightBarButtonItem = UIBarButtonItem(customView:
moreButton)
} //right bar buttons / action setup
lazy var mainFeedButton: UIButton! = {
let button = UIButton(type: .custom) // button type
button.setTitle("Main",for: .normal) //button title
button.sizeToFit() // size button to fit the title
var frame = button.frame //create frame to manipulate the body
button.frame = CGRect(x: 0, y: 0, width: 100, height: 40)
button.addTarget(self, action:
#selector(self.showMainFeedController),
for: .touchUpInside)
return button
}() //mainFeed button connected to Feed Controller
lazy var peekFeedButton: UIButton! = {
let button = UIButton(type: .custom) //button type
button.setTitle("Spy",for: .normal) //button title
button.sizeToFit() // size button to fit the title
var frame = button.frame //create frame to manipulate the body
button.frame = CGRect(x: 20, y: 0, width: 100, height: 40)
button.addTarget(self, action:
#selector(self.showSpyFeedController),
for: .touchUpInside)
return button
}()//peekFeed button frame and action setup
lazy var favoriteButton: UIButton! = {
let button = UIButton(type: .system) //default button with blue
text
button.setImage(#imageLiteral(resourceName:
"star").withRenderingMode(.alwaysOriginal), for: .normal)
button.contentMode = .scaleAspectFit
button.frame = CGRect(x: 0, y: 0, width: 24, height: 24)
button.addTarget(self, action: #selector(favoriteButton_tapped),
for: .touchUpInside)
return button
}() //favorites button frame and action setup
lazy var moreButton: UIButton! = {
let button = UIButton(type: .system) //default button with blue
text
button.setImage(#imageLiteral(resourceName:
"more").withRenderingMode(.alwaysOriginal), for: .normal)
button.contentMode = .scaleAspectFit
button.frame = CGRect(x: 0, y: 0, width: 24, height: 24)
button.addTarget(self, action: #selector(moreButton_tapped),
for: .touchUpInside)
return button
}() //more button frame and action setup
func showMainFeedController() {
let navController = MainFeedController()
self.present(navController, animated: true, completion: nil)
} //mainFeed button connected to Feed Controller
func showSpyFeedController() {
let navController = SpyFeedController()
self.present(navController, animated: true, completion: nil)
}//peekFeed button connected to SpyFeedController
func favoriteButton_tapped(sender: UIButton) {
print("You touched this!")
}
func moreButton_tapped(sender: UIButton) {
print("You touched this!")
}
}
I then try and call the function by setupNavigationBarItems() like this
import UIKit
class EventsController: UIViewController{
override func viewDidLoad() {
super.viewDidLoad()
let navbar = NavBarController()
navbar.setupNavigationBarItems()
self.navigationController?.isToolbarHidden = false
self.view.backgroundColor = .white
}
}
I'm not sure if this a valid way. I'm still kinda new to all of this.
It's not clear what you expect to happen, but here's what does happen:
let navbar = NavBarController()
A completely new NavBarController object is created.
navbar.setupNavigationBarItems()
That NavBarController object's setupNavigationBarItems is called.
self.navigationController?.isToolbarHidden = false
self.view.backgroundColor = .white
Your code comes to an end. navbar was a local variable, so the NavBarController object vanishes in a puff of smoke. The end. This object was created and configured to no purpose.
I remember my first month in iOS way back 2015 :D, didn't have any knowledge in OOP, I didn't know too how to pass a data to another screen or class.
Anyways, you DO NOT create a new instance of your NavBarController class in your EventsController. If you want to talk to your NavBarController from your EventsController, then you will need a reference that is currently alive. You can also use delegate (search for that later).
So before you show or present your EventsController from your NavBarController, pass your current NavBarController instance to the next screen which is EventsController. BUT FIRST, you need to declare a variable in your EventsController, correct? :)
Declare a variable with a type of NavBarController inside your EventsController class, like so:
var navBarController: NavBarController!
Then in this piece of code of yours, pass your self (the NavBarController instance) to the EventsController class before showing or presenting, take note that you mistakenly gave a wrong name to your EventsController new instance, so I renamed it:
func showEventsController() {
let eventsController = EventsController()
eventsController.navBarController = self // THIS :)
self.present(eventsController, animated: true, completion: nil)
}
Lastly, instead of this:
let navbar = NavBarController()
navbar.setupNavigationBarItems()
Make use of your declared variable, like so:
self.navBarController.navbar.setupNavigationBarItems()
Hope this helps! :)

iOS - Adding controls on top of AVPlayerViewController

I have a subclass of AVPlayerViewController that I want to add my custom controls on it. I successfully added my custom controls on top of the AVPlayerViewController. However, these controls are only responsive for a short time. After a few seconds, clicking any of the button won't trigger the touchUpInside event.
Below is the code I'm using:
class LSPlayerViewController : AVPlayerViewController {
private var btnPlay : UIButton!
func addControls() {
self.showsPlaybackControls = false
//PLAY BUTTON
let btnPlayRect = CGRect(x: 0, y: 0, width: 19, height: 25)
btnPlay = UIButton(frame: btnPlayRect)
btnPlay.center = self.view.center
btnPlay.setImage(#imageLiteral(resourceName: "PauseIcon"), for: .normal)
btnPlay.addTarget(self, action: #selector(self.playButtonTapped(sender:)), for: .touchUpInside)
btnPlay.alpha = 0
btnPlay.isUserInteractionEnabled = true
self.view.addSubview(btnPlay)
}
func playButtonTapped(sender: UIButton!) {
if sender.isPlayIconOn == true {
sender.setImage(#imageLiteral(resourceName: "PauseIcon"), for: .normal)
self.player?.play()
} else {
sender.setImage(#imageLiteral(resourceName: "PlayIcon"), for: .normal)
self.player?.pause()
}
}
I solved the issue by adding the following two lines alongside the addSubview function:
self.addChildViewController(playerViewController)
playerView.addSubview(playerViewController.view)
playerViewController.didMove(toParentViewController: self)

Adding UIButton to AVPlayer in iOS (Swift) When Video Plays in WebView

I'm putting together an iOS app (using Swift) that contains a webview. A user will click on a video in the webview and the video will load, at which time I want to display a button on the video. I've been able to display the button but it is not responding to touches and I'm not sure why. I made the button huge just to make sure it was being hit. Here's my code. I extended AVPlayerViewController.
Thanks in advance for your time and patience.
extension AVPlayerViewController {
open override func viewDidLoad() {
let btn = UIButton(type: .roundedRect)
btn.backgroundColor = UIColor.red
btn.setTitle("Do stuff", for: .normal)
btn.frame = CGRect(x: 0, y: 330, width: 300, height: 300)
btn.addTarget(self, action: #selector(self.pressButton(button:)), for: .touchUpInside)
btn.isUserInteractionEnabled = true
btn.isEnabled = true
btn.clipsToBounds = true
self.contentOverlayView?.addSubview((btn as? UIButton)!)
self.showsPlaybackControls = false
}
func pressButton(button: UIButton) {
print("Worked")
}
}
In your extension of AVPlayerViewController, you have overridden viewDidLoad method, which is not correct in my view. Instead you can add the button adding logic in the extension and call that method eherever ypu have initialized the AVPlayerViewController.
extension AVPlayerViewController {
func addButton() {
let btn = UIButton(type: .roundedRect)
btn.backgroundColor = UIColor.red
btn.setTitle("Do stuff", for: .normal)
btn.frame = CGRect(x: 0, y: 330, width: 300, height: 300)
btn.addTarget(self, action: #selector(self.pressButton(button:)), for: .touchUpInside)
btn.isUserInteractionEnabled = true
btn.isEnabled = true
btn.clipsToBounds = true
self.contentOverlayView?.addSubview((btn as? UIButton)!)
self.showsPlaybackControls = false
}
func pressButton(button: UIButton) {
print("Worked")
}
}

addTarget not working for UIButton created in Swift

So, I've created a UIButton using Swift, and I animate it and it's superview using the following code:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//create a UIView to contain buttons. Set background color to nil, so it's transparent.
var controlsView = UIView(frame: CGRect(x:0, y:0, width:400, height:400));
controlsView.alpha = 1.0
controlsView.backgroundColor = nil
//create a button and add some attributes. Self explanatory
var loginButton: UIButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
loginButton.frame = CGRectMake(20.0, 40.0, 200, 60)
loginButton.alpha = 0.0
loginButton.setTitle("Login", forState: .Normal)
loginButton.titleColorForState(.Normal)
loginButton.layer.cornerRadius = 20;
loginButton.layer.borderWidth = 1.0
loginButton.layer.borderColor = CGColorCreate(CGColorSpaceCreateDeviceRGB(), [0.827, 0.325, 0.0, 1.0])
loginButton.addTarget(self, action: "loginPressed:", forControlEvents: .TouchUpInside)
//add the button to the view created to hold it
controlsView.addSubview(loginButton)
//now test blurring the background view. Create a UIVisualEffectView to create the blur effect. New in iOS7/8
/*
var visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .Light)) as UIVisualEffectView
visualEffectView.frame = self.introImage.bounds
self.introImage.addSubview(visualEffectView)
visualEffectView.alpha = 0.0
*/
//add the controls subview to the image view already here.
self.introImage.addSubview(controlsView)
//loginButton.addTarget(self, action: "loginButtonWasPressed:",forControlEvents:.TouchUpInside)
UIView.animateWithDuration(0.7,
animations: {
loginButton.alpha = 1.0
loginButton.frame = CGRect(x: 20.0, y: 100.0, width: 200, height: 60)
//visualEffectView.alpha = 0.5
},
completion:nil)
}
That's working fine. Then I created a function which should be called on touchUpInside of the button I created above:
#IBAction func loginPressed(sender:UIButton){
var alertView = UIAlertView();
alertView.addButtonWithTitle("Ok");
alertView.title = "title";
alertView.message = "message";
alertView.show();
}
When I touch the button, the function DOES NOT fire. Can anyone help? I'm testing this on an actual device (iPhone 6 Plus/iOS 8.1.1
To add an action to a button, you need to use the following code
loginButton.addTarget(self,action:#selector(loginPressed),
forControlEvents:.TouchUpInside)
This code is working fine for me.
For more detail you can follow this [Attach parameter to button.addTarget action in Swift
Thanks.

exc_bad_access on uibutton click using Xcode 6 and Swift language

I am trying to add another sub view on click of button.
First view is created using storyboard and button action is working fine.
But when I add new viewcontroller's view as subview and click on button, application crashes with exc_bad_access on iPhone Simulator.
here is the code:
override func viewDidLoad() {
super.viewDidLoad()
let datePick : UIDatePicker = UIDatePicker(frame: CGRectMake(0, 0, 218, 216))
datePick.backgroundColor = UIColor.whiteColor()
datePick.layer.borderColor = UIColor.blackColor().CGColor
datePick.layer.borderWidth = 5
let actionView : UIView = UIView(frame: CGRectMake(0, 216, datePick.frame.width, 50))
actionView.backgroundColor = UIColor.blackColor()
self.view.addSubview(datePick)
var cancelButton : UIButton = UIButton.buttonWithType(UIButtonType.System) as UIButton
cancelButton.frame = CGRectMake(datePick.frame.width-80, 5, 60, 40)
cancelButton.setTitle("Cancel", forState: UIControlState.Normal)
cancelButton.backgroundColor = UIColor.redColor()
cancelButton.addTarget(self, action: "cancelPressed:", forControlEvents: UIControlEvents.TouchUpInside)
actionView.addSubview(cancelButton)
var doneButton = UIButton.buttonWithType(UIButtonType.System) as UIButton
doneButton.frame = CGRectMake(20, 5, 60, 40)
doneButton.backgroundColor = UIColor.greenColor()
doneButton.setTitle("Done", forState: .Normal)
doneButton.addTarget(self, action: "buttonAction:",forControlEvents:UIControlEvents.TouchUpInside)
self.view.addSubview(actionView)
actionView.addSubview(doneButton)
// datePicker.frame = CGRectMake(0, 0, 218, 216);
// Do any additional setup after loading the view, typically from a nib.
}
func buttonAction(sender:UIButton!)
{
println("Button tapped")
}
func cancelPressed(sender: UIButton!) {
println("Cancel Clicked!!!")
}
IBAction Method called from first viewcontroller's view is :
#IBAction func dateSelectClicked (sender:AnyObject){
var datePicker = DatePicker()
datePicker.view.frame = CGRectMake(0, self.view.frame.height, self.view.frame.width, self.view.frame.height)
let isfinished : Bool = true
self.view.addSubview(datePicker.view)
UIView.animateWithDuration(0.5, delay: 1.0 , options: UIViewAnimationOptions.CurveEaseInOut, animations: { datePicker.view.frame = CGRectMake(0, self.view.frame.height-266, self.view.frame.width, self.view.frame.height)}, completion: {(isfinished) in println("finished")})
}
Change this 2 lines from:
self.view.addSubview(actionView)
actionView.addSubview(doneButton)
to:
actionView.addSubview(doneButton)
self.view.addSubview(actionView)
Finally I found the problem :
As provided in question, DatePicker viewcontroller was adding as :
var datePicker = DatePicker()
on click of button.(func dateSelectClicked)
So this is the problem with creating instance of DatePicker.
We need to create it as property outside method as :
Class firstViewController :UIViewController {
var datePicker = DatePicker()
}
and use as property in method self.datepicker

Resources