私はiOSにかなり慣れていないので、ここで説明するように、別のViewController内からViewControllerを提示しようとしています。
スタックオーバーフローの回答(3番目の回答を参照)
HelloWorldのサンプルコードから始めます。MyViewController.mを開き、updateString()メソッドを変更して、このコードを最後に追加します。
{
// Get the application's "window"
UIWindow *window = [UIApplication sharedApplication].keyWindow;
// Get the window's rootViewController (NOTE: Only works on iOS 4.0 or later!)
UIViewController *rootViewController = window.rootViewController;
// Create our new controller
UIViewController *enterNameController = [[UIViewController alloc] initWithNibName:@"HighScore" bundle:[NSBundle mainBundle]];
if (enterNameController != NULL)
{
printf("\nLoaded UIView controller. Installing as actionSheetController...\n\n");
UINavigationController *navigationController = [[UINavigationController alloc]
initWithRootViewController:enterNameController];
// Present out Enter Name for High Score view as a modal view
// [rootViewController presentViewController:navigationController animated:YES]; // iOS 2.0 and newer
[self presentViewController:navigationController animated:YES completion: nil]; // iOS 5.0 and newer
}
}
また、HelloWorldに付属している他のNIBファイルと一緒に配置した「HighScore.xib」というNIBファイルを作成し、HighScore.xibをXCodeプロジェクトに追加して、「バンドルリソースのコピー」セクションにドラッグしました。 。
現状では、実行して編集フィールドにテキストを入力するとエラーが発生します。
*キャッチされなかった例外「NSInternalInconsistencyException」が原因でアプリを終了しています。理由:「NIBをバンドルにロードできませんでした:「NSBundle(ロード済み)」、名前は「HighScore」
ただし、最後のステートメントを「self」ではなく「rootViewController」を使用するように変更しても、何も起こりません。例外はありませんが、新しいビューも表示されません。
質問:
1)NIBがロードされないのはなぜですか?私は明らかにここで何か間違ったことをしています。
2)「self」の代わりに「rootViewController」を使用すると異なる結果が得られるのはなぜですか?rootViewControllerは「self」と同じクラスを見つけるべきではありませんか?
「rootViewController」を使用すると、XCodeで警告が表示されることに注意してください:「MyViewController」は「-presentViewController:animated:completion」に応答しない可能性があります