アカウント作成画面

ダイアログの表示

UIAleartControllerのインスタンスを作成し、presentする。

let dialog = UIAlertController(title: "タイトル", message: "詳細", preferredStyle: .alert)
dialog.addAction(UIAlertAction(title: "ボタンのテキスト", style: .default, handler: nil))
present(dialog, animated: true, completion: nil)

カメラロールから画像の選択

UIImagePickerControllerDelegate, UINavigationControllerDelegate

//UIImagePickerControllerを表示する
@objc func onImage(){
    let picker = UIImagePickerController()
    picker.sourceType = .photoLibrary
    picker.mediaTypes = ["public.image"]
    picker.delegate = self
    present(picker, animated: true)
}

//画像が選択された時
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    //選択された画像を取得
    let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage
    imageView.image = image
    dismiss(animated: true, completion: nil)
}

完成したコード

import Foundation
import UIKit

class CreateAccountView :UIViewController,UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
    @IBOutlet weak var nameField: UITextField!
    @IBOutlet weak var emailField: UITextField!
    @IBOutlet weak var passwordField: UITextField!
    @IBOutlet weak var imageView: UIImageView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(hideKeyboard)))
        imageView.isUserInteractionEnabled = true
        imageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(onImage)))
    }
    
    @objc func onImage(){
        let picker = UIImagePickerController()
        picker.sourceType = .photoLibrary
        picker.mediaTypes = ["public.image"]
        picker.delegate = self
        present(picker, animated: true)
    }
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage
        imageView.image = image
        dismiss(animated: true, completion: nil)
    }
    
    @objc func hideKeyboard(){
        view.endEditing(true)
    }
    
    @IBAction func onResister(_ sender: Any) {
        if nameField.text!.count < 3 || nameField.text!.count > 11 {
            showError(message: "名前は3字以上10字以内で設定してください。")
            return
        }
        AuthHelper().createAccount(email: emailField.text!, password: passwordField.text!, result: {
            success in
            if success {
                DatabaseHelper(uid: AuthHelper().uid()).resisterUserInfo(name: self.nameField.text!, image: self.imageView.image!)
                self.dismiss(animated: true, completion: nil)
            } else {
                self.showError(message: "有効なメールアドレス、6文字以上のパスワードを設定してください。")
            }
        })
    }
    
    @IBAction func onBack(_ sender: Any) {
        dismiss(animated: true, completion: nil)
    }
    
    func showError(message:String){
        let dialog = UIAlertController(title: "エラー", message: message, preferredStyle: .alert)
        dialog.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
        present(dialog, animated: true, completion: nil)
    }
    
}

コメント

タイトルとURLをコピーしました