web-dev-qa-db-ja.com

モーダルセグエ、ナビゲーションバーが消えます

Objective-CでのコーディングにXcode 4.6.1を使用しています。 2つのView Controllerの間にモーダルセグエを作成するときにNavigation Barを表示したままにする方法を知りたいのは、ストーリーボードでセグエを実行しているため、アプリケーションを実行すると2番目のView ControllerのNavigation Barが消えるそのバーに完了ボタンがありますが、表示されません。

46
Johnny Dahdah

モーダルセグエが画面全体を引き継ぐため、現在のコントローラーにあるナビゲーションバー、ツールバー、またはタブバーはすべて隠されます。このモーダルコントローラーにナビゲーションバーが必要な場合は、それを具体的に追加し、その新しいナビゲーションバー(またはツールバー)に必要なボタンを追加する必要があります。これを行いたくない場合は、モーダルで提示せず、プッシュします。

57
rdelmar

モーダルビューコントローラに別のNavigation Controllerを追加するだけです。手順に従ってください

  1. Modal View Controllerを選択します
  2. Editor menuに移動します
  3. Embed Inを選択します
  4. Navigation Controllerを選択します

アプリケーションを実行します。これで完全に動作するはずです。

これで問題が解決することを願っています。

83
Saleh Masum

ナビゲーションバーを表示するには、次の手順を実行するだけです。

Objective-C

UINavigationController alloc]initWithRootViewController:modalVC];

Swift

let modelVC = self.storyboard?.instantiateViewController(withIdentifier: "modalVC") as! ModalVC
let navBarOnModal: UINavigationController = UINavigationController(rootViewController: modalVC)
self.present(navBarOnModal, animated: true, completion: nil)

これにより、モーダルビューコントローラーがナビゲーションバーと共に表示されます。 Objective-Cの知識は限られているため、プレゼンテーションの一部は書きませんでした。あなたはそれを理解できるはずです。 ;)

お役に立てれば!

17

IOS 8では、より良い方法があります。アダプティブプレゼンテーションスタイルを使用できます。

  1. 「ポップオーバーとして表示」セグエを使用する
  2. UIPopoverPresentationControllerDelegateプロトコルを採用するようにコントローラーを設定します
  3. 2つの方法を実装する

Objective-C:

- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
    return UIModalPresentationFullScreen;
}

- (UIViewController *)presentationController:(UIPresentationController *)controller viewControllerForAdaptivePresentationStyle:(UIModalPresentationStyle)style {
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController: controller.presentedViewController];
    return navController;
}

迅速:

UIPopoverPresentationControllerDelegate
    func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
        return UIModalPresentationStyle.FullScreen
    }

func presentationController(controller: UIPresentationController, viewControllerForAdaptivePresentationStyle style: UIModalPresentationStyle) -> UIViewController? {
    var navController = UINavigationController(rootViewController: controller.presentedViewController)
    return navController
}

スウィフト4:

extension MyViewController: UIPopoverPresentationControllerDelegate {
    func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
        return UIModalPresentationStyle.fullScreen
    }

    func presentationController(_ controller: UIPresentationController, viewControllerForAdaptivePresentationStyle style: UIModalPresentationStyle) -> UIViewController? {
        return UINavigationController(rootViewController: controller.presentedViewController)
    }
}

セグエメソッドの準備でデリゲートを設定します。

   override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
        if let adpostVC = segue.destinationViewController as? XXXController {
            let popPC = adpostVC.popoverPresentationController
            popPC?.delegate = self
10
user1760527

Swiftバージョン:

手順に従ってください:

  1. VCをNavigationControllerに埋め込みます
  2. PrepareForSegue()をオーバーライドします

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
      if segue.identifier == "goToYourController" {
        let navigation: UINavigationController = segue.destinationViewController as! UINavigationController
    
        var vc = YourViewController.init()
        vc = navigation.viewControllers[0] as! YourViewController
        //if you need send something to destnation View Controller 
        //vc.delegate = self
      }
    }
    

これを行う簡単な方法があります。前述のコメントのように(すべての手順を説明しませんでした)、目的のビューコントローラーをナビゲーションコントローラーに埋め込み、目的のビューコントローラーをナビゲーションコントローラーに設定し、ナビゲーションコントローラーが目的のビューコントローラーを呼び出します。

まず、VCをNavVCに埋め込みます。次に、セグエの宛先をNav VCに設定するコードを記述する必要があります。

コードは次のようになります。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    UINavigationController *nav = segue.destinationViewController;
    AddTriviaVC *triv = [[AddTriviaVC alloc]init];
    triv = nav.viewControllers[0]; 
    triv.location = self.location; 
} 

これが理にかなっていることを願っています。

4
jungledev

これはおそらく、モーダルにUINavigationControllerがないためです。使用する(またはStoryboardでViewControllerにナビゲーションバーを追加する)だけで、モーダルで表示する必要があります。

2
Levi

ストーリーボードでは、新しいviewControllerにNavigation Itemを追加し、完了ボタンにBar Button Itemを追加する必要があります

1
LE24

-(void)viewDidLoadで以下を実行することにより、プログラムでツールバーを追加できます。

NSInteger tbHeight = 50;
tb = [[UIToolbar alloc] initWithFrame:CGRectMake(0, (self.view.frame.size.height - tbHeight), self.view.frame.size.width, tbHeight)];
tb.translucent = YES;
emailButton = [[UIBarButtonItem alloc] initWithTitle:@"Email Results" style:UIBarButtonItemStyleBordered target:tvController action:@selector(actionSheet:)];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(doneButtonPressed:)];

NSArray *barButton  =   [[NSArray alloc] initWithObjects:emailButton,flexibleSpace,doneButton,nil];
[tb setItems:barButton];


[self.view addSubview:tb];

barButton = nil;

次に、完了ボタンを押すためのIBActionを作成する必要があります。これは次のように行われます。

-(IBAction)doneButtonPressed:(id)sender {
    [self dismissModalViewControllerAnimated:YES];
}

これで、モーダルビューコントローラーで必要なものが得られるはずです。

0
Stan Cromlish

Swift 4.2の「Volodymyr Nazarkevych」の短縮版です。

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    super.prepare(for: segue, sender: sender)
    switch(segue.identifier ?? "") {

    case "segueIdentifier": // name of your segue
        let navigation: UINavigationController = segue.destination as! UINavigationController

        //here you create new name of your destination ViewController and assign his name to let constant above
        guard let myNewViewController = navigation.viewControllers[0] as? DestinationViewController
       //DestinationViewController is your 2-ViewController where you go to from first one.

       else {
            fatalError("Unexpected destination: \(segue.destination)")
        }
       // TO DO SOMETHING HERE

   default:
        fatalError("Unexpected Segue Identifier; \(String(describing: segue.identifier))")
    }
}

「Volodymyr Nazarkevych」のコードは完全に機能しますが、セグエが2-ViewControllerに直接ではなく、1-ViewControllerから2-ViewControllerのNavigationControllerにある場合のみです。たくさんのVOVAをありがとう!。

また、宛先ブロックコードの後のスイッチケースでは、2番目のViewControllerから情報やファイルを取得するなど、さまざまなことを実行できます。

0
Alex D.