Why Doesn't Swift 3 recognize me button tap? - swift

let playButton: UIButton = {
let button = UIButton()
let image = UIImage(named: "VideoIcon.png") as UIImage?
button.backgroundImage(for: .normal)
button.addTarget(self, action: #selector(pressBackButton(button:)), for: .touchUpInside)
button.setImage(image, for: .normal)
return button
}()
func pressBackButton(button: UIButton) {
print("test")
if let playVideoButtonURL = post?.videourl {
let player = AVPlayer(url: playVideoButtonURL as URL)
let playerLayer = AVPlayerLayer(player:player)
playerLayer.frame = CGRect(x: 100, y: 200, width: 100, height: 100)
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
self.layer.addSublayer(playerLayer)
player.play()
}
}
even if the video code is wrong it should still print test but it doesn't which is strange. I feel like something may be wrong with the selector but i currently have no idea what could be wrong with it.

access function or property in self outside of initializer.
class TestViewController: UIViewController {
let playButton: UIButton = {
let button = UIButton()
let image = UIImage(named: "VideoIcon.png") as UIImage?
button.backgroundImage(for: .normal)
button.setImage(image, for: .normal)
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
button.addTarget(self, action: #selector(pressBackButton(button:)), for: .touchUpInside)
}
}

Instead of doing:
let playButton: UIButton = {
...
...
return button
}()
which won't work (because you're attempting to create playButton at the wrong time -- which is before the view is loaded or before it's about to appear)
In your viewDidLoad or viewWillAppear, create the button, define the target and simply add it as a subview.
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton()
if let image = UIImage(named: "VideoIcon.png") as UIImage?
{
button.backgroundImage(for: .normal)
button.addTarget(self, action: #selector(pressBackButton(button:)), for: .touchUpInside)
button.setImage(image, for: .normal)
}
self.addSubview(button)
}
Adding the button as a subview increments the retain count (i.e. it won't go away), but if you want to keep it around as a property simply declare it as:
var playButton : UIButton?
and then set playButton = button at the end of viewDidLoad.

Related

#selector(didTapRegister) not in scope

When I run this code by #selector(didTapRegister) I get a didTapRegister is not in scope. I have a bigger application I am using it in and on one page it works and another it won't, well it won't work anywhere else.
I am stuck on figuring out why. Can anyone help solve it? seems to be an Objective-C issue possibly because I have same issues trying to make IBOutlets, I have tried searching for answers and I just don't have enough knowledge to understand why it isn't working, I've tried moving the code to different scopes and still nothing. with the IBOutlets it's saying IBOutlet property cannot have non-object type UIButton?.Type
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let Button = UIButton.init(type: .custom)
Button.setTitle("Chat", for: .normal)
Button.layer.backgroundColor = CGColor.init(gray: 0.0, alpha: 0.0)
Button.setTitleColor(.white, for: .normal)
Button.setTitleColor(.red, for: .highlighted)
Button.layer.borderWidth = 1
Button.layer.cornerRadius = 5
Button.layer.borderColor = UIColor.white.cgColor
Button.isHighlighted = false
Button.showsTouchWhenHighlighted = true
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: Button)
Button.addTarget(self, action: #selector(didTapRegister), for: .touchUpInside)
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.isTranslucent = true
func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Do any additional setup after loading the view.
}
}
}
Maybe try what he said but
Just use SwiftUI it is much better
#objc and all of that is fine but in SwiftUI you can just right click and add buttons
There 2 mistakes in the code.
You have to create #objc function for the button action
In your code move viewWillAppear method outside the viewDidLoad
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton.init(type: .custom)
button.setTitle("Chat", for: .normal)
button.setTitleColor(.white, for: .normal)
button.setTitleColor(.red, for: .highlighted)
button.layer.borderWidth = 1
button.layer.cornerRadius = 5
button.layer.borderColor = UIColor.white.cgColor
button.isHighlighted = false
button.showsTouchWhenHighlighted = true
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: button)
button.addTarget(self, action: #selector(didTapRegister), for: .touchUpInside)
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.isTranslucent = true
}
#objc func didTapRegister(_ sender: UIButton) {
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Do any additional setup after loading the view.
}
}
Note: button name start with lowercase.

I need help streaming an online radio http://www.vdee.org:8000/salinas . I have this code so far

I need to stream an online radio station to put into an ios app using swift. Mine is running but not playing.
var player:AVPlayer?
var playerItem:AVPlayerItem?
var playButton:UIButton?
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let url = URL(string: "http://www.vdee.org:8000/salinasMim")
let playerItem:AVPlayerItem = AVPlayerItem(url: url!)
player = AVPlayer(playerItem: playerItem)
let playerLayer=AVPlayerLayer(player: player!)
playerLayer.frame=CGRect(x:0, y:0, width:10, height:50)
self.view.layer.addSublayer(playerLayer)
playButton = UIButton(type: UIButton.ButtonType.system) as UIButton
let xPostion:CGFloat = 50
let yPostion:CGFloat = 100
let buttonWidth:CGFloat = 150
let buttonHeight:CGFloat = 45
playButton!.frame = CGRect(x: xPostion, y: yPostion, width: buttonWidth, height: buttonHeight)
playButton!.backgroundColor = UIColor.lightGray
playButton!.setTitle("Play", for: UIControl.State.normal)
playButton!.tintColor = UIColor.black
playButton!.addTarget(self, action: #selector(ViewController.playButtonTapped(_:)), for: .touchUpInside)
self.view.addSubview(playButton!)
}
#objc func playButtonTapped(_ sender:UIButton)
{
if player?.rate == 0
{
player!.play()
//playButton!.setImage(UIImage(named: "player_control_pause_50px.png"), forState: UIControlState.Normal)
playButton!.setTitle("Pause", for: UIControl.State.normal)
} else {
player!.pause()
//playButton!.setImage(UIImage(named: "player_control_play_50px.png"), forState: UIControlState.Normal)
playButton!.setTitle("Play", for: UIControl.State.normal)
}
}
I check your code. it's working fine with below VOD content and it should also work with DTV content.
https://cstr-vod.castr.io/vdafddac30977c11e9bdcb/71983cc5-f510-4b36-b77a-fc3d216e15d3/index.m3u8
http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8
I am not able to get response from your URL: http://www.vdee.org:8000/salinasMim
Please use this URL:http://www.vdee.org:8000/salinas
this DTV content is working fine please check. You can listen the audio as the content in audio only.
var player:AVPlayer?
var playerItem:AVPlayerItem?
var playButton:UIButton?
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
//https://cstr-vod.castr.io/vdafddac30977c11e9bdcb/71983cc5-f510-4b36-b77a-fc3d216e15d3/index.m3u8
//http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8
let url = URL(string: "http://www.vdee.org:8000/salinas")
let playerItem:AVPlayerItem = AVPlayerItem(url: url!)
player = AVPlayer(playerItem: playerItem)
let playerLayer=AVPlayerLayer(player: player!)
playerLayer.frame=CGRect(x:0, y:0, width:10, height:50)
self.view.layer.addSublayer(playerLayer)
playButton = UIButton(type: UIButton.ButtonType.system) as UIButton
let xPostion:CGFloat = 50
let yPostion:CGFloat = 100
let buttonWidth:CGFloat = 150
let buttonHeight:CGFloat = 45
playButton!.frame = CGRect(x: xPostion, y: yPostion, width: buttonWidth, height: buttonHeight)
playButton!.backgroundColor = UIColor.lightGray
playButton!.setTitle("Play", for: UIControl.State.normal)
playButton!.tintColor = UIColor.black
playButton!.addTarget(self, action: #selector(ViewController.playButtonTapped(_:)), for: .touchUpInside)
self.view.addSubview(playButton!)
}
#objc func playButtonTapped(_ sender:UIButton)
{
if player?.rate == 0
{
player!.play()
//playButton!.setImage(UIImage(named: "player_control_pause_50px.png"), forState: UIControlState.Normal)
playButton!.setTitle("Pause", for: UIControl.State.normal)
} else {
player!.pause()
//playButton!.setImage(UIImage(named: "player_control_play_50px.png"), forState: UIControlState.Normal)
playButton!.setTitle("Play", for: UIControl.State.normal)
}
}

How to change UIButton's image every time it is pressed

How can I make a UIButton change image from A to B and again B to A, every time it's pressed?
Not as .normal to .highlighted, but permanent.
I've tried the .highlighted method because I can't find the code for permanent, here's my code:
func ChangeButton() {
Button.setImage(A, for: .normal)
Button.setImage(B, for: .highlighted)
Thanks for mentions of setBackgroundImage to Xavier L. from the other answer.
Try the following code:
import UIKit
class ViewController: UIViewController {
#IBOutlet var button: UIButton!
var buttonActive = false
override func viewDidLoad() {
super.viewDidLoad()
button.setBackgroundImage(UIImage(named: "apple"), for: .normal)
}
#IBAction func buttonPressed() {
if buttonActive {
button.setBackgroundImage(UIImage(named: "apple"), for: .normal)
} else {
button.setBackgroundImage(UIImage(named: "pineapple"), for: .normal)
}
buttonActive = !buttonActive
}
}
And your storyboard should look like that:
N.B. On Stack Overflow you should perform some actions before asking. Try to include some code you tried.
I recommend changing the backgroundImage because it's scaled automatically, and the button text in front won't get covered.
If it's a custom button, within its initializer:
self.addTarget(self, action: #selector(*yourfunctoswitchimages*), for: .touchUpInside)
... and then that yourfunctoswitchimages can just check what the current background image is, and switch it accordingly.
If it's not a custom button, add a target to it in viewDidLoad or something. Here's a lot of sample code.
In your viewDidLoad()
yourButt.addTarget(self, action: #selector(switcheroo), for: .touchUpInside)
Another func in your view controller:
func switcheroo()
{
if yourButt.currentBackgroundImage == image1
{
yourButt.setBackgroundImage(image2), for: .normal)
}
else { // ...set image to image1 }
}
yourButt would be a property of your view controller of course, that way it's accessible to the viewDidLoad() and to other funcs within it.

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()
}

Swift custom back button in navigation bar

I am trying to use a custom image for my back button in the navigation bar. I am using the below code, which adds the image, but also keeps the text "Back" in the button. I want to also remove the text. Can I do that?
self.navigationController?.navigationBar.backIndicatorImage = UIImage(named: "icon-back")
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "icon-back")
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)
Try code below :-)
func SetBackBarButtonCustom()
{
//Back buttion
let btnLeftMenu: UIButton = UIButton()
btnLeftMenu.setImage(UIImage(named: "back_arrow"), for: UIControlState())
btnLeftMenu.addTarget(self, action: #selector(UIViewController.onClcikBack), for: UIControlEvents.touchUpInside)
btnLeftMenu.frame = CGRect(x: 0, y: 0, width: 33/2, height: 27/2)
let barButton = UIBarButtonItem(customView: btnLeftMenu)
self.navigationItem.leftBarButtonItem = barButton
}
func onClcikBack()
{
_ = self.navigationController?.popViewController(animated: true)
}
If you want to add Back button in every UIViewController then you can add the code in UIViewController extension else you can use addBackButton() directly as follows.
extension UIViewController {
func addBackButton() {
let btnLeftMenu: UIButton = UIButton()
let image = UIImage(named: "backButtonImage");
btnLeftMenu.setImage(image, for: .normal)
btnLeftMenu.setTitle("Back".localized, for: .normal);
btnLeftMenu.sizeToFit()
btnLeftMenu.addTarget(self, action: #selector (backButtonClick(sender:)), for: .touchUpInside)
let barButton = UIBarButtonItem(customView: btnLeftMenu)
self.navigationItem.leftBarButtonItem = barButton
}
func backButtonClick(sender : UIButton) {
self.navigationController?.popViewController(animated: true);
}
}
Make sure you should add the following file "backButtonImage.png" in your app bundle.
Call this method self.addBackButton()in your viewDidLoad method of your custom UIViewController class like this
override func viewDidLoad() {
super.viewDidLoad()
self.addBackButton()
}
Note : if you don't add addBackButton Method in extension then you will need to add this method directly in the class and set target and Selector accordingly.