簡體   English   中英

表視圖控制器顯示下一個視圖

[英]Table View View Controller Show Next View

我在故事板中使用表視圖控制器。

我目前在故事板中有一個嵌入導航控制器的tableview控制器。 此表視圖控制器鏈接到ObjectiveC文件。

通過代碼,我可以使用tableView didSelectRowAtIndexPath方法顯示另一個具有正確數據的表視圖控制器。

碼:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    WorkoutType *workoutType = (WorkoutType *)[self.fetchedResultsController objectAtIndexPath:indexPath];
    WorkoutSetViewController *detailViewController = [[WorkoutSetViewController alloc] initWithWorkoutType:workoutType];

    [self.navigationController pushViewController:detailViewController animated:YES];

}

但是,如果我嘗試在storyboard中添加另一個表視圖控制器,將其與objectiveC視圖控制器文件關聯,並將第一個TVC連接到它,在運行代碼時不顯示該表視圖控制器。

我需要直觀地自定義第二個表視圖並嘗試使用segue push ...如下所示:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"show"]) {



        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];

        WorkoutType *workoutType = (WorkoutType *)[self.fetchedResultsController objectAtIndexPath:indexPath];
        //WorkoutSetViewController *detailViewController = [[WorkoutSetViewController alloc] initWithWorkoutType:workoutType];
        WorkoutSetViewController *destViewController = segue.destinationViewController;
        destViewController.workoutType = workoutType;
         //[self.navigationController pushViewController:detailViewController animated:YES];


    }
}

但是,當我運行此代碼時,第二個表視圖控制器甚至不加載,盡管應用程序不會崩潰。

無論我做錯什么都必須簡單,任何幫助都會受到贊賞。

謝謝

簡單您必須瀏覽下面的代碼。 (在同一個故事板ViewController中工作時無需設置故事板)

(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  
{  
    if(indexpath.row==0)//for first row click  
    {  
         WorkoutSetViewController *objWorkoutSetVC=[[WorkoutSetViewController alloc] i  nitWithNibName:@"WorkoutSetViewController" bundle:nil];  
        [self.navigationController pushViewController:objWorkoutSetVC animated:YES];  
    }  
    else if(indexpath.row==1)//for second row click  
    {  
        //Put code in which screen you have to navigate  
    }  
}  

不要 ALLOC初始化新視圖控制器。 請改用此代碼:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
WorkoutSetViewController *detailViewController = (WorkoutSetViewController *)[storyboard instantiateViewControllerWithIdentifier:@"coolID"];
[self.navigationController pushViewController:detailViewController animated:YES];

對於MainStoryboard編寫故事板名稱,並為coolID編寫您在故事板中設置的視圖控制器ID名稱。

暫無
暫無

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

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