1
votes

I had the idea to subclass UIViewController as a base class for my subsequent controllers with the purpose of adding a "logout" button to the UINavigationController in my app delegate.

//MyAppDelegate.m
@synthesize navigationController; //UINavigationController

//-didFinishLaunchingWithOptions...
[self.window addSubview:navigationController.view];
...

In my MainWindow.xib I have a NavigationController attached to.. navigationController with DashboardViewController as its root view controller.

//BaseLogoutViewController.m

- (void)loadView {
    UIBarButtonItem *addButton = [[UIBarButtonItem alloc]
                               initWithTitle:NSLocalizedString(@"Logout", @"")
                               style:UIBarButtonItemStyleBordered
                               target:self
                               action:@selector(logoutPressed)];

   self.navigationItem.rightBarButtonItem = addButton;
   [addButton release];
   [super loadView];
}

// -(void)logoutPressed callback implemented
// viewDidLoad, didRecieveMemoryWarning, dealloc implemented

//DashboardViewController.h
@interface DashboardViewController : BaseLogoutViewController { }

DashboardViewController : UIViewController
DashboardViewController : UIViewController

DashboardViewController : BaseLogoutViewController
DashboardViewController : BaseLogoutViewController

The good news is it does show the logout button, but the dashboard does not show it's own view. BaseLogoutViewController does not have a Nib of its own. My question is, how come when I subclass BaseLogoutViewController it no longer shows my DashboardViewController's view?

2
try this and i hope u will get the solution for your problem. Please read all the comments given below for this link. stackoverflow.com/questions/4436278/uinavigation-problemsandy
I appear to be doing what that answer says. The only difference is I am adding the navigationController's subview which adds Dashboard's view because it is specified in the Nib. the problem occurs when I derive the dashboard from my custom UIViewController class.Tom Fobear

2 Answers

2
votes

things you have implied, but i'd like to confirm:

  • you are going to have more than one view controller pushed/popped onto the navigation controller?
  • you want the logout button on every view that gets pushed onto the navigation controller?

that said, loadView is not what you want to override, viewDidLoad is where you want to put the above code,

'loadView` is responsible for (oddly enough) loading the view, which it is not doing, by overriding it, you have prevented it from happening, you want to augment the view once it has been loaded,

0
votes

Try calling [super loadView] in your - (void)loadView method...that'll do the trick.