現在および解任のモーダルビューコントローラー
-
20-09-2019 - |
質問
誰でもできるのでのコード例は、私が使用できる最初に現在のモーダルビューコントローラー、その解任のですか?アドバイスをお聞かせ下さい:
NSLog(@"%@", blue.modalViewController); [blue presentModalViewController:red animated:YES]; NSLog(@"%@", blue.modalViewController); [blue dismissModalViewControllerAnimated:YES]; NSLog(@"%@", blue.modalViewController);このコードはviewDidLoad("青"と"赤"の両方をサブクラスのUIViewController).期待していることを示す赤色のビューし、その後直ちに非表示、アニメーションしかしこの作品のコードを記載したにすぎず、モーダルビューとな解任します。ずっと面白いアイデアないか?最初のログを示す"null"ですが、他のログ示 0x3d21bf0>
もう一つの点は、このコードapplicationDidFinishLaunching:赤のビューが表示されない、すべてのログ取得"null"
解決
まず、それは(まだnil
ているので、「赤」と「青」)にInterface Builderからインスタンス化コントローラは、まだあなたのアプリケーションにリンクされていない場合があります。
、何が間違ってやっていることは間違ったコントローラ上dismissModalViewControllerAnimated:
を呼んでいるということです!それは、このようにする必要があります:
[blue presentModalViewController:red animated:YES];
[red dismissModalViewControllerAnimated:YES];
通常「赤」コントローラは、(「キャンセル」ボタンがクリックされるかもしれない)いくつかの点で自分自身を却下することを決定しなければなりません。そして、「赤」のコントローラはself
上のメソッドを呼び出すことができます:
[self dismissModalViewControllerAnimated:YES];
それでも動作しない場合は、、コントローラがアニメーション形式で提示されているという事実とは何かを持っているので、あなたはそうすぐにそれを提示した後、コントローラを却下することは許されない場合があります。
他のヒント
は、私が疲れたXcodeの4.52で追加のビューを作成し、セグエモーダル使用してそれらを接続することで簡単な方法(制御が第2のビューにビュー1からのボタンをドラッグし、モーダルを選択しました)。 そして、2番目のビューや、作成したモーダルビューにボタンにドラッグします。制御およびヘッダファイルには、このボタンをドラッグして、アクションの接続を使用します。これはあなたのcontroller.mファイルにIBactionを作成します。コードに、ボタンのアクションタイプを検索します。
[self dismissViewControllerAnimated:YES completion:nil];
迅速かつ
更新のための迅速かつ3
絵コンテ
を二つのビューのコントローラーボタンすることが望ましい。のためのコントローラの設定クラス名 SecondViewController
の絵コンテのID secondVC
.
コード
ViewController.迅速かつ
import UIKit
class ViewController: UIViewController {
@IBAction func presentButtonTapped(_ sender: UIButton) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let myModalViewController = storyboard.instantiateViewController(withIdentifier: "secondVC")
myModalViewController.modalPresentationStyle = UIModalPresentationStyle.fullScreen
myModalViewController.modalTransitionStyle = UIModalTransitionStyle.coverVertical
self.present(myModalViewController, animated: true, completion: nil)
}
}
SecondViewController.迅速かつ
import UIKit
class SecondViewController: UIViewController {
@IBAction func dismissButtonTapped(_ sender: UIButton) {
self.dismiss(animated: true, completion: nil)
}
}
ソース:
presentModalViewControllerます:
MainViewController *mainViewController=[[MainViewController alloc]init];
[self.navigationController presentModalViewController:mainViewController animated:YES];
dismissModalViewControllerます:
[self dismissModalViewControllerAnimated:YES];
スウィフト
self.dismissViewControllerAnimated(true, completion: nil)
それを行うための最も簡単な方法は、ストーリーボードとセグエを使用しています。
ただ、ログインUIとLoginViewControllerにごTabBarControllerのFirstViewController(ないナビゲーションコントローラ)からセグエを作成して、「showLogin」という名前を付けます。
ユーザーがログインおよび/または彼/彼女のセッションは、好ましくは、AppDelegateに...有効であるかどうかを検証するためにBOOLを返すメソッドを作成します。それをisSessionValid呼び出します。
次のようにあなたのFirstViewController.mのメソッドviewDidAppearをオーバーライドします:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if([self isSessionValid]==NO){
[self performSegueWithIdentifier:@"showLogin" sender:self];
}
}
ユーザーがログインした後ならば成功し、ちょうど解任またはポップアウトLoginViewControllerをあなたのタブを表示します。
100%作品!
それが役に立てば幸い!