XCode iPhone:从子 UIScrollView 隐藏父 UIViewController 中的 UIToolbar

发布于 2024-08-18 10:35:05 字数 1610 浏览 9 评论 0原文

我有一个类似于Apple的PageControl示例的项目。我有一个 UIViewController“PhotoViewController”,其中包含 UIScrollView 和 UIToolbar。 UIScrollView 加载另一个 XIB 和 UIViewController“PhotoScrollViewController”。

在 PhotoScrollViewController 中,我有一个显示图像的 UIButton。我在这个按钮上有一个 IBAction,我想单击它来显示/隐藏 PhotoViewController 中的 UIToolbar。

在 PhotoViewController.h 中,我

@interface PhotoViewController : UIViewController <UIScrollViewDelegate> {
  IBOutlet UIToolbar *toolBar;
  ..
}

@property (nonatomic, retain) UIToolbar *toolBar;

在 PhotoScrollViewController 中尝试了一些操作,例如在 PhotoScrollViewController.h 中导入 PhotoViewController.h 并将其添加到界面中,然后尝试通过我的函数访问它,例如:

@implementation PhotoScrollViewController

- (IBAction)toggleMenu {
  photoViewController.toolBar.hidden = NO;
}

但这不起作用。我还尝试了removeFromSuperView和self.parentViewController以及其他一些东西。我不知道如何隐藏这个工具栏(我也尝试过阿尔法,但我根本无法访问工具栏)。

我尝试使用 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;
}

我也尝试将工具栏添加到 PhotoScrollViewController,并且我可以切换它,但我无法弄清楚如何告诉主 UIViewController 关闭 PhotoViewController...所以无论我以哪种方式解决这个问题,我都不知道如何在 UIViewController 之间正确通信..并且我阅读的文档似乎遵循我的尝试。

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".

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.

In PhotoViewController.h I have

@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:

@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).

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):

@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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

飘过的浮云 2024-08-25 10:35:06

基本上,您的问题似乎是您无法从另一个控制器引用一个控制器。这可以通过多种方式来完成。

1:加载应用程序时在应用程序委托中创建控制器,并公开对它们的引用,例如作为委托的属性。

2:在创建或显示 PhotoScrollViewController 时传递对 PhotoViewController 的引用。你说这不起作用:

 @implementation PhotoScrollViewController

 - (IBAction)toggleMenu {
     photoViewController.toolBar.hidden = YES;
 }

如果你的 photoViewController 引用为零,那么这可能不起作用的唯一方法。在哪里/如何设置它?

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.

2: Pass references to the PhotoViewController when creating or showing the PhotoScrollViewController. You say this doesn't work:

 @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?

Spring初心 2024-08-25 10:35:06

[photoViewController.navigationController setToolbarHidden:是动画:否];

仅当 photoViewController 被推入 UINavigationController 时它才有效。否则,设置 toolBar.hidden=YES。

[photoViewController.navigationController setToolbarHidden:YES animated:NO];

It works only if photoViewController is pushed into a UINavigationController. Otherwise, set toolBar.hidden=YES.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文