xcode 调用 rootViewController 在内容视图中切换视图 (iOS)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4982279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call rootViewController to switch views within content view (iOS)
提问by eriknelson
I'm working on a pretty simple multiview app for the iOS and I've been following a great tutorial in an Apress book. I've basically got my rootViewController instantiated and displayed with the app delegate, and I've got a number of content viewControllers (6) which I'd like to swap in and out based on user input. However, in the book they perform their switches with a button on a toolbar placed in the rootView using Interface Builder. It fires a method in rootView that loads up the new content ViewController and displays it.
我正在为 iOS 开发一个非常简单的多视图应用程序,我一直在学习Apress 书中的一个很棒的教程。我基本上已经实例化了我的 rootViewController 并与应用程序委托一起显示,并且我有许多内容 viewControllers (6) 我想根据用户输入换入和换出。然而,在书中,他们使用界面生成器通过放置在 rootView 中的工具栏上的按钮来执行切换。它在 rootView 中触发一个方法来加载新的内容 ViewController 并显示它。
My problem is that I'd like to perform the content view switch (that lies in my rootViewController instance), but I'd like to trigger the switch action with a button that's in my content view (and is therefore unavailable as my File Owner is my contentViewController, whose reference is held inside my rootViewController).
我的问题是我想执行内容视图切换(位于我的 rootViewController 实例中),但我想用我的内容视图中的按钮触发切换操作(因此作为我的文件所有者不可用是我的 contentViewController,它的引用保存在我的 rootViewController 中)。
Hopefully I've explained it well enough, please let me know if I should elaborate more. I appreciate any help!
希望我已经解释得足够好,如果我应该详细说明,请告诉我。我感谢任何帮助!
回答by Andy S.
You need to pass down a reference to your root view controller (RootViewController *rootViewController) when you create your content view either in a custom init method or by just assigning it after you created it: self.contentView.rootViewController = self;.
当您在自定义 init 方法中创建内容视图或在创建它后仅分配它时,您需要传递对根视图控制器 (RootViewController *rootViewController) 的引用:self.contentView.rootViewController = self; .
Now inside your content view you can then call the appropriate method in the root view controller to do the switch: [self.rootViewController switchView]. This call then can be triggered inside the method that is called when you press the button (IBAction method).
现在在您的内容视图中,您可以在根视图控制器中调用适当的方法来进行切换:[self.rootViewController switchView]。然后可以在按下按钮时调用的方法(IBAction 方法)内触发此调用。
So this is what you need to do: 1) Create a property inside the your content view controller of type RootViewController
所以这就是你需要做的:1)在你的内容视图控制器中创建一个 RootViewController 类型的属性
@class RootViewController;
@interface MyContentViewController : NSObject {
@private
RootViewController *rootViewController;
}
@property (retain) RootViewController *rootViewController;
and make sure it retains the reference. 2) Synthesis the property and add the callback to the root view controller that switches the view:
并确保它保留参考。2)合成属性,在切换视图的根视图控制器中添加回调:
@implementation MyContentViewController
@synthesize rootViewController;
- (IBAction) switchView:(id) sender {
[rootViewController switchToNextView];
}
-(void) dealloc {
[rootViewController release];
[super dealloc];
}
Also release your retain reference at the end.
最后还要释放您的保留引用。
3) Assign the root view controller to the content view inside your RootViewController:
3) 将根视图控制器分配给 RootViewController 内的内容视图:
self.contentViewController = [[[MyContentViewController alloc]
initWithNibName:@"ContentView"
bundle:nil] autorelease];
self.contentViewController.rootViewController = self;
That should be all. I hope that helps you.
这应该是全部。我希望这对你有帮助。
回答by mackworth
Well, you could simply create an IBAction in each of your child controllers that calls:
好吧,您可以简单地在每个调用的子控制器中创建一个 IBAction:
[self.parentViewController switchToDifferentController:(int) viewNumber]
and then implement the switchToDifferentController method in your root. Other than ignore the compiler warning that parentView might not implement that method, it might work.
然后在你的根目录中实现 switchToDifferentController 方法。除了忽略 parentView 可能没有实现该方法的编译器警告之外,它可能会起作用。
However, that is a bit brittle, as you'd have to assume that it was the parent calling you and that nobody will forget to implement that method.
但是,这有点脆弱,因为您必须假设是父级在调用您,并且没有人会忘记实现该方法。
In general, you use the "delegate" concept for a child controller to ask its parent to do something. The general idea is that you declare a group of methods as a "protocol". Think of it as a contract between objects. One object can say "I promise to implement these methods," and another can then choose to send those messages to it. The contract allows the compiler/system to check for conformance. You'll see this in UITableView, where the OS provides a standard table, but it calls back to your code to provide the individual cells as needed.
通常,您对子控制器使用“委托”概念来要求其父控制器做某事。一般的想法是将一组方法声明为“协议”。将其视为对象之间的契约。一个对象可以说“我保证实现这些方法”,然后另一个可以选择向它发送这些消息。契约允许编译器/系统检查一致性。您将在 UITableView 中看到这一点,其中操作系统提供了一个标准表格,但它会回调您的代码以根据需要提供单个单元格。
To implement a protocol, you mustdo the following: (See code segments below
要实现协议,您必须执行以下操作:(请参阅下面的代码段
- Declares a protocol for the conversation
- Specify that the parent will follows that protocol
- Create a delegate property in your child
- When the parent is about to launch the child, it assigns itself as the delegate for that child.
- When the child wants to switch, it calls the parent using that protocol
- 声明对话协议
- 指定父级将遵循该协议
- 在您的孩子中创建委托属性
- 当父级即将启动子级时,它会将自己分配为该子级的委托。
- 当孩子想要切换时,它会使用该协议调用父母
@protocol myVCDelegate
- (void)switchToDifferentController:(int) viewNumber ;
@end
@interface ParentViewController : UIViewController <VCDelegate>
@property(nonatomic, assign) id <VCDelegate> delegate
childController.delegate = self;
[self.delegate switchToDifferentController:kController5];