views:

1311

answers:

1

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.

+1  A: 

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?

Adam Woś
I am referencing photoViewController in PhotoScrollViewController.h as: #import <UIKit/UIKit.h> #import "PhotoViewController.h" @interface PhotoScrollViewController : UIViewController { PhotoViewController *photoViewController; } @end
Ryan
photoViewController==nil is true, so that must be my problem.
Ryan
As i said in my response, you need to set the reference somehow. Try to assign it just after you `[[PhotoScrollViewController alloc] init...]` or something similar. The `PhotoViewController` will need to be exposed as a `@property`.
Adam Woś
I just set the reference by adding a view controller in IB, and creating an @property in PhotoScrollViewController.h and linking them in IB. photoViewController is no longer nil, and I can verify that I can execute a function in PhotoViewController through NSLog. toolBar.hidden = NO in PhotoViewController still has no effect unless I execute it from a button in PhotoViewController. I could get this before using [[PhotoViewController alloc] toggleTopMenu]; I'm really not sure what to do from here. photoViewController.toolBar.hidden = NO doesn't work either, even after the reference is set.
Ryan
Also just tried PhotoViewController *theController = [[PhotoViewController alloc] init]; theController.toolBar.hidden = NO; and still no effect. Not sure what I'm doing wrong here. Is my toolbar not available? Do I need to set the properly differently then @property (nonatomic, retain) UIToolbar *toolBar;
Ryan
In that example, theController.toolBar==nil. toolBar is set in PhotoViewController as IBOutlet UIToolbar *toolBar; and the property statement above. It is synthesized in PhotoViewController.m.
Ryan
If I execute a function that toggles the toolbar in PhotoViewController.m from a toolbar button, it works and NSLog dumps <UIToolbar: 0x4d27ac0; frame = (0 436; 320 44); hidden = YES; opaque = NO; autoresize = W+TM; layer = <CALayer: 0x4d28090>>. If I execute the same function from a button in PhotoScrollViewController NSLog dumps (null). Somehow the toolBar reference gets lost when I run it from PhotoScrollViewController.
Ryan
Is the reference to the PhotoViewController the same? BTW, the code here: `[[PhotoViewController alloc] toggleTopMenu];` is incorrect! You're leaking an object, and you're sending a message to a non-`init`ed object! Don't do this.
Adam Woś
Thanks Adam, I appreciate your help. I've given you so much info it's clouded the problem, so if you're still game, please see my last comment above yours. I'm properly referencing PhotoViewController because I can NSLog my toggleMenu function and successfully run it from PhotoScrollViewController. The issue is that toolBar is null when I execute it from a button in PhotoScrollViewController and not null when I execute it from PhotoViewController.
Ryan
Only thing that comes to my mind is to check if the PhotoViewController reference (not the toolbar, but the whole view) is not-null when you execute it from the button in PScrollVC. If it's not null, I'd write a setter for toolbar property and set a breakpoint there and check in debug why it's being set to nil.
Adam Woś