[Swift 입문] UI 부품의 Outlet과 Action(연결)의 차이를 흠뻑
[Swift 초입문] UI 부품의 Outlet과 Action을 버튼에 의한 동작으로 확인
 사용 UI 부품
버튼과 레이블만
 아웃렛과 액션의 차이점
문서 개요에서 코드에 연결할 때 (control 키를 누르면서 드래그 & 드롭 작업) 사진과 같은 대화 상자가 나올까 생각합니다.
 
이 때 Connection 부분에서 Outlet 또는 Action을 선택하게 됩니다. 이 두 가지의 차이를 이해하지 못한 경우에 일어나는 에러를 실체험과 함께 적어 가려고 합니다.
우선 그 전에 Outlet과 Action의 차이를 문면적으로 쓰면,
Outlet: UI 파트를 속성으로 연결합니다.
Action:UI 부품을 메소드로서 접속한다.
즉,
Outlet은 그 부품에 관해서 다른 함수나 메소드 등에서 행해지는 동작을 코드에 쓸 때
Action은 그 부품이 하는 처리의 내용을 쓸 때
각각 연결하고 정의해야합니다.
예를 든다. 버튼으로 생각하면, 그 버튼을 누르는 것으로 행해지는 동작의 정의는 Action에서, 다른 버튼을 누르거나 하는 것으로 특정의 버튼에 영향을 준다 (버튼을 누르는 것을 불가능하게 한다, 그 버튼을 보이지 않게 한다)등이라고 말한 동작의 정의에는 Outlet을 이용하게 되는 것입니다.
문장에서는 잘 모르겠다고 생각하므로, 실제의 처리를 봐 갑시다.
 버튼의 아웃렛을 연결하지 않은 경우
sample.swiftimport UIKit
class ViewController: UIViewController {
    @IBOutlet weak var time: UILabel!
    var selectedTime = 0
    var count = 0
    //1秒追加する
    @IBAction func timer1(_ sender: Any) {
        selectedTime += 1
        time.text = "\(selectedTime)" //変数展開を行い、時間を表示する
    }
    //10秒追加する
    @IBAction func timer10(_ sender: UIButton) {
        selectedTime += 10
        time.text = "\(selectedTime)"
    }
    //設定された時間によるタイマーをスタートさせる
    @IBAction func timeStart(_ sender: UIButton) {
     //スタートボタンを押すと時間追加ボタンを触れなくする
        count += 1
        if count % 2 == 1{
            timer1.isEnabled = false
            timer10.isEnabled = false
     //ストップを押した時に時間追加ボタンを触れるようにする
        }else{
            timer1.isEnabled = true
            timer10.isEnabled = true
        }
    }
    override func viewDidLoad() { 
        super.viewDidLoad()
        // 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.
    }
}
이러한 코드를 쓰는 것으로, 1초 버튼, 10초 버튼을 눌렀을 때에 계측 시간에 수치가 가산되고 있습니다. (실제 타이머의 작성에 관해서는 아직 미완성입니다.)
하지만 지금은 그대로,
sample.swift//スタートボタンを押すと時間追加ボタンを触れなくする
        count += 1
        if count % 2 == 1{
            timer1.isEnabled = false
            timer10.isEnabled = false
     //ストップを押した時に時間追加ボタンを触れるようにする
        }else{
            timer1.isEnabled = true
            timer10.isEnabled = true
        }
부분에 대해 다음과 같은 오류가 발생합니다. (본래는 여기에서 시작 정지 버튼을 누르면 타이머가 작동하는 동안 다른 두 개의 버튼이 작동하지 않는 것을 목표로합니다.)
 
이것은, 현재 상태에서는 1초 추가 버튼, 10초 추가 버튼 모두 정의하고 있는 것은 Action만이며, Action에서는 그 버튼을 누르는 것으로 실시하는 동작을 기입하는 것을 가능하게 하고 있기 때문입니다.
거기서, 각각의 버튼으로 Action 뿐만이 아니라, Outlet를 다시 접속하는 것으로, 이러한 에러를 해제할 수 있었습니다.
 동작 확인
 
 
 이번에 사용한 코드 전체
sample.swiftimport UIKit
class ViewController: UIViewController {
    @IBOutlet weak var time: UILabel!
    @IBOutlet weak var timer1: UIButton!
    @IBOutlet weak var timer10: UIButton!
    var selectedTime:Int = 0
    var count = 0
    //1秒追加する
    @IBAction func timer1(_ sender: Any) {
        selectedTime += 1
        time.text = "\(selectedTime)"
    }
    //10秒追加する
    @IBAction func timer10(_ sender: UIButton) {
        selectedTime += 10
        time.text = "\(selectedTime)"
    }
    //設定された時間によるタイマーをスタートさせる
    @IBAction func timeStart(_ sender: UIButton) {
     //スタートボタンを押すと時間追加ボタンを触れなくする
        count += 1
        if count % 2 == 1{
            timer1.isEnabled = false
            timer10.isEnabled = false
     //ストップを押した時に時間追加ボタンを触れるようにする
        }else{
            timer1.isEnabled = true
            timer10.isEnabled = true
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        // 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.
    }
}
 요약
Action과 Outlet을 잘 구분하여, 이번과 같이 버튼과 라벨의 연동, 버튼끼리의 연동을 가능하게 할 수 있었습니다.
                
                    
        
    
    
    
    
    
                
                
                
                
                    
                        
                            
                            
                            Reference
                            
                            이 문제에 관하여([Swift 입문] UI 부품의 Outlet과 Action(연결)의 차이를 흠뻑), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
                                
                                https://qiita.com/TakujiKato/items/ee0341af4bee1105dee3
                            
                            
                            
                                텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                            
                            
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)
                            
                            
                        
                    
                
                
                
            
import UIKit
class ViewController: UIViewController {
    @IBOutlet weak var time: UILabel!
    var selectedTime = 0
    var count = 0
    //1秒追加する
    @IBAction func timer1(_ sender: Any) {
        selectedTime += 1
        time.text = "\(selectedTime)" //変数展開を行い、時間を表示する
    }
    //10秒追加する
    @IBAction func timer10(_ sender: UIButton) {
        selectedTime += 10
        time.text = "\(selectedTime)"
    }
    //設定された時間によるタイマーをスタートさせる
    @IBAction func timeStart(_ sender: UIButton) {
     //スタートボタンを押すと時間追加ボタンを触れなくする
        count += 1
        if count % 2 == 1{
            timer1.isEnabled = false
            timer10.isEnabled = false
     //ストップを押した時に時間追加ボタンを触れるようにする
        }else{
            timer1.isEnabled = true
            timer10.isEnabled = true
        }
    }
    override func viewDidLoad() { 
        super.viewDidLoad()
        // 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.
    }
}
//スタートボタンを押すと時間追加ボタンを触れなくする
        count += 1
        if count % 2 == 1{
            timer1.isEnabled = false
            timer10.isEnabled = false
     //ストップを押した時に時間追加ボタンを触れるようにする
        }else{
            timer1.isEnabled = true
            timer10.isEnabled = true
        }
import UIKit
class ViewController: UIViewController {
    @IBOutlet weak var time: UILabel!
    @IBOutlet weak var timer1: UIButton!
    @IBOutlet weak var timer10: UIButton!
    var selectedTime:Int = 0
    var count = 0
    //1秒追加する
    @IBAction func timer1(_ sender: Any) {
        selectedTime += 1
        time.text = "\(selectedTime)"
    }
    //10秒追加する
    @IBAction func timer10(_ sender: UIButton) {
        selectedTime += 10
        time.text = "\(selectedTime)"
    }
    //設定された時間によるタイマーをスタートさせる
    @IBAction func timeStart(_ sender: UIButton) {
     //スタートボタンを押すと時間追加ボタンを触れなくする
        count += 1
        if count % 2 == 1{
            timer1.isEnabled = false
            timer10.isEnabled = false
     //ストップを押した時に時間追加ボタンを触れるようにする
        }else{
            timer1.isEnabled = true
            timer10.isEnabled = true
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        // 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.
    }
}
Reference
이 문제에 관하여([Swift 입문] UI 부품의 Outlet과 Action(연결)의 차이를 흠뻑), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/TakujiKato/items/ee0341af4bee1105dee3텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)