Skip navigation
This discussion is archived

Setting a RootViewController element to hidden from MainViewController?

3649 Views 8 Replies Latest reply: Feb 9, 2009 4:03 PM by RayNewbie RSS
Doug Lerner2 Level 4 Level 4 (1,030 points)
Currently Being Moderated
Sep 21, 2008 4:10 AM
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?


MBP C2D 17", Mac OS X (10.5), 3.0 GB RAM
  • RickMaddy Level 4 Level 4 (1,320 points)
    No special syntax. What is your code and what is the error?
    15" MacBook Pro Intel Core Duo 2.0GHz, 2GB RAM, Mac OS X (10.5.4), iPod touch 16GB, iPhone 16GB
  • 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.
    15" MacBook Pro Intel Core Duo 2.0GHz, 2GB RAM, Mac OS X (10.5.4), iPod touch 16GB, iPhone 16GB
  • 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
    15" MacBook Pro Intel Core Duo 2.0GHz, 2GB RAM, Mac OS X (10.5.4), iPod touch 16GB, iPhone 16GB
  • Bill Anderson Calculating status...
    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?
    MacBook Pro 2.4 GHz, Mac OS X (10.5.5)
  • kiichi Calculating status...
    quick and dirty
    ((UIView*)[self.view.superview.subviews objectAtIndex:1]).hidden = YES;
  • 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.
    MacBook, Mac OS X (10.5.4)


More Like This

  • Retrieving data ...

Bookmarked By (0)


  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.