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

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

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

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

Q&A

解決済

1回答

1096閲覧

Swift ボタンを押した時に呼ばれるメソッドで引数を使いたい

ataru2222

総合スコア272

Swift

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

0グッド

0クリップ

投稿2022/05/29 05:22

編集2022/05/29 18:37

実現したいこと

ボタンを押したときに使用するメソッドのbutton.addTarget()で引数を渡したい。

発生している問題・エラーメッセージ

現在、ボタンを押した際に画面遷移するコードを書いているのですが、ボタンによって遷移した際の初期状態を変更したいと考えているのですが、引数を渡そうとしたら次のエラーが発生しました。

Argument of '#selector' does not refer to an '@objc' method, property, or initializer

訳:
'#selector'の引数は、'@objc'メソッド、プロパティ、または初期化子を参照していません

該当のソースコード

Swift

1import Foundation 2import UIKit 3 4class MenuViewController: UIViewController{ 5 6 var button1 = UIButton() 7 var button2 = UIButton() 8 9 override func viewDidLoad() { 10 super.viewDidLoad() 11 12 buttonAdd() 13 } 14 15 //ボタンを追加する 16 func buttonAdd() { 17 18 //botton1 19 button1.frame = CGRect(x: 100, y: 100, width: 100, height: 20) 20 21 button1.addTarget(self, 22 action: #selector(sendView(num: 1)), 23 for: .touchUpInside) 24 button1.backgroundColor = UIColor.blue 25 view.addSubview(button1) 26 27 //button2 28 button2.frame = CGRect(x: 100, y: 200, width: 100, height: 20) 29 button2.addTarget(self, 30 action: #selector(sendView(num: 2)), 31 for: .touchUpInside) 32 button2.backgroundColor = UIColor.blue 33 view.addSubview(button2) 34 35 } 36 37 //NextViewに遷移する 38 @objc func sendView(num: Int) { 39 // ①storyboardのインスタンス取得 40 let storyboard: UIStoryboard = self.storyboard! 41 42 // ②遷移先ViewControllerのインスタンス取得 43 let nextView = storyboard.instantiateViewController(withIdentifier: "main") as! ViewController 44 nextView.modalPresentationStyle = .fullScreen 45 // ③画面遷移 46 self.present(nextView, animated: true, completion: nil) 47 } 48 49 50} 51

試したこと

#selector(sendView(_:))や引数を複数にしてみたり、考えられる範囲のものは試しましたが、上手くいかず。

こちらのサイトで調べると引数は使えない。と書かれておりました。
https://pg-happy.jp/swift-addtarget.html

かなり使う機能だと思うのですが、実際にaddTarget()メソッドでは引数は渡せないのでしょうか?

こちらのサイトではimageViewだったのですが、ボタンの時で1とか2等の引数を渡したい時はどのようにすると良いでしょうか?
無理やりvar 等に格納した変数に入れて送ることもできると思うのですが、何かスマートではない気がしております。

こちらについて詳しい方いらっしゃいましたら、ご教授願います。
よろしくお願いいたします。

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

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

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

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

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

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

guest

回答1

0

ベストアンサー

かなり使う機能だと思うのですが、実際にaddTarget()メソッドでは引数は渡せないのでしょうか?

A selector identifying the action method to be called. You may specify a selector whose signature matches any of the signatures in Listing 1. This parameter must not be nil.
呼び出されるアクションメソッドを特定するセレクタ。リスト 1 のシグネチャのいずれかに一致するセレクタを指定することができます。このパラメーターは nil であってはいけません。
https://developer.apple.com/documentation/uikit/uicontrol/1618259-addtarget

Listing 1 Action method signatures

swift

1@IBAction func doSomething() 2@IBAction func doSomething(sender: UIButton) 3@IBAction func doSomething(sender: UIButton, forEvent event: UIEvent)

https://developer.apple.com/documentation/uikit/uicontrol#1943645

アクションメソッド?はボタンの取り決めの通りの引数しか受け取れないのかなと思います。


sendViewメソッドの中で押されたボタンを判断したいだけなら次のような感じでも良いのかなと思いました。

swift

1 @objc func sendView(sender: UIButton, forEvent event: UIEvent) { 2 if sender == button1 { 3 print("button1") 4 } 5 if sender == button2 { 6 print("button2") 7 } 8 }

An integer that you can use to identify view objects in your application.
アプリケーションでビューオブジェクトを識別するために使用できる整数値です。
https://developer.apple.com/documentation/uikit/uiview/1622493-tag

あるいはtagを使って識別する感じかなと思います。

swift

1 func buttonAdd() { 2 button1.frame = CGRect(x: 100, y: 100, width: 100, height: 20) 3 button1.addTarget(self, 4 action: #selector(sendView(sender:forEvent:)), 5 for: .touchUpInside) 6 button1.backgroundColor = UIColor.blue 7 button1.tag = 1 // ***** button1のtagを1に設定 8 view.addSubview(button1) 9 button2.frame = CGRect(x: 100, y: 200, width: 100, height: 20) 10 button2.addTarget(self, 11 action: #selector(sendView(sender:forEvent:)), 12 for: .touchUpInside) 13 button2.backgroundColor = UIColor.blue 14 button2.tag = 2 // ***** button1のtagを2に設定 15 view.addSubview(button2) 16 } 17 @objc func sendView(sender: UIButton, forEvent event: UIEvent) { 18 if sender.tag == 1 { 19 print("button1") 20 } 21 if sender.tag == 2 { 22 print("button2") 23 } 24 }

投稿2022/05/29 08:44

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

ataru2222

2022/05/29 09:37

解答していただき、ありがとうございます。 このような感じの書き方ができるのですね。 今回はtagを使う事にしましたが、とても勉強になりました。 本当にありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問