簡體   English   中英

從彈出窗口將UIViewController推送到UINavigationController

[英]Push UIViewController to UINavigationController from a popover

嘗試將新的視圖控制器推送到現有導航控制器時遇到問題。

我正在嘗試的是在按下導航UIBarButtonItem時使UIPopoverController出現,並從該“下拉列表”中選擇一個菜單點,該菜單點會將關聯的視圖控制器推到“主”導航控制器上。

我嘗試了以下方法,它給出了一個模態。 但我想推動觀點。 在此處輸入圖片說明 如果選擇推式而不是模態,結果如下。

在此處輸入圖片說明 我還嘗試制作了一個自定義UITableViewController (在彈出窗口上),並從中嘗試了以下代碼:

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UINavigationController *dash = [storyboard instantiateViewControllerWithIdentifier:@"dash_nav"];
    UIViewController *students = [storyboard instantiateViewControllerWithIdentifier:@"students"];

    if (indexPath.row == 0) {
       [dash pushViewController:students animated:YES];
//     [[dash navigationController] presentViewController:students animated:YES completion:nil];
    }
    NSLog(@"%@", [dash title]);
    NSLog(@"index = %i", indexPath.row);
}

有什么方法可以做我想完成的事情?

這段代碼:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UINavigationController *dash = [storyboard instantiateViewControllerWithIdentifier:@"dash_nav"];
UIViewController *students = [storyboard instantiateViewControllerWithIdentifier:@"students"];

正在創建太多新實例。 您應該使用現有的情節提要板( self. storyboard )和現有的導航控制器。 導航控制器需要傳遞給表視圖控制器(您應該使用此視圖控制器,因為情節提要沒有所需的信息)。 我們將其稱為originatingNavigationController ,這是表視圖控制器上的新@property

當segue觸發顯示彈出窗口時,將導航控制器引用設置為目標視圖控制器(表視圖)。

然后,在didSelectRowAtIndexPath:方法中,您只需實例化students VC並將其推送:

UIViewController *students = [self.storyboard instantiateViewControllerWithIdentifier:@"students"];
[self.originatingNavigationController pushViewController:students animated:YES];

然后表格視圖控制器應關閉自身(彈出窗口)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM