在OS X中使用NSViewControllers在视图之间切换

时间:2015-01-21 21:15:31

标签: objective-c macos xcode6 nsviewcontroller

如何使用视图控制器在IBAction的帮助下在多个视图之间切换?

这是我到目前为止编写的代码:

在接口文件中我做了一个IBAction:

-(IBAction)Next;

在实施文件中我写了这个:

- (IBAction)Next
{
  SecondPopovercontrol *second = [[SecondPopovercontrol alloc] initWithNibName:nil bundle:nil];
  [self presentingViewController:second animated: YES completion:NULL]; // This is the step I am facing issues with
}

我无法运行 - 两个视图控制器的名称是:WOMPopoverControllerSecondPopovercontrol

错误是:

“没有可见的@interface for WOMPopoverController声明选择器呈现Viewcontroller:animated:completion”

我是StackExchange的新手,所以如果我的问题有任何不一致之处,请告诉我。并且,请指导我如何解决这个问题。

谢谢

1 个答案:

答案 0 :(得分:3)

presentingViewController是UIViewController的一个属性。它包含一个指向viewController的指针,该指针呈现 ed 当前的viewController(如果此viewController未呈现viewController,则为nil)。

您要做的是向presentViewController:animated:completion发送self条消息:

[self presentViewController:second animated:YES completion:nil]; 

(现在'现在',没有'呈现')

当显示second时,它的presentingViewController属性将设置为此自我,并且自我presentedViewController属性将设置为second }

修改
正如Jef指出的那样,我错误地认为你正在处理iOS。在OSX上,方法类似。 NSViewController具有presentingViewController属性和presentedViewControllers 数组。您可以使用的各种呈现方法有所不同:

– presentViewController:animator:
– presentViewController:asPopoverRelativeToRect:ofView:preferredEdge:behavior:
– presentViewControllerAsModalWindow:
– presentViewControllerAsSheet:

在任何情况下,我认为您需要仔细考虑NSViewController documentation,以确保您使用正确的方法来获得正确的平台(您的错误方法似乎来自iOS,而不是OSX)。

编辑2
我已经制作了一个small demo app来展示OSX上NSViewController的各种表现行为,包括一个非常基本的自定义动画对象。