[ Is that right if I switch View in this way... (IPhone) ]
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... ...
Answer 1
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.
Answer 2
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.
Answer 3
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];
Answer 4
ViewBController *vc = [[[ViewBController alloc] initWithNib:@"ViewBController"
mainBundle:nil] autorelease];
MyAppDelegate *appDelegate = (MyAppAppDelegate *)[UIApplication sharedApplication].delegate;
[appDelegate.rootController presentModalViewController:vc animated:YES];