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

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

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

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

Xcode

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

Q&A

解決済

1回答

2693閲覧

Xcodeでのボタンとメソッドの紐付け方

退会済みユーザー

退会済みユーザー

総合スコア0

Objective-C

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

Xcode

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

0グッド

0クリップ

投稿2016/12/24 15:31

編集2016/12/24 15:41

storyboard上にボタンを複数配置してロングタップしたらアラート表示、シングルタップでログを表示させたいのですが、ボタンとシングルタップメソッド、ロングタップメソッドのヒモ付方がわかりません。

アドバイス頂けると幸いです。

– (IBAction)buttonTapped:(id)sender { }

このようなIBActionに紐付けてその中でシングルタップやロングタップさせるしかないのでしょうか。

初心者ゆえ、無知で申し訳ありませんがよろしくお願いいたします。

下記のコードのシングルとロングタップのメソッド部分です。

[ViewController.m]

#import "ViewController.h" // Delegate画面遷移用 #import "AppDelegate.h" //ロングタップ生成 UILongPressGestureRecognizer *LongGesture; //シングルタップ生成 UITapGestureRecognizer *SingletapGesture; @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // delegateデータを送る準備 ViewAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; //ロングタップ開始 //ロングタップ LongGesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(long_tapped:)]; //デリゲートをセット LongGesture.delegate = self; //ロングタップ有効化 //btn に追加 [_btn addGestureRecognizer:LongGesture]; _btn.userInteractionEnabled = YES; //シングルタップ開始 //シングルタップ SingletapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(single_tapped:)]; //シングルタップ有効化 //btn に追加 [_btn addGestureRecognizer:SingletapGesture]; _btn.userInteractionEnabled = YES; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } //ロングタップ - (void)long_tapped:(UILongPressGestureRecognizer *)sender{ UIButton *btn = (UIButton *)sender.view; //長押ししたボタンを取得 switch (btn.tag) { case 1: //ボタンに応じてアラート表示 //btn [self voice_Alert]; break; case 2: //ボタンに応じてアラート表示 //btn [self voice_Alert]; break; } } //シングルタップ - (void)single_tapped:(UITapGestureRecognizer *)sender{ UIButton *btn = (UIButton *)sender.view; //1回だけ押したボタンを取得 switch (btn.tag) { case 1: NSlog(@"OK1"); break; case 2: NSlog(@"OK1"); break; } } //音声アラート - (void)voice_Alert { //音声名代入 switch (_btn.tag) { case 1: ac = [UIAlertController alertControllerWithTitle:@"音声名" message:@"btn2" preferredStyle:UIAlertControllerStyleAlert]; break; case 2: ac = [UIAlertController alertControllerWithTitle:@"音声名" message:@"btn2" preferredStyle:UIAlertControllerStyleAlert]; break; } @end

[ViewController.h]

#import <UIKit/UIKit.h> @interface ViewController : UIViewController<UIGestureRecognizerDelegate,UIAlertViewDelegate> { // アラートコントローラを生成 UIAlertController * ac; } //ロングタップ - (void)long_tapped:(UILongPressGestureRecognizer *)sender; //シングルタップ - (void)single_tapped:(UILongPressGestureRecognizer *)sender; //ボタン生成 @property (weak, nonatomic) UIButton *btn; //音声アラート - (void)voice_Alert; @end

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

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

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

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

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

guest

回答1

0

ベストアンサー

以下のサイトを参考にすると簡単にできますよ。

UIButtonのタップと長押しを検出する

★ 動作確認したコード

objective

1#import "ViewController.h" 2 3@interface ViewController () 4 5@end 6 7@implementation ViewController 8 9- (void)viewDidLoad { 10 [super viewDidLoad]; 11 12 13 //ボタン設定 14 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 15 button.frame = CGRectMake(100, 100, 100, 100); 16 [button setBackgroundColor:[UIColor redColor]]; 17 [button addTarget:self action:@selector(buttonTappedHandler:) forControlEvents:UIControlEventTouchUpInside]; 18 [self.view addSubview:button]; 19 20 //ボタンの長押し設定部分 21 UILongPressGestureRecognizer *gestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressedHandler:)]; 22 [button addGestureRecognizer:gestureRecognizer]; 23} 24 25 26//通常のタップ時の処理 27-(void)buttonTappedHandler:(UIButton *)button 28{ 29 NSLog(@"Tap !!!!!"); 30} 31 32//長押し関連処理 33-(void)longPressedHandler:(UILongPressGestureRecognizer *)gestureRecognizer 34{ 35 36 switch (gestureRecognizer.state) { 37 case UIGestureRecognizerStateBegan://長押しを検知開始 38 { 39 NSLog(@"UIGestureRecognizerStateBegan"); 40 } 41 break; 42 case UIGestureRecognizerStateEnded://長押し終了時 43 { 44 NSLog(@"UIGestureRecognizerStateEnded"); 45 } 46 break; 47 default: 48 break; 49 } 50} 51 52@end

投稿2016/12/24 23:11

_Kentarou

総合スコア8490

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

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

退会済みユーザー

退会済みユーザー

2016/12/25 05:31

ありがとうございます。この参考サイトは初めてみました!利用させていただきます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.49%

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

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

質問する

関連した質問