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

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

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

Objective-Cはオブジェクト指向型のプログラミング言語のひとつです。C言語をベースにSmalltalkが取り入れられています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Q&A

解決済

1回答

1937閲覧

Xcodeでアラートがうまく表示されない場合の解決法は?

退会済みユーザー

退会済みユーザー

総合スコア0

Objective-C

Objective-Cはオブジェクト指向型のプログラミング言語のひとつです。C言語をベースにSmalltalkが取り入れられています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

0グッド

0クリップ

投稿2016/12/20 08:21

編集2016/12/20 08:45

下記のソースコードは、ボタンを長押しするとアラートが表示されるプログラムです。
しかし、長押ししても1度はアラートが表示されますが、アラートを閉じてまたボタン長押ししても表示されないのです。

さらにログに下記のようなメッセージが表示されます。
解決策をアドバイス頂けると幸いです。

「ログ」
2016-12-20 17:13:17.992 SeBtn[2079:81223] Warning: Attempt to present <UIAlertController: 0x7fb671e9e280> on <ViewController: 0x7fb671c07d90> which is already presenting <UIAlertController: 0x7fb671e1ddc0>

[ViewController.m]

#import "ViewController.h" #import "AppDelegate.h" AppDelegate *ViewAppDelegate; UILongPressGestureRecognizer *LongGesture; @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //ロングタップ開始 [self setLongTapGesture]; //delegateデータを送る準備 ViewAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } //ロングタップ開始 - (void)setLongTapGesture{ LongGesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(long_tapped:)]; LongGesture.delegate = self; } //音声アラートname_keep_str1-24 - (void)voice_Alert { //音声名代入 if(_btn1.tag == 1) { ac = [UIAlertController alertControllerWithTitle:@"音声名" message:ViewAppDelegate.name_keep_str1 preferredStyle:UIAlertControllerStyleAlert]; } if(_btn2.tag == 2) { ac = [UIAlertController alertControllerWithTitle:@"音声名" message:ViewAppDelegate.name_keep_str2 preferredStyle:UIAlertControllerStyleAlert];; } if(_btn3.tag == 3) { ac = [UIAlertController alertControllerWithTitle:@"音声名" message:ViewAppDelegate.name_keep_str3 preferredStyle:UIAlertControllerStyleAlert]; } if(_btn4.tag == 4) { ac = [UIAlertController alertControllerWithTitle:@"音声名" message:ViewAppDelegate.name_keep_str4 preferredStyle:UIAlertControllerStyleAlert]; } if(_btn5.tag == 5) { ac = [UIAlertController alertControllerWithTitle:@"音声名" message:ViewAppDelegate.name_keep_str5 preferredStyle:UIAlertControllerStyleAlert]; } // コントローラを生成 // Cancel用のアクションを生成 UIAlertAction * recoAction = [UIAlertAction actionWithTitle:@"録音" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) { // ボタンタップ時の処理 }]; UIAlertAction * delAction = [UIAlertAction actionWithTitle:@"削除" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { }]; [ac addAction:recoAction]; [ac addAction:delAction]; // アラート表示処理 [self presentViewController:ac animated:YES completion:nil]; } //ロングタップ - (void)long_tapped:(UILongPressGestureRecognizer *)sender{ //ボタンに応じてアラート表示 //btn1 if(_btn1.tag == 1) { [self voice_Alert]; } //btn2 if(_btn2.tag == 2) { [self voice_Alert]; } //btn3 if(_btn3.tag == 3) { [self voice_Alert]; } //btn4 if(_btn4.tag == 4) { [self voice_Alert]; } //btn5 if(_btn5.tag == 5) { [self voice_Alert]; } } //action btn1-24 - (IBAction)btn1:(id)sender { //ロングタップ有効化 //btn に追加 [_btn1 addGestureRecognizer:LongGesture]; _btn1.userInteractionEnabled = YES; } - (IBAction)btn2:(id)sender { [_btn2 addGestureRecognizer:LongGesture]; _btn2.userInteractionEnabled = YES; } - (IBAction)btn3:(id)sender { [_btn3 addGestureRecognizer:LongGesture]; _btn3.userInteractionEnabled = YES; } - (IBAction)btn4:(id)sender { [_btn4 addGestureRecognizer:LongGesture]; _btn4.userInteractionEnabled = YES; } - (IBAction)btn5:(id)sender { [_btn5 addGestureRecognizer:LongGesture]; _btn5.userInteractionEnabled = YES; } @end

[ViewController.h]

#import <UIKit/UIKit.h> @interface ViewController : UIViewController<UIGestureRecognizerDelegate,UIAlertViewDelegate> { // アラートコントローラを生成 UIAlertController * ac; } //ロングタップ - (void)long_tapped:(UILongPressGestureRecognizer *)sender; //ロングタップ開始 - (void)setLongTapGesture; //音声アラート - (void)voice_Alert; @end

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

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

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

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

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

guest

回答1

0

ベストアンサー

なんとなくやりたいことは分かりましたが、コードが無茶苦茶ですね。

パッと見で気付いたことを書いておきます。

  • UILongPressGestureRecognizerはボタンの数だけ用意して、それぞれのボタンにaddGestureRecognizerして下さい。

  • 今はタップするたびにaddGestureRecognizerしていますが、最初に一度だけにして下さい。

  • 各ボタンのtagを調べていますが、全く意味がありません。

objectivec

1long_tapped:(UILongPressGestureRecognizer *)sender 2{ 3 UIButton *button = (UIButton *)sender.view; //長押ししたボタンを取得 4 switch (button.tag) 5 { 6 case 1: 7 //ボタン1の処理 8 break; 9 case 2: 10 //ボタン2の処理 11 break; 12 : 13 } 14}

として下さい。

とりあえずこんなところで。

投稿2016/12/20 08:39

編集2016/12/20 09:01
fuzzball

総合スコア16731

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

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

退会済みユーザー

退会済みユーザー

2016/12/20 08:45

すいません、わたしの説明が不足でした。アラートを閉じてまたボタン長押ししても表示されないのです。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問