views:

133

answers:

4

I have a MyAppAppDelegate, it contains a window, and a UITabBarController.

@interface MyAppAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    IBOutlet UITabBarController *rootController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *rootController;
@end

And I have View A, that contain a button to switch to View B. It is the .h file:

#import <UIKit/UIKit.h>
@class MyAppAppDelegate;
@class ViewBController;


@interface ViewAController : UIViewController {
    IBOutlet UIView *view;
    IBOutlet UIButton *switchToViewBButton;
}
@property (retain, nonatomic) UIView *view;
@property (retain, nonatomic) UIButton *switchToViewBButton;

-(IBAction) startSwitching: (id)sender;

@end

And it is the.m file:

#import "ViewAController.h"
#import "ViewBController.h"

#import "MyAppAppDelegate.h"

@implementation ViewAController

/*skip the default generated codes*/

-(IBAction) startClock: (id)sender{
    NSLog(@"Start Switching");

    [rootController presentModalViewController:ViewBController animated:YES];
}

Plz notice that the ViewB is not enable to display on UITabBarController, it only appear, when the ViewA button is clicked. Also, I found that the debugger tell me that the rootController is undeclared. but I already import MyAppDelegate to the file. thz a lot... ...

A: 

No you need to do something like this:

ViewBController* vc = [[ViewBController alloc] initWithNib: @"ViewBController" mainBundle: nil];
if (vc != nil) {
    [rootController presentModalViewController: vc animated:YES];
    [vc release];
}

The mistake that you are making is that you are passing presentModalViewController: the class of the ViewBController. Instead it needs an instance.

St3fan
umum, I still get the "rootController is undeclared" error. Also, it warns me that that is no -initWithNib:mainBundle: method found, wt's going on? thz in advance.
Tattat
Yeah it is `initWithNibName:bundle:`. Try `self.tabBarController` instead of `rootController`.
St3fan
+1  A: 

You need to synthesize the rootController instance:

@synthesize rootController;

Then it should work. Put this line of code below the implementation line in the .m file. There is no reason why you should be getting the second error, so try my solution and then tell us what happened. Also, please try to write in complete sentences. In my experience, if you write well in a forum post, you will gain more respect from people who might help you.

James
That won't work as rootController is a property of `MyAppAppDelegate` not `ViewAController`, where OP wants to use it.
Brandon Bodnár
@James try you write sentences complete, not first language English many people lighten up and also give answers that actually help.
Yar
Sorry I was wrong. I'm pretty sure they didn't teach you plz or thz in english class.
James
A: 
ViewBController* viewBController = [[[ViewBController alloc] initWithNibName: @"NameOfViewBControllerNibFile" bundle:nil] autorelease];
[self presentModalViewController:viewBController animated:YES];

You can not access rootController from ViewAController, because it is a property of MyAppAppDelegate, not ViewAController. If you want to access the UITabBarController in charge of ViewAController, then inside ViewAController you use self.tabBarController

So if you want the UITabBarController to do the above, change it to

ViewBController* viewBController = [[[ViewBController alloc] initWithNib: @"NameOfViewBControllerNibFile" mainBundle: nil] autorelease];
[self.tabBarController presentModalViewController:viewBController animated:YES];
Brandon Bodnár
A: 
ViewBController *vc = [[[ViewBController alloc] initWithNib:@"ViewBController"
                                                 mainBundle:nil] autorelease];
MyAppDelegate *appDelegate = (MyAppAppDelegate *)[UIApplication sharedApplication].delegate;
[appDelegate.rootController presentModalViewController:vc animated:YES];
iwat