switch statements in swift using buttons - swift

i cant figure out how to use my switch statement in swift so that when i click either button it will execute the println(). each time i click either button it just executes the default.
class ViewController: UIViewController{
var addOne : UIButton = UIButton(frame: CGRectMake(0, 0, 40, 40))
var addFive : UIButton = UIButton(frame: CGRectMake(0, 0, 40, 40))
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
addOne.setTitle("+1", forState: UIControlState.Normal)
addOne.backgroundColor = UIColor.blackColor()
addOne.center = (CGPointMake(40, 250))
addOne.addTarget(self, action: "player", forControlEvents: UIControlEvents.TouchUpInside)
view.addSubview(addOne)
addFive.setTitle("+5", forState: UIControlState.Normal)
addFive.backgroundColor = UIColor.blackColor()
addFive.center = (CGPointMake(200, 250))
addFive.addTarget(self, action: "player", forControlEvents: UIControlEvents.TouchUpInside)
view.addSubview(addFive)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func player(){
switch (true){
case addOne:
println("goodbye")
case addFive:
println("hello world")
default:
println("hello")
}
}
}

Change This:
addOne.addTarget(self, action: "player", forControlEvents: UIControlEvents.TouchUpInside)
To This:
addOne.addTarget(self, action: "player:", forControlEvents: UIControlEvents.TouchUpInside)
And This:
addFive.addTarget(self, action: "player", forControlEvents: UIControlEvents.TouchUpInside)
To This:
addFive.addTarget(self, action: "player:", forControlEvents: UIControlEvents.TouchUpInside)
Note the addition of the colon in "player:"
Next, change This:
func player(){
switch (true){
case addOne:
println("goodbye")
case addFive:
println("hello world")
default:
println("hello")
}
}
To This:
func player(sender: UIButton){
switch (sender){
case addOne:
println("goodbye")
case addFive:
println("hello world")
default:
println("hello")
}
}

Use switch-case statement for applying different states of player with using of UIButton Action.
// --------------------------------------------------------
// MARK:- UIButton Action
// --------------------------------------------------------
#IBAction func onTapPauseBtn(_ sender: Any) {
switch self.player.playbackState {
case .stopped:
self.player.playFromBeginning()
break
case .paused:
self.player.playFromCurrentTime()
btnPause.setImage(UIImage(named: ""), for: .normal)
break
case .playing:
self.player.pause()
btnPause.setImage(UIImage(named: "btn_play_video"), for: .normal)
break
case .failed:
self.player.pause()
break
}
}

Related

Cannot attach existing method to UIButton

I'm attempting to make a mock-up application, but I cannot seem to get the UIButton to hook up to the action that should be executed when I click it.
I'm not sure why it cannot find the method, the names are the same and adding in a (_:) to the #selector does nothing.
import UIKit
class LoginViewController: UIViewController {
var loginButton: UIButton?
#objc func onLoginButtonPress(sender: UIButton!) {
print("Login Button Press")
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
setup()
}
func setup() -> Void {
initLoginButton()
self.view.backgroundColor = .green
}
func initLoginButton() -> Void {
let loginButtonBounds = CGRect(x: 100, y: 100, width: 250, height: 250)
loginButton = UIButton(frame: loginButtonBounds)
loginButton!.backgroundColor = .red
loginButton!.setTitle("Login", for: .normal)
loginButton!.addTarget(self, action: #selector("onLoginButtonPress"), for: .touchUpInside)
self.view.addSubview(loginButton!)
}
loginButton!.addTarget(self, action: #selector(onLoginButtonPress(sender:)), for: .touchUpInside)
edit this line code, this should work for you. Its better to not use force-unwrapping so avoid using it.
Have you tried removing the parenthesis from "onLoginButtonPress"? I don't think they are needed.
loginButton!.addTarget(self, action: #selector("onLoginButtonPress"), for: .touchUpInside)

Swift 3 - Floating Button over UICollectionView

I created a floating button by this first answer. It works but when the UICollectionViewlaunched, the floating button still square and became circle after all datas appear (after loadAPI finished running).
Here's my code:
override func viewDidLoad() {
super.viewDidLoad()
self.roundButton = UIButton(type: .custom)
self.roundButton.setTitleColor(UIColor.orange, for: .normal)
self.roundButton.addTarget(self, action: #selector(self.ButtonClick(_:)), for: UIControlEvents.touchUpInside)
self.view.addSubview(self.roundButton)
self.loadAPI(Page: 1)
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
roundButton.layer.cornerRadius = roundButton.layer.frame.size.width/2
roundButton.backgroundColor = green
roundButton.clipsToBounds = true
roundButton.setImage(UIImage(named:"ic_add_white_2x"), for: .normal)
roundButton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
roundButton.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
roundButton.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -20),
roundButton.widthAnchor.constraint(equalToConstant: 60),
roundButton.heightAnchor.constraint(equalToConstant: 60)
])
}
#IBAction func ButtonClick(_ sender: UIButton){
//print("clicked")
}
I need the button being a circle since the UICollectionView first appears. Can somebody help me please? Thanks!
// update this code
override func viewDidLoad() {
super.viewDidLoad()
self.roundButton = UIButton(type: .custom)
self.roundButton.setTitleColor(UIColor.orange, for: .normal)
self.roundButton.layer.cornerRadius = roundButton.layer.frame.size.width/2
self.roundButton.addTarget(self, action: #selector(self.ButtonClick(_:)), for: UIControlEvents.touchUpInside)
self.view.addSubview(self.roundButton)
self.loadAPI(Page: 1)
}

Button to reload UIWebView

I have created a button over top of a UIWebView that I would like to use to refresh the webview when pressed. The following code is in my ViewController class:
#IBOutlet weak var webView: UIWebView!
override func shouldAutorotate() -> Bool {
return false
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Landscape
}
override func viewDidLoad() {
super.viewDidLoad()
let webView:UIWebView = UIWebView(frame: CGRectMake(30, 20, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height))
let button = UIButton(type: .System)
webView.scalesPageToFit = true
webView.multipleTouchEnabled = true
webView.backgroundColor = UIColor.whiteColor()
webView.loadRequest(NSURLRequest(URL: NSURL(string: "http://www.example.com")!))
self.view.addSubview(webView)
button.frame = CGRectMake(20, -20, 100, 100)
button.setTitle("Refresh", forState: UIControlState.Normal)
button.addTarget(self, action: Selector(reload()), forControlEvents: UIControlEvents.TouchUpInside)
webView.insertSubview(button, aboveSubview: webView)
}
func reload() {
webView.reload()
}
I have created the reload method to refresh the webView but nothing happens when I click the button. I also tried to create the reload method inside viewDidLoad to see if this fixed the issue. What could be causing this issue?
action: Selector(reload())
The syntax is wrong. On Xcode version 7.2 and below it should be
action: Selector("reload")
on Xcode 7.3 and up it should be
action: #selector(ClassName.funcName)
In Xcode 7.2 whole thing should be
let webView:UIWebView = UIWebView(frame: CGRectMake(30, 20, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height))
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton(type: .System)
webView.scalesPageToFit = true
webView.multipleTouchEnabled = true
webView.backgroundColor = UIColor.whiteColor()
webView.loadRequest(NSURLRequest(URL: NSURL(string: "http://www.example.com")!))
self.view.addSubview(webView)
button.frame = CGRectMake(20, -20, 100, 100)
button.setTitle("Refresh", forState: UIControlState.Normal)
button.addTarget(self, action: Selector("reload"), forControlEvents: .TouchUpInside)
webView.insertSubview(button, aboveSubview: webView)
}
func reload() {
webView.reload()
}
You defined webview inside view did load scope, take it out:
let webView:UIWebView = UIWebView(frame: CGRectMake(30, 20, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height))
func viewDidLoad() {
super.viewDidLoad()
let button = UIButton(type: .System)
webView.scalesPageToFit = true
webView.multipleTouchEnabled = true
webView.backgroundColor = UIColor.whiteColor()
webView.loadRequest(NSURLRequest(URL: NSURL(string: "http://www.example.com")!))
webView.layer.zPosition = 1
self.view.addSubview(webView)
button.frame = CGRectMake(0, 0, 100, 100)
button.setTitle("Reload Page", forState: UIControlState.Normal)
button.addTarget(self, action: Selector(reload()), forControlEvents: UIControlEvents.TouchUpInside)
webView.insertSubview(button, aboveSubview: webView)
}
func reload() {
webView.reload()
}

Button triggers another view

Right now I have this:
lazy var Button: UIButton = {
let tb = UIButton()
tb.backgroundColor = UIColor.yellowColor()
tb.clipsToBounds = true
tb.layer.cornerRadius = 39
tb.addTarget(self, action: #selector(showButton), forControlEvents: .TouchUpInside)
tb.setBackgroundImage(UIImage(named: "buttonMenu"), forState: UIControlState.Normal)
return tb
}()
How can I make the button take me to another view or do other stuff like opening the camera?
PS: The showButton function doesn't do anything really.
So you have
tb.addTarget(self, action: #selector(showButton), forControlEvents: .TouchUpInside)
but you need
tb.addTarget(self, action: #selector(className.showButton), forControlEvents: .TouchUpInside)
func showButton(sender:UIButton) {
//Code
}
I have tried this in 7.2.1 Xcode
import UIKit
import CoreLocation
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let Button: UIButton = {
let tb = UIButton()
tb.frame = CGRectMake(0, 0, 100, 50)
tb.backgroundColor = UIColor.yellowColor()
tb.clipsToBounds = true
tb.layer.cornerRadius = 39
tb.addTarget(self, action: "showButton:", forControlEvents: .TouchUpInside)
// only change the action syntax like "action: "showButton:" "
return tb
}()
self.view .addSubview(Button)
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func showButton(sender:AnyObject?) {
//Code
}
}

Why is my UIButton's target never reached?

func setupNewBinderButton() {
let binderButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
binderButton.frame = CGRectMake(50, 384-25, 50, 50)
binderButton.setTitle("Add", forState: UIControlState.Normal)
binderButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
binderButton.addTarget(self, action: "addBinderTapped", forControlEvents: UIControlEvents.TouchUpInside)
binderButton.userInteractionEnabled = true
self.view.addSubview(binderButton)
}
func addBinderTapped() {
println("hi")
abort()
}
override func viewDidLoad() {
super.viewDidLoad()
setupNewBinderButton()
The breakpoint I set in this function doesn't get hit. The log doesn't get hit. The abort doesn't happen.
How do I make my UIButton respond to touches in Swift?
Use like this as it needs to be tied to IBAction
#IBAction func addBinderTapped(sender: UIButton)
{
}
Connect this action with your button by command+drag to storyboard button to tie this function.