1

私のビュー コントローラー StartViewController は、他の 2 つのビュー コントローラー (RootViewController または TelnetAddressbookViewController) のいずれかによって開かれ、追加されます (addSubview)。これらの 2 つのうちどれが StartViewController を起動したのかを把握しようとしています。

NSLog(@"superview %@",self.view.superview.description);
NSLog(@"superview %@",self.superclass);

if ([self.view.superview isKindOfClass:[RootViewController class]]) {

    NSLog(@"launched by RootViewController");

}else if ([self.view.superview isKindOfClass:[TelnetAddressbookViewController class]]) {

    NSLog(@"launched TelnetAddressbookViewController");
}

[self.view removeFromSuperview];

最初の NSLOG (self.view.superview.description) の出力:

スーパービュー UIView: 0x81d6710; フレーム = (0 0; 748 1024); 変換 = [0, 1, -1, 0, 0, 0]; 自動サイズ変更 = RM+BM; 層 = >

その参照は必要なもの (0x81d6710) ですが、そのクラスはカスタム ビュー コントローラーではなく UIViewController です。

4

4 に答える 4

4

それ以外の

NSLog(@"superview %@",self.view.superview.description);

あなたが欲しい...

#import <objc/runtime.h>
NSLog(@"superview %s",class_getName([self.view.superview class]) );
/* or if you want an NSString */
NSLog(@"superview %@", NSStringFromClass([self.view.superview class]) );
于 2013-01-31T17:27:28.543 に答える
1

ビューのコントローラーを検索する場合は、おそらく次のようにする必要があります。[self.view.superview nextResponder];

于 2013-01-31T17:30:05.187 に答える
1

あなたの質問のもつれを解くと、あなたは2つのことのうちの1つを望んでいると思います...どちらにしても、最良の答えはデリゲートを含むようです...

(1) viewController を作成した ViewController 作成中のビュー コントローラにフックを戻す最善の方法は、.delegate プロパティをStartViewController. 次に、それが作成するviewControllerは、それ自体をデリゲートとして設定できます。次に、StartViewController必要な参照が表示されます。

(2)self.viewのUIViewControllerは、現在のビューの観点からビュー階層の最上位にあります。次に、UIViewControllerのサブクラスを見つけようとしています。

ビューから:

self.window.rootViewController

現在の画面コンテンツのトップレベルのviewControllerを取得します。

次に、たとえばクラスを確認できます

if ([self.window.rootViewController isKindOfClass:[RootViewController class]]) {

    NSLog(@"superview RootViewController");

}else if ([self.view.rootViewController isKindOfClass:[TelnetAddressbookViewController class]]) {

    NSLog(@"superview TelnetAddressbookViewController");
}

しかし、ビューが 2 つの viewController クラスについて知る必要があり、MVC が機能しなくなるため、これはかなり面倒です。

ビューのデリゲートを作成し、それぞれの viewControllers にデリゲート メソッドを処理させる方がはるかに優れています。

ところで、クラスの 1 つを「RootViewController」と呼ぶべきではありません。これは UIWindow プロパティの名前であるため、混乱を招きます。

于 2013-01-31T17:51:54.683 に答える
0

元の質問に答えないプロパティを設定してしまいました(申し訳ありません)。スーパービューのフレームを評価することでそれを行ったとしても、実際には方法があるはずです。

于 2013-02-04T15:55:20.943 に答える