Search code examples
objective-cuiviewuinavigationcontrollerquartz-graphicsquartz-2d

NavigationController with a UIView


I'm training to develop an application without using any graphical interface manually. for this, I am using Quartz 2D framework. In my case I created a custom view (UIView) and added it to my UIWindow in AppDelegate.m file:

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{   
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    ViewCustom *view = [[ViewCustom alloc] initWithFrame:CGRectMake(0, 0, self.window.frame.size.width, self.window.frame.size.height)];

    [self.window addSubview:view];

    [self.window makeKeyAndVisible];
    return YES;
}

ViewCustom.m

- (void)drawRect:(CGRect)rect {
   //Draw code comes here
}

All i'm trying to do is create a navigation controller inside AppDelegate or inside my Custom View (what way is better?), I try to use this code below:

UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:view];

But he say that is incompatible UIView with an UIViewController, how I solve this problem, and add an navigationController in my project?


Solution

  • initWithRootViewController accepts, as the error message reports, an UIViewController. The view you are trying to add is a UIView.

    Try creating instead a UIViewController and set your view as the view of this controller.

    Ex:

    ViewCustom *customView = [[ViewCustom alloc] initWithFrame:CGRectMake(0, 0, self.window.frame.size.width, self.window.frame.size.height)];
    
    UIViewController *myViewController = [[UIViewController alloc] init];
    myViewController.view = customView;
    

    Then:

    UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:myController];