-1

I am learning Swift and iOS development, and I am just trying to figure out how to open an URL from a button click.

I found this answer: SwiftUI: How do I make a button open a URL in safari?

So I am trying to incorporate "Link" into my code below:

class ViewController: UIViewController {
    private let visitwebsitebutton: UIButton = {
    let visitwebsitebutton = UIButton()
    visitwebsitebutton.backgroundColor = .gray
    visitwebsitebutton.setTitle("Visit Website", for: .normal)
    visitwebsitebutton.setTitleColor(.white, for: .normal)
    visitwebsitebutton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 18)
    visitwebsitebutton.layer.cornerRadius = 20
    visitwebsitebutton.Link("Some label", destination: URL(string: "https://www.mylink.com")!) // <-- link used here
    return visitwebsitebutton
  }()

  override func viewDidLoad() {
    super.viewDidLoad()

    view.addSubview(visitwebsitebutton)
  }
}

Using Link above gives me an error that reads "Value of type 'UIButton' has no member 'Link'".

What am I doing wrong and how can I fix it?

Edit 1

I just tried this inside private let visitwebsitebutton:

visitwebsitebutton(action: {"www.redacted.com"})

But now I'm getting the below error:

Cannot call value of non-function type 'UIButton'

Edit 2

Within private let visitwebsitebutton, I attempted the following:

visitwebsitebutton.addTarget(self, action: "buttonClicked", for: UIControl.Event.touchUpInside)

Using the above, I am getting a few warning:

'self' refers to the method 'ViewController.self', which may be unexpected
Use 'ViewController.self' to silence this warning
No method declared with Objective-C selector 'buttonClicked'
Replace '"buttonClicked"' with 'Selector("buttonClicked")'

I tried to call the buttonClicked like this:

@objc func buttonClicked(sender:UIButton)
{
    if(sender.tag == 5){

        var abc = "argOne" //Do something for tag 5
    }
    print("hello")
}

And above, I am getting the below warning:

Initialization of variable 'abc' was never used; consider replacing with assignment to '_' or removing it
Replace 'var abc' with '_'

I just want to get the button to work.

halfer
  • 19,824
  • 17
  • 99
  • 186
John Beasley
  • 2,577
  • 9
  • 43
  • 89
  • You are confusing `UIButton` with a button in SwiftUI. `UIButton` has no such function. You have to add a handler using `addTarget(:action)`. – Sulthan Sep 22 '22 at 15:03
  • @Sulthan - are you able to provide an example? – John Beasley Sep 22 '22 at 15:07
  • You are almost there with the second attempt. However, you will have to initialize the variable lazily: `private lazy var visitwebsitebutton`, otherwise `self` won't be accessible yet. Also, use `#selector(buttonClicked(sender:))` as the `action`. – Sulthan Sep 22 '22 at 16:53
  • @Sulthan - I was able to get the warnings to disappear, but when clicking on the button, I get an error that directs me to the AppDelegate file in Xcode that reads the following: Thread 1: "-[MobApp.ViewController buttonClicked]: unrecognized selector sent to instance 0x1048061e0" – John Beasley Sep 22 '22 at 17:14
  • As buttonClicked has 1 argument you must add (sender:) in the selector, for the code to recognize the function. – Ptit Xav Sep 22 '22 at 18:32
  • @PtitXav - I just want the button to be clicked, and send the user to a web page. I am new to Swift, and I'm following tutorials but getting stuck. – John Beasley Sep 22 '22 at 19:13

1 Answers1

-1

This is how I solved the problem:

class ViewController: UIViewController {
    private lazy var visitwebsitebutton: UIButton = {
    let visitwebsitebutton = UIButton()
    let mygreen = UIColor(rgb: 0x12823b)
    visitwebsitebutton.backgroundColor = mygreen
    visitwebsitebutton.setTitle("Visit Website", for: .normal)
    visitwebsitebutton.setTitleColor(.white, for: .normal)
    visitwebsitebutton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 18)
    visitwebsitebutton.layer.cornerRadius = 20
    visitwebsitebutton.addTarget(self, action: #selector(visitwebsitebuttonTapped), for: .touchUpInside)
    return visitwebsitebutton
  }()

  @objc func visitwebsitebuttonTapped() {
    if let yourURL = URL(string: "https://www.somesite.com") {
            UIApplication.shared.open(yourURL, options: [:], completionHandler: nil)
    }
  }
}

If anyone needs help with iOS mobile development with Swift, and you just want to be able to click on a button and have it take you to a site, look no further.

halfer
  • 19,824
  • 17
  • 99
  • 186
John Beasley
  • 2,577
  • 9
  • 43
  • 89