LoginSignup
1
2

More than 5 years have passed since last update.

デリゲートやプロトコルを使わずに他クラスに定義されたメソッドを呼び出す方法

Last updated at Posted at 2016-11-14

classAに定義されたUIViewControllerをclassBから呼び出したい:kissing_closed_eyes:

やりたいことは単純だ。ボタンを押したら画面が繊維する。でも、色々あって自クラスにUIViewControllerを定義できないのだ。

ストーリーボードやUIの動作はclassAにあるけど、処理はclassBにあって、clssAでボタンを押した結果、その後の処理はclassBに来るから、そこで、pushViewControllerを定義したいけれど、

でもclassBはUIViewControllerを定義できないからpushViewControllerを呼び出せない。どう上手に説明していいか分かりません。わからなければ、僕にはもう、どうすルコともできません。

どちらにせよpresentViewControllerにしてもpushViewControllerにしても、それらはUIViewControllerのインスタンスメソッドですから

uivewcontrollerが定義されてないclassではメソッドが呼べないのです。私には、UIViewControllerを定義してるclassが必要なのです

どうすればいいかUIViewControllerを定義してるclassからUIViewControllerUIViewControllerを渡せばいいじゃないか

デリゲートが処理を受け取るイメージで、プロトコルが自分を大きくするイメージなら、もらうイメージです。よくわかりませんか、困ったなあ。

コード

受け取る方(uiviewcontrollerを継承してないclass)

classB

var viewControllerPush : UIViewController? = nil
//変数viewControllerを定義してnillを入れておく
//ここにUIViewControllerが入ってきます

渡す方(uiviewcontrollerを継承してるclass)

classA

var jumpB = ClassB()


    override func viewDidLoad() {
        super.viewDidLoad()

        self.jumpB.viewControllerPush = self

//classBをインスタンス化したjumpBを呼び出して
//classBにはviewControllerPushがいますから
//そこに、self(つまり、classAにはUIViewControllerが定義されてま)を入れる

参考

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2