prettyprint

2017年5月5日 星期五

範例:撥打電話

Objective


學習使用 Button 元件,同時處理多按鈕共用事件

Lab


1. 建立 Single View Application
2. UI 設計

在此畫面需加入 2 張 image,一個作為 Phone Call,另一個為 Phone Hangup


Code

//
//  ViewController.swift
//  MyPhoneDemo
//
//  Created by Elvis Meng on 2017/5/5.
//  Copyright © 2017 Elvis Meng. All rights reserved.
//

import UIKit

var str:String = ""

class ViewController: UIViewController {

    @IBOutlet weak var displayPhoneNumber: UILabel!
    
    @IBAction func tapDigitPad(_ sender: UIButton) {
        str = str + (sender.titleLabel?.text)!
        displayPhoneNumber.text = str
    }
    
    @IBAction func phoneCall(_ sender: UIButton) {
        let url = URL(string: "tel:"+str)
        if #available(iOS 10.0, *) {
            UIApplication.shared.open(url!, options: [:], completionHandler: nil)
        } else {
            UIApplication.shared.openURL(url!)
        }
    }
    
    @IBAction func phoneHangUp(_ sender: UIButton) {
        str = ""
        displayPhoneNumber.text = ""
    }
    
    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.
    }


}


Test




Summary


此範例並處理 Phone Call,也處理 Phone Up


 /end

沒有留言:

張貼留言

prettyPrint();