XCode iPhone:从子 UIScrollView 隐藏父 UIViewController 中的 UIToolbar
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2088834/
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
XCode iPhone: hiding UIToolbar in parent UIViewController from child UIScrollView
提问by
I have a project similar to Apple's PageControl example. I have a UIViewController "PhotoViewController" which contains a UIScrollView and a UIToolbar. The UIScrollView loads another XIB and UIViewController "PhotoScrollViewController".
我有一个类似于 Apple 的 PageControl 示例的项目。我有一个 UIViewController“PhotoViewController”,它包含一个 UIScrollView 和一个 UIToolbar。UIScrollView 加载另一个 XIB 和 UIViewController "PhotoScrollViewController"。
In PhotoScrollViewController, I have a UIButton which displays an image. I have an IBAction on this button, and I would like to click on it to show/hide the UIToolbar in PhotoViewController.
在 PhotoScrollViewController 中,我有一个显示图像的 UIButton。我在这个按钮上有一个 IBAction,我想点击它来显示/隐藏 PhotoViewController 中的 UIToolbar。
In PhotoViewController.h I have
在 PhotoViewController.h 我有
@interface PhotoViewController : UIViewController <UIScrollViewDelegate> {
IBOutlet UIToolbar *toolBar;
..
}
@property (nonatomic, retain) UIToolbar *toolBar;
I have tried a few things in PhotoScrollViewController, such as importing PhotoViewController.h in PhotoScrollViewController.h and adding it to the interface, then attempting to access it through my function like:
我在 PhotoScrollViewController 中尝试了一些东西,例如在 PhotoScrollViewController.h 中导入 PhotoViewController.h 并将其添加到界面,然后尝试通过我的函数访问它,例如:
@implementation PhotoScrollViewController
- (IBAction)toggleMenu {
photoViewController.toolBar.hidden = NO;
}
But this doesn't work. I've also tried removeFromSuperView, and self.parentViewController, and some other things. I am not sure how to make this toolbar hidden (I've tried alpha as well, I just can't access the toolbar at all).
但这不起作用。我也试过 removeFromSuperView、self.parentViewController 和其他一些东西。我不知道如何隐藏这个工具栏(我也试过 alpha,我根本无法访问工具栏)。
I tried adding a function to PhotoViewController instead, using toolBar.hidden = YES. This works if I execute the function from PhotoViewController, but it doesn't work if I access it from PhotoScrollViewController (with PhotoViewController *photoViewController in .h):
我尝试使用 toolBar.hidden = YES 向 PhotoViewController 添加一个函数。如果我从 PhotoViewController 执行该函数,这会起作用,但如果我从 PhotoScrollViewController 访问它(在 .h 中使用 PhotoViewController *photoViewController),则它不起作用:
@implementation PhotoScrollViewController
- (IBAction)toggleMenu {
photoViewController.toolBar.hidden = NO;
[photoViewController toggleTopMenu];
[[PhotoViewController alloc] toggleTopMenu];
}
@implementation PhotoViewController
- (IBAction)toggleTopMenu {
toolBar.hidden = NO;
}
I also tried adding the toolbar to PhotoScrollViewController instead, and I can toggle it, but I can't figure out how to tell the main UIViewController to dismiss PhotoViewController... so whichever way I attack this problem I don't know how to communicate properly between UIViewControllers.. and the documentation I read seems to follow what I've tried.
我也尝试将工具栏添加到 PhotoScrollViewController 中,我可以切换它,但我不知道如何告诉主 UIViewController 关闭 PhotoViewController ......所以无论我如何解决这个问题,我都不知道如何沟通在 UIViewControllers 之间正确地......我阅读的文档似乎遵循了我的尝试。
回答by Adam Wo?
Basically, your problem seems to be you cannot reference one controller from another. This can be done in various ways.
基本上,您的问题似乎是您无法从另一个控制器引用一个控制器。这可以通过多种方式完成。
1: Create your controllers in your application delegate when loading the application, and expose references to them e.g. as properties of the delegate.
1:在加载应用程序时在应用程序委托中创建控制器,并公开对它们的引用,例如作为委托的属性。
2: Pass references to the PhotoViewController when creating or showing the PhotoScrollViewController. You say this doesn't work:
2:在创建或显示 PhotoScrollViewController 时传递对 PhotoViewController 的引用。你说这不起作用:
@implementation PhotoScrollViewController
- (IBAction)toggleMenu {
photoViewController.toolBar.hidden = YES;
}
The only way this may not work if if your photoViewController reference is nil. Where/how do you set it?
如果您的 photoViewController 引用为零,则这可能不起作用的唯一方法。你在哪里/如何设置它?
回答by Wayne Lo
[photoViewController.navigationController setToolbarHidden:YES animated:NO];
[photoViewController.navigationController setToolbarHidden:YES 动画:NO];
It works only if photoViewController is pushed into a UINavigationController. Otherwise, set toolBar.hidden=YES.
仅当 photoViewController 被推入 UINavigationController 时才有效。否则,设置 toolBar.hidden=YES。