user2526811
user2526811

Reputation: 1253

Initialize the view controller in iOS sdk

I have a custom view controller named CKCalendarViewControllerInternal.

CKCalendarViewControllerInternal

This class is the subclass of UIViewController.

CkCalendarViewController

I have a custom view controller named CKCalendarViewController. It's a subclass of UINavigationController as follow:

@interface CKCalendarViewController : UINavigationController <CKCalendarViewDelegate, UINavigationControllerDelegate>

This class is initialize with the CKCalendarViewControllerInternal as follow:

- (id)init
{
    CKCalendarViewControllerInternal *calendarViewController = [CKCalendarViewControllerInternal new];

    self = [super initWithRootViewController:calendarViewController];
}

Now, In AppDelegate my first view is as follow:

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];

    UINavigationController *n1=[[UINavigationController alloc]init];
    n1.viewControllers=[[NSArray alloc]initWithObjects:self.viewController, nil];

 self.window.rootViewController=n1;
    [self.window makeKeyAndVisible];
    return YES;
}

CkDemoViewController

This class is the subclass of CkCalendarViewController as follow

@interface CKDemoViewController : CKCalendarViewController

ViewController.m

When i try to push the CKDemoViewController on button clicked.

Error & Question It shows me error like

Exception: Pushing a navigation controller is not supported Exception: [NSException]:Pushing a navigation controller is not supported ex.name:'NSInvalidArgumentException' ex.reason:'Pushing a navigation controller is not supported'

Reason for error This is because the CKCalendarViewController is the subclass of UINavigationController. If i try to open the modal view, it works perfectly.

But How can i initialize the CKCalendarViewController as shown above with the CKCalendarViewControllerInternal class??

Thank you,

Answer will greatly appreciate

Upvotes: 1

Views: 4748

Answers (1)

sergio
sergio

Reputation: 69027

If I understand correctly what you are doing, the simplest "hackish" way to make things work would be making CKCalendarViewController derive from CKCalendarViewControllerInternal. I am suggesting this because I see that you are trying to use your CKCalendarViewController as a normal view controller, so there should be no reason to have it be a navigation controller.

Another possibility would be for you to actually use your CKCalendarViewController as a navigation controller by doing this in your app delegate:

UINavigationController *n1 = [[CKCalendarViewController alloc]init];
n1.viewControllers = [[NSArray alloc]initWithObjects:self.viewController, nil];
self.window.rootViewController = n1;

but this depends on what you are trying to achieve.

More generally, if you are interested in "nesting" controllers within controllers, you should learn about controller containment. In controller containment, what you do to add a controller to another one is basically this:

[vc willMoveToParentViewController:self];
[self addChildViewController:vc];
[self.view addSubview:vc.view];
[vc didMoveToParentViewController:self];

Upvotes: 2

Related Questions