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

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

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

Swift 2は、Apple社が独自に開発を行っている言語「Swift」のアップグレード版です。iOSやOS X、さらにLinuxにも対応可能です。また、throws-catchベースのエラーハンドリングが追加されています。

Q&A

解決済

1回答

3819閲覧

エラーが出ず実行すると画面が真っ白になる

surf

総合スコア17

Swift 2

Swift 2は、Apple社が独自に開発を行っている言語「Swift」のアップグレード版です。iOSやOS X、さらにLinuxにも対応可能です。また、throws-catchベースのエラーハンドリングが追加されています。

0グッド

0クリップ

投稿2016/09/09 15:23

編集2016/09/09 23:30

プロジェクトにsound01〜sound04までの4曲を追加し、セルにButtonを設置してそれを押すと音が鳴るように以下のコードを書いたのですが、画面が真っ白になってしまいます。また、停止ボタンを追加するとエラーが出てしまい、画面が白くなるのと関係があるのかと思い、今はその部分のコードを書いてませんが、それも解決できれば解決したいです。

//<ViewControllerのコード>
import UIKit
import AVFoundation

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, CustomTableViewCellDelegate {

let imageNames = ["futako.jpg", "yokado.jpg", "fran.jpg", "zikken.jpg"] let imageTitles = ["イヌ2", "ネコ2", "イヌ1", "イヌ2"] var audioPlayer = AVAudioPlayer() let imageDescriptions = [ "イヌ", "ネコ", "イヌ", "イヌ" ] 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. } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return imageNames.count } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("MyCell") as! CustomTableViewCell cell.setCell(imageNames[indexPath.row], titleText: imageTitles[indexPath.row], descriptionText: imageDescriptions[indexPath.row]) cell.index = indexPath cell.delegate = self return cell } func selectCellButton(index: NSIndexPath) { let fileName = "sound\(index.row + 1)" print(fileName) do { let filePath = NSBundle.mainBundle().pathForResource(fileName, ofType: "mp3") let audioPath = NSURL(fileURLWithPath: filePath!) audioPlayer = try AVAudioPlayer(contentsOfURL: audioPath) if audioPlayer.prepareToPlay() { audioPlayer.play() } } catch { print("Error") } }

}

//<CustomTableViewCellのコード>
import UIKit

// protocol定義
protocol CustomTableViewCellDelegate: class {
func selectCellButton(index: NSIndexPath)
}

class CustomTableViewCell: UITableViewCell {

@IBOutlet weak var myImageView: UIImageView! @IBOutlet weak var myTitleLabel: UILabel! @IBOutlet weak var myDescriptionLabel: UILabel! @IBOutlet weak var tapButton: UIButton! weak var delegate: CustomTableViewCellDelegate! var index: NSIndexPath! override func awakeFromNib() { super.awakeFromNib() // Initialization code } override func setSelected(selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) // Configure the view for the selected state } func setCell(imageName: String, titleText: String, descriptionText: String) { myImageView.image = UIImage(named: imageName) myTitleLabel.text = titleText myDescriptionLabel.text = descriptionText } @IBAction func tapButton(sender: AnyObject) { delegate?.selectCellButton(index) }

}

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

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

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

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

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

guest

回答1

0

ベストアンサー

起動すると、すぐに何も表示されず画面が真っ白になるのでしたら、
StoryboardのViewControllerの設定の、
「Is Initial View Controller」にチェックを入れて実行。

アプリ実行時に画面が真っ黒 → is Initial View Controller にチェックが入っていないのでは

StoryboardにNavigationControllerを追加して起動時の表示画面(Initial View Controller)にする

投稿2016/09/10 00:13

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

surf

2016/09/10 00:32

Uecky_Equalさん 回答ありがとうございます。 ただ、「Is Initial View Controller」にはチェックが入っているんです^^;
退会済みユーザー

退会済みユーザー

2016/09/10 01:03

@IBAction func tapButton(sender: AnyObject) の「AnyObject」を、接続時に「UIButton」にする必要があります。
退会済みユーザー

退会済みユーザー

2016/09/10 01:11

とりあえず、ボタンをタップしたら、そのセルの位置をprintするようにしたコードです。 import UIKit import AVFoundation class TestViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { let imageNames = ["futako.jpg", "yokado.jpg", "fran.jpg", "zikken.jpg"] @IBOutlet weak var tableView: UITableView! let imageTitles = ["イヌ2", "ネコ2", "イヌ1", "イヌ2"] var audioPlayer = AVAudioPlayer() let imageDescriptions = [ "イヌ", "ネコ", "イヌ", "イヌ" ] 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. } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return imageNames.count } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("MyCell") as! CustomTableViewCell cell.setCell(imageNames[indexPath.row], titleText: imageTitles[indexPath.row], descriptionText: imageDescriptions[indexPath.row]) cell.index = indexPath // cell.delegate = self return cell } func selectCellButton(index: NSIndexPath) { let fileName = "sound\(index.row + 1)" print(fileName) do { let filePath = NSBundle.mainBundle().pathForResource(fileName, ofType: "mp3") let audioPath = NSURL(fileURLWithPath: filePath!) audioPlayer = try AVAudioPlayer(contentsOfURL: audioPath) if audioPlayer.prepareToPlay() { audioPlayer.play() } } catch { print("Error") } } @IBAction func tapButton(sender: UIButton) { var hoge = sender.superview while(hoge!.isKindOfClass(UITableViewCell) == false) { hoge = hoge!.superview } let cell = hoge as! UITableViewCell // touchIndexは選択したセルが何番目かを記録しておくプロパティ let indexPath = self.tableView.indexPathForCell(cell) print(indexPath) // delegate?.selectCellButton(index) } } //<CustomTableViewCellのコード> import UIKit class CustomTableViewCell: UITableViewCell { @IBOutlet weak var myImageView: UIImageView! @IBOutlet weak var myTitleLabel: UILabel! @IBOutlet weak var myDescriptionLabel: UILabel! @IBOutlet weak var tapButton: UIButton! // weak var delegate: CustomTableViewCellDelegate! var index: NSIndexPath! override func awakeFromNib() { super.awakeFromNib() // Initialization code } override func setSelected(selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) // Configure the view for the selected state } func setCell(imageName: String, titleText: String, descriptionText: String) { myImageView.image = UIImage(named: imageName) myTitleLabel.text = titleText myDescriptionLabel.text = descriptionText } }
surf

2016/09/10 04:15

Uecky_Equalさん 回答ありがとうございます。 画面が真っ白なのはなおりません。ですが、Uecky_Equalさんの方では実行できているということであれば、私の方の問題で、何か根本的な間違いをしていると考えられます。 コードに問題はないでしょうから、それ以外の箇所を色々といじってみます。 改めて、ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問