Multi Developer SuHo

iOS 프로그래밍 14주차 (신체나이 계산기 앱) 본문

Swift 과제물

iOS 프로그래밍 14주차 (신체나이 계산기 앱)

Dreaming Developer Student 2023. 12. 5. 13:25
SMALL

 

/
//  WebViewController.swift
//  BMI1711
//
//  Created by 소프트웨어컴퓨터 on 2023/11/28.
//

import UIKit
import WebKit
class WebViewController: UIViewController {
    

    @IBOutlet weak var webView: WKWebView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let myURL = URL(string:"https://simplecoding77.tistory.com/")
        let myRequest = URLRequest(url: myURL!)
        webView.load(myRequest)
        
    }
    
    @IBAction func goWeb(_ sender: UISegmentedControl) {
        if sender.selectedSegmentIndex == 0 {
            let myURL = URL(string:"https://m.naver.com/")
            let myRequest = URLRequest(url: myURL!)
            webView.load(myRequest)
        } else {
            let myURL = URL(string:"https://simplecoding77.tistory.com/")
            let myRequest = URLRequest(url: myURL!)
            webView.load(myRequest)
        }
    }
    
    @IBAction func goTistory(_ sender: UIButton) {
        let myURL = URL(string:"https://simplecoding77.tistory.com/")
        let myRequest = URLRequest(url: myURL!)
        webView.load(myRequest)
    }
    
}

 

리팩토리 후 소스입니다.

import UIKit
import WebKit

class WebViewController: UIViewController {
    
    @IBOutlet weak var webView: WKWebView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        loadURL("https://simplecoding77.tistory.com/")
    }
    
    @IBAction func goWeb(_ sender: UISegmentedControl) {
        let url: String
        switch sender.selectedSegmentIndex {
        case 0:
            url = "https://m.naver.com/"
        default:
            url = "https://simplecoding77.tistory.com/"
        }
        loadURL(url)
    }
    
    @IBAction func goTistory(_ sender: UIButton) {
        loadURL("https://simplecoding77.tistory.com/")
    }
    
    private func loadURL(_ urlString: String) {
        if let url = URL(string: urlString) {
            let request = URLRequest(url: url)
            webView.load(request)
        }
    }
}

 









 

 

 





 




LIST