8 Replies Latest reply: Feb 9, 2009 4:03 PM by RayNewbie
Doug Lerner2 Level 4 Level 4 (1,030 points)
In an app originally derived from the Utility app, I want to sometimes set infoButton to hidden.

But because infoButton is defined in a different view - the "RootViewController.h" file I get compile errors if I try to change the hidden setting.

Is there some special syntax I need for this?

Thanks,

doug

MBP C2D 17", Mac OS X (10.5), 3.0 GB RAM
  • 1. Re: Setting a RootViewController element to hidden from MainViewController?
    RickMaddy Level 4 Level 4 (1,320 points)
    No special syntax. What is your code and what is the error?
  • 2. Re: Setting a RootViewController element to hidden from MainViewController?
    Doug Lerner2 Level 4 Level 4 (1,030 points)
    The syntax I've tried is just:


    infoButton.hidden = YES;


    I've also tried:


    [infoButton setHidden: YES]


    In either case, the error is:

    error: 'infoButton' undeclared (first use in this function)


    It is declared in RootViewController.h.

    It shows up in MainView, but I'm not sure why.

    Thanks,

    doug
  • 3. Re: Setting a RootViewController element to hidden from MainViewController?
    RickMaddy Level 4 Level 4 (1,320 points)
    Show the lines from RootViewController.h to show how it is declared.

    What file are you referencing infoButton from? You can't just access a variable from any file.

    Read the error. It's not your syntax that is the problem. You are trying to use a variable that can't be found.
  • 4. Re: Setting a RootViewController element to hidden from MainViewController?
    Doug Lerner2 Level 4 Level 4 (1,030 points)
    I know. That's why my question was really how to access the property of a variable declared in RootViewController from MainViewController.

    In RootController.h it is


    UIButton *infoButton;
    .
    .
    .
    @property (nonatomic, retain) IBOutlet UIButton *infoButton;


    and in RootController.m it is


    @synthesize infoButton;


    It isn't declared at all in MainViewController - but it is visible in my main view.

    So I know it isn't declared there and that is the problem. I just don't know how to "reach" it from my MainViewController methods. That's why I wondered if there was some extra syntax I could use to "reach beyond my current scope" or something.

    Or do I have to doubly declare it maybe?

    doug
  • 5. Re: Setting a RootViewController element to hidden from MainViewController?
    RickMaddy Level 4 Level 4 (1,320 points)
    If you have access to the RootController object in your MainViewController object then you can call the infoButton property on that. Like this:


    // Code inside MainViewController
    RootController *root = ??? // You need a way to get this
    root.infoButton.enabled = YES; // or NO
  • 6. Re: Setting a RootViewController element to hidden from MainViewController?
    Bill Anderson Level 1 Level 1 (30 points)
    Anyone have any thoughts on this?

    I am having the same difficulty.

    If you create a project using the Utility template, 3 ViewControllers are created; root, main and flipside.

    The RootViewContoller creates a UIButton called infoButton on the MainWindow.xib file. This button is used to toggle between the 2 sub views, MainView and FlipsideView. Each sub view has a viewController.

    Is there a way to hide the info button programmatically from a subview viewController? In this case, MainViewController.

    I've tried..

    self.view.superview.infoButton.hidden = YES;
    \[self.view.superview.infoButton setHidden:YES\];

    With no luck.

    Any thoughts?
  • 7. Re: Setting a RootViewController element to hidden from MainViewController?
    kiichi Level 1 Level 1 (0 points)
    quick and dirty
    ((UIView*)[self.view.superview.subviews objectAtIndex:1]).hidden = YES;
  • 8. Re: Setting a RootViewController element to hidden from MainViewController?
    RayNewbie Level 5 Level 5 (6,810 points)
    Hi Doug and Bill. I think Rick Maddy was trying to lead you to the solution instead of finishing his code for you, but I know how frustrating it is to just not see it.. Been there!!!

    Here's a snippet from my MainViewController.m where I'm doing something similar with the Utility template:


    #import "MyProgAppDelegate.h"
    #import "RootViewController.h"

    -(void)changeState:(int)iState {
    nState = iState;
    MyProgAppDelegate *delegate = [UIApplication sharedApplication].delegate;
    RootViewController *rootViewController = delegate.rootViewController;
    rootViewController.buMyControl.enabled = nState == 1 ? YES : NO;
    }


    Or, if you're doing lots of this you might consider declaring a RootViewController* instance variable (and matching property with retain) in MainViewController.h. You could then initialize that in RootViewController.m viewDidLoad:


    MainViewController *viewController = [[MainViewController alloc] initWithNibName:@"MainView" bundle:nil];
    viewController.rootViewController = self; // <--- here
    self.mainViewController = viewController;
    [viewController release];


    I think kiichi's solution will work too, but note that it depends on keeping the target control at index 1. That might break if you later add other controls to the root view as I did.