質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

1回答

8326閲覧

AppDelegateでアプリが固まってしまう

退会済みユーザー

退会済みユーザー

総合スコア0

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2017/03/03 08:28

編集2022/01/12 10:55

AppDelegateでアプリが固まってしまいます。
今、カメラアクセスと写真のアップロード部分を作っていて、それが実機でできません。
Xcodeから自分のiPhoneでアプリを起動したのですが
Camera access ボタンとPhoto upload ボタンは自分のiPhoneで表示されたのですが
それらは押せるだけしか機能がありません。Camera access ボタンを押したら、iPhone(実機)のカメラが開いて写真が撮れて、撮った写真をアプリに表示させたいです。Photo upload ボタンを押したら、iPhone(実機)のカメラロールが開き元からある写真の中から一枚を選べ、選択した写真をアプリに表示させたいです。
実機で立ち上げ、両方のボタンを押すと
AppDelegate.swiftの

class AppDelegate: UIResponder, UIApplicationDelegate {

の部分でエラーが出て(多分No signboardと)固まって動かなくなります。

コントローラには

import UIKit class SendController:UIViewController, UINavigationControllerDelegate,UIImagePickerControllerDelegate{ //定数 let ButtonCamera = 0 let ButtomRead = 1 let ButtonWrite = 2 //変数 var imageView:UIImageView = UIImageView() var btnCamera:UIButton = UIButton(type: .custom) var btnRead:UIButton = UIButton(type: .custom) var btnWrite:UIButton = UIButton(type: .custom) //ロード完了時に呼ばれる override func viewDidLoad() { super.viewDidLoad() // //カメラボタン生成 // self.view.addSubview(btnCamera) // //読み込み // self.view.addSubview(btnRead) // // イメージビューの生成 // self.view.addSubview(imageView) imageView.frame = CGRect(x: 150, y: 100, width: 200, height: 200) imageView.contentMode = .scaleAspectFit view.addSubview(imageView) btnCamera.frame = CGRect(x: 0, y: 100, width: 100, height: 100) btnCamera.setTitle("Camera", for: .normal) btnCamera.tag = ButtonCamera btnCamera.addTarget(self, action: #selector(self.onClick(sender:)), for: .touchUpInside) btnCamera.backgroundColor = UIColor.green self.view.addSubview(btnCamera) btnRead.frame = CGRect(x: 0, y: 200, width: 100, height: 100) btnRead.setTitle("Read", for: .normal) btnRead.tag = ButtomRead btnRead.addTarget(self, action: #selector(self.onClick(sender:)), for: .touchUpInside) btnRead.backgroundColor = UIColor.red self.view.addSubview(btnRead) btnWrite.frame = CGRect(x: 0, y: 300, width: 100, height: 100) btnWrite.setTitle("Write", for: .normal) btnWrite.tag = ButtonWrite btnWrite.addTarget(self, action: #selector(self.onClick(sender:)), for: .touchUpInside) btnWrite.backgroundColor = UIColor.blue self.view.addSubview(btnWrite) } //ボタンクリック時に呼ばれる @IBAction func ButtonCamera(_ sender: Any) { } @IBAction func ButtonRead(_ sender: Any) { } func onClick(sender:UIButton){ if sender.tag == ButtonCamera { openPicker(sourceType: UIImagePickerControllerSourceType.camera) }else if sender.tag == ButtomRead { openPicker(sourceType: UIImagePickerControllerSourceType.photoLibrary) } } //アラートの表示 func showAlert(title: String?, text: String?) { let alert = UIAlertController(title: title, message: text, preferredStyle: UIAlertControllerStyle.alert) alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)) present(alert, animated: true, completion: nil) } //イメージピッカーのオープン func openPicker(sourceType:UIImagePickerControllerSourceType){ if !UIImagePickerController.isSourceTypeAvailable(sourceType){ showAlert(title: nil, text: "利用できません") return } //イメージピッカーの生成 let picker = UIImagePickerController() picker.sourceType = sourceType picker.delegate = self //ビューコントローラーのビューを開く present(picker, animated: true, completion: nil) } // // イメージピッカーのイメージ取得時に呼ばれる func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { let image = info[UIImagePickerControllerOriginalImage]as! UIImage imageView.image = image //ビューコントローラーのビューを閉じる picker.presentingViewController?.dismiss(animated: true,completion:nil) } // //イメージピッカーのキャンセル取得時に呼ばれる func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { picker.presentingViewController?.dismiss(animated: true, completion: nil) } }

と書き
ボタンの設定などは
イメージ説明
イメージ説明

のようにしました。
AppDelegate.swiftには

import UIKit import Alamofire @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. return true } func applicationWillResignActive(_ application: UIApplication) { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. } func applicationDidEnterBackground(_ application: UIApplication) { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } func applicationWillEnterForeground(_ application: UIApplication) { // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. } func applicationDidBecomeActive(_ application: UIApplication) { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } func applicationWillTerminate(_ application: UIApplication) { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } }

のように書いてあります。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

推測にはなりますがコードとStoryboardが繋がっていない、または一度繋げたけど関数名を書き換えた等Storyboardが絡んだ問題じゃないかと思います。
一旦接続を切って
””
それからoption + クリックでIBActionのコードと再接続してみてください。

もう一つ個人的な考えではありますがButtonの設定はStoryboardとコード両方使うよりも、Storyboardを出来るだけ使って設定した方が可読性も上がりますしトラブルが起きた時も楽になりやすいです。

投稿2017/03/04 08:52

xAxis

総合スコア1349

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問