Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.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
Objective c 从ViewControllerB显示ViewControllerA_Objective C_Ios_Uiviewcontroller_Uiapplicationdelegate - Fatal编程技术网

Objective c 从ViewControllerB显示ViewControllerA

Objective c 从ViewControllerB显示ViewControllerA,objective-c,ios,uiviewcontroller,uiapplicationdelegate,Objective C,Ios,Uiviewcontroller,Uiapplicationdelegate,我有一个rootController和5个contentcontroller,每个都作为自己的类 我希望能够从当前内容控制器调用下一个内容控制器。例如,如果我当前正在显示contentController1,我需要一种显示contentController2的方法 如果我可以在每个控制器的实现文件中添加一个短方法,将要调用的控制器的编号传递给加载和显示新控制器的实际方法,那将是一个理想的选择 例如: @implementation ContentController1 - (int) load

我有一个rootController和5个contentcontroller,每个都作为自己的类

我希望能够从当前内容控制器调用下一个内容控制器。例如,如果我当前正在显示contentController1,我需要一种显示contentController2的方法

如果我可以在每个控制器的实现文件中添加一个短方法,将要调用的控制器的编号传递给加载和显示新控制器的实际方法,那将是一个理想的选择

例如:

@implementation ContentController1

- (int) loadNextController {
  //take the 1 from ContentController1, add 1 to it, and pass it somewhere else
}
那么根控制器在其他地方?应用程序代理?添加以下方法,然后根据int-loadNextController方法发送的int加载并显示contentController:

-(void) loadNextController: (int) nextController {
//init and show controller
}
如果你能给我看一下代码,更重要的是,它的去向,我会非常感激


Trevor

不清楚您希望这些视图控制器如何相互关联。例如,您可能希望依次将每个视图控制器推送到导航控制器的堆栈上,以便用户始终可以选择返回以前的视图控制器。或者,您可能有一个要求,即用户必须连续通过五个控制器,而不选择返回,在这种情况下,您可能会在时间到来时将窗口的rootViewController属性设置为每个控制器。您的决定将决定如何编写表示每个控制器视图的代码。详情请阅读

上面描述的设计中,每个视图控制器都决定下一个显示哪个视图控制器。这通常是合适的,尤其是在视图控制器形成层次结构的情况下。不过,从您的描述来看,集中所有关于控制器在一个对象中呈现顺序的知识可能会有所帮助。使该对象成为每个控制器的委托,并让该对象(可能是应用程序委托)确定控制器的顺序。作为一个未经测试的示例,您可以将以下内容添加到您的应用程序委派中:

-(void)application:(UIApplication*)app didFinishLaunchingWithOptions:(NSDictionary*)options
{
    //...
    self.controllers = [NSArray arrayWithObjects:
                   [[[ViewControllerA alloc] initWithNibName:nil bundle:nil] autorelease],
                   [[[ViewControllerB alloc] initWithNibName:nil bundle:nil] autorelease],
                   //...
                   [[[ViewControllerN alloc] initWithNibName:nil bundle:nil] autorelease]];

    // Make self the delegate for each of the view controllers.
    [self.controllers setValue:self forKey:@"delegate"];

    [self.navigationController pushViewController:[self.controllers objectAtIndex:0] animated:NO];
}

-(void)viewControllerDidFinish:(UIViewController*)controller
{
    NSUInteger index = [self.controllers indexOfObject:controller];
    NSUInteger nextIndex = index + 1;

    if (nextIndex < [self.controllers count]) {
        [self.navigationController pushViewController:[self.controllers objectAtIndex:nextIndex animated:YES];
    }
}
同样,这里的想法是,数组中视图控制器的顺序决定了控制器的显示顺序。我在这里用过导航控制器,但你不必。此外,您可能希望使用-viewControllerdFinish:UIViewController*控制器方法声明协议,在应用程序委托或管理控制器的任何对象中采用该协议,并让控制器的委托属性指定该协议。这样可以避免关于委托未实现该方法的任何警告

[self.delegate viewControllerDidFinish:self];