Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 管理父视图控制器中的几个UIViewController_Ios_Uiviewcontroller - Fatal编程技术网

Ios 管理父视图控制器中的几个UIViewController

Ios 管理父视图控制器中的几个UIViewController,ios,uiviewcontroller,Ios,Uiviewcontroller,我有UIViewController,其中包含另外两个UIViewController作为属性 MenuViewController包含: @property (nonatomic, strong) TeamsViewController *teamsViewController; @property (nonatomic, strong) ResultsViewController *resultsViewController; MenuViewController包含表视图,当用户点击单

我有
UIViewController
,其中包含另外两个
UIViewController
作为属性

MenuViewController包含:

@property (nonatomic, strong) TeamsViewController *teamsViewController; 
@property (nonatomic, strong) ResultsViewController *resultsViewController;
MenuViewController包含表视图,当用户点击单元格“show teams”时,我需要初始化teamsViewController并显示teamsViewController视图。当用户按下单元格“ShowResults”(显示结果)时也是如此,但在这种情况下,我需要显示resultsViewController视图

所以,我通常这样做是在按下单元格时初始化控制器,并调用addSubview方法来添加控制器视图。但我认为这不是一个好的解决方案,对吗

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_teamsViewController) _teamsViewController = nil;
    _teamsViewController = [TeamsViewController new]
    [self.view addSubView:_teamsViewController];

}
上面的方法行吗

这是我的视图层次结构,每个视图都由自己的控制器管理。因此,由MenuViewController管理的白色视图和由ResultViewController管理的灰色视图以及由TeamsViewController管理的蓝色视图

正如我之前所说,当我点击菜单视图控制器中的相应单元格时,我需要显示团队或结果。但每个视图都有另一个视图控制器。或者我对视图控制器范例感到困惑?也许TeamsViewController应该是TeamsView,ResultsViewController应该是ResultsView?因此,两个视图控制器都有在其控制器中管理的表。所以我不认为它必须是UIView而不是UIViewController


我建议使用情节提要,并确保每个情节提要都有一个“情节提要ID”。这样,根据需要推送各种
UIViewController
实例就更容易了。这是我的典型模式:

UIViewController *vc = [self.storyboard
    instantiateViewControllerWithIdentifier:@"selected identifier"];
[self.navigationController pushViewController:vc animated:YES];
此外,除非需要在子视图控制器中设置属性,否则不需要对其进行属性引用。此外,此答案假设您使用的是
UINavigationController
,而
MenuViewController
设置为
rootViewController
。您可以使用故事板在IB中进行设置(简单,可能是首选方式),也可以使用如下代码:

MenuViewController *vc = [[UIViewController alloc] initWithNibName:@"" 
                                                            bundle:nil];
UINavigationController *navVc = [[UINavigationController alloc]
                                  initWithRootViewController:vc];
//MenuViewController.h
@interface MenuViewController : UINavigationController
   @property (nonatomic, strong) TeamsViewController *teamsViewController; 
   @property (nonatomic, strong) ResultsViewController *resultsViewController;
   //Insert other properties and methods....

@end

我想你最好的办法是把它设置成一个UINavigationController。UINavigationController继承自UIViewController,因此不会以这种方式丢失任何功能。然后您可以这样设置:

MenuViewController *vc = [[UIViewController alloc] initWithNibName:@"" 
                                                            bundle:nil];
UINavigationController *navVc = [[UINavigationController alloc]
                                  initWithRootViewController:vc];
//MenuViewController.h
@interface MenuViewController : UINavigationController
   @property (nonatomic, strong) TeamsViewController *teamsViewController; 
   @property (nonatomic, strong) ResultsViewController *resultsViewController;
   //Insert other properties and methods....

@end
在有人单击单元格时调用的方法中,您只需执行以下操作:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (!self.teamsViewController) {
       self.teamsViewController = [[UIViewController alloc] initWithNibName:@"nibName" bundle:nil];
    }
    [self pushViewController:self.teamsViewController animated:YES];
}
现在,您有两个视图控制器,因此您必须告诉您的方法,在上面哪个视图控制器要推送到堆栈上。如果行是固定的,您可以根据
indepath
简单地决定显示哪一行,但是如果它更动态(即tableview是从数据库创建的),那么您需要在这里有一些更复杂的逻辑代码

在不做太多假设的情况下,为了给您一些一般性的指导,当您创建一个单元格时,您通常会在其上设置某种标志来指示它是什么类型。如果只有两个状态,则可以使用NS_枚举或简单BOOL(我更喜欢尽可能使用枚举,因为它们更具描述性)。然后在
tableView:didSelectRowAtIndexPath:
方法中检查该标志是否存在,并将相应的视图推送到导航堆栈上。类似这样的代码(这不是文字代码,只是为了让您了解:

// This code assumes in the method tableView:cellForRowAtIndexPath:
// you have set the tag property to '1' if a TeamsViewController is
// needed or '2' if a ResultsViewController is needed when that cell
// is pressed.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        self.teamsViewController = self.teamsViewController ? self.teamsViewController : [[UIViewController alloc] initWithNibName:@"TeamsViewController" bundle:nil];
        self.resultsViewController = self.resultsViewController ? self.resultsViewController : [[UIViewController alloc] initWithNibName:@"ResultsViewController" bundle:nil];
        switch ([[tableView cellForRowAtIndexPath:indexPath] tag]) {
           case 1:
              [self pushViewController:self.teamsViewController animated:YES];
              break;
           case 2:
              [self pushViewController:self.resultsViewController animated:YES];
              break;
           default:
              break;
        }
    }

您仍然需要在团队或结果视图控制器中进行初始化以显示视图,但这将引导您朝着大致的方向前进。

使用uinavigationcontroller作为MenuViewController的基类,并将其他视图控制器推到导航堆栈上可能更容易。这将为您提供内置的“ba”ck'功能也一样。@MySpecialPurpose但在这种情况下,我需要在主视图控制器视图上添加uinaviagtioncontroller视图,对吗?它可以是不同的设备,但主要的是,表视图位于顶部站点,在底部,我有一些类似于容器视图的内容,我通过添加视图控制器视图来实现。@MySpecialPurpose我喜欢导航的想法,因此我可以添加一个视图,然后只需推另一个视图控制器当我将navigationcontroller视图作为suview添加到我的视图中时,我使用此代码,然后我就可以看到整个视图[_containerNavigationController.view setFrame:CGRectMake(247,64,10000,10000)];如果我使用700表示高度,我推的视图会被截断,但其视图大小为700 x 700。你是否遇到过同样的问题?如果你看到分割视图控制器,这是我想问的问题。因此,但它也可以用于iPhone,所以我们有一些组件,其中有两个视图,一个主视图,另一个视图控制器,但每个视图都在屏幕上屏幕上,用户每次都可以看到2个视图控制器的两个视图在这种情况下,您需要设置多个故事板;一个用于iPhone,一个用于iPad,为每个设备创建适当的UI。您不能使2个
UIViewController
实例在iPhone上同时可见。至少需要使用
UIViewController
实例全屏显示;请检查我已实现我要查找的源代码。你能对其进行评论吗?请检查源代码。这就是我要查找的源代码。你能对其进行评论吗?我签出了你的代码。尝试将你的视图控制器设置为从UIViewController中的UINavigationController继承。m你通常不会放置导航控件呃,在子视图位置,它应该是根控制器,并且应该负责。它可能会工作。我从来没有这样使用过它。只要测试它,确保它不会损坏,并且你应该没事。祝你好运!欢迎投票:)