带有UIView的NavigationController

时间:2014-12-24 11:46:18

标签: objective-c uiview uinavigationcontroller quartz-graphics quartz-2d

我在不使用任何图形界面手动开发应用程序的培训。为此,我使用的是Quartz 2D框架。在我的例子中,我创建了一个自定义视图(UIView)并将其添加到AppDelegate.m文件中的UIWindow:

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
}

我想要做的就是在AppDelegate中或在我的自定义视图中创建导航控制器(哪种方式更好?),我尝试使用下面的代码:

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

但他说 UIView与UIViewController不兼容,我如何解决这个问题,并在我的项目中添加一个navigationController?

2 个答案:

答案 0 :(得分:1)

initWithRootViewController的方法UINavigationController需要UIViewController个实例作为参数,因此不要传递视图,而是使用UIViewContrller这样包装:

UIViewController *controller = [[UIViewController alloc] init];
//The view of this controller will be instance of your CustomView.
controller.view = view; 

//Add UIViewController to navigation controller as root.
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:controller];

//now add navigation as root.
[self.window addRootViewController:navigation];

我想知道您如何不将UINavigationController视为原生GUI组件并在您的应用中使用它,如您自己所说:

  

我在不使用任何图形的情况下开发应用程序   手动界面。

答案 1 :(得分:1)

initWithRootViewController在错误消息报告时接受UIViewController。您要添加的视图是UIView

尝试创建UIViewController并将view设置为此控制器的view

例如:

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;

然后:

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