如何构建 UINavigation 控制器和关联的视图控制器以进行事件处理? [英] How do I structure the UINavigation controller and associated view controllers for event handling?

查看:23
本文介绍了如何构建 UINavigation 控制器和关联的视图控制器以进行事件处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UINavigationController 需要管理多个视图控制器.导航控制器在下面的 Take.m 中,每个视图控制器在它自己的文件中(参见下面的 CamverViewController.m).作为一名新的 XCode 开发人员,我试图弄清楚如何构建应用程序,以便诸如点击导航栏中按钮之类的事件可以访问导航控制器的实例以更改活动视图控制器.我了解如何操作视图控制器,但需要一些有关设置事件处理的建议.

I have a UINavigationController that needs to manage several view controllers. The navigation controller is in Take.m below, and each view controller in it's own file (see CamverViewController.m below). As a new XCode developer, I'm trying to figure out how to structure the application so events such as the tap of the button in the navigation bar can get access to the instance of the navigation controller to change the active view controller. I understand how to manipulate the view controllers, but need some advice on setting up the event handling.

注意:我已经从示例源中删除了不相关的方法.

Note: I have remove irrelevant methods from the sample source.

任何参考资料、示例、建议,不胜感激.

Any references, samples, advice, appreciated.

这个类是 UINavigationController 和顶层视图控制器的实例化.

This class is there the UINavigationController and top level view controller are instantiated.

Take.m 来源

@implementation Take

+ (UINavigationController*) createController {

//Controllers for navigation interface
CameraViewController *cameraViewController=[[CameraViewController alloc]init];

UINavigationController *navigationController=[[UINavigationController alloc]initWithRootViewController:cameraViewController];
navigationController.toolbarHidden=NO;

//Create tab bar item
navigationController.title=@"Take";
UITabBarItem *tabBarItem=[[UITabBarItem alloc]initWithTitle:@"Take" image:NULL tag:0];
navigationController.tabBarItem=tabBarItem;

[navigationController.view.window addSubview:navigationController.view];

return navigationController;
}

@end

这是包含导航栏和右侧按钮的顶级视图控制器,当单击/点击时需要更改导航控制器中的视图.当这个事件被困时,主要的问题是如何访问 Take.m 中的导航控制器来操纵活动视图控制器?CameraViewController.m

This is the top level view controller which contains the navigation bar and right side button which, when clicked/tapped needs to change the view in the navigation controller. When this event is trapped the primary question is how to get access to the navigation controller in Take.m to manipulate the active view controller? CameraViewController.m

@implementation CameraViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization

    //Navigation bar items
    [self configureNavigationBarItems];

    //Toolbar for navigation interface
    [self configureToolbarItems];

}
return self;
}

-(void) configureNavigationBarItems{

//Add navigation bar items
//    [navigationController setNavigationBarHidden:YES];
UIBarButtonItem *categorizeButton=[[UIBarButtonItem alloc]initWithTitle:@"Categorize" style:UIBarButtonItemStylePlain target:self action:@selector(categorizeButtonHandler:)];
self.navigationItem.rightBarButtonItem=categorizeButton;

}

-(void) configureToolbarItems{

UISegmentedControl *options=[[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:@"Photo",@"Video", nil]];
options.segmentedControlStyle=UISegmentedControlStyleBar;
options.selectedSegmentIndex=0;
[options addTarget:self action:@selector(toggleSorting:) forControlEvents:UIControlEventValueChanged];

UIBarButtonItem *optionButton=[[UIBarButtonItem alloc]initWithCustomView:options];
UIBarButtonItem *spacer=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

self.toolbarItems=[NSArray arrayWithObjects:spacer,optionButton,spacer, nil];

}


#pragma mark - Event Handlers

- (void)categorizeButtonHandler:(id)sender
{
UIBarButtonItem *barButton=(UIBarButtonItem*)sender;

//How to access the navigation controller here???

}

@end

推荐答案

这可能看起来有点颠倒,但 UINavigationController 被设计为位于视图控制器层次结构的顶部.所以你不应该在 UIViewController 中实例化一个导航控制器(我假设 Take.m 就是这样)

It may seem a bit upside down, but UINavigationController is designed to be at the top of the view controller hierarchy. So you shouldn't be instantiating a navigation controller inside a UIViewController (which is what I'm assuming Take.m is)

您在视图控制器中构建的导航栏出现在屏幕顶部的事实可能会使您的视图控制器看起来包含导航控制器 - 它没有.UINavigationController 将它的导航栏插入到您的视图控制器上方(或者您可以将其关闭,然后 UINavigationController 只是通过调用 push 和 pop 提供了一种处理屏幕切换的好方法).

The fact that you have a navbar appear at the top of your screen that you construct in your view controller may make it seem that your view controller contains a navigation controller - it doesn't. UINavigationController inserts it's navbar above your view controller for you (or you can turn it off and then UINavigationController just provides a great way to handle screen switching by calling push and pop).

尝试将 UINavigationController 作为 rootViewController 直接添加到窗口中,然后将您的 UIViewController 推入堆栈以显示您的第一个屏幕.

Try adding a UINavigationController directly to window as rootViewController, then push your UIViewController onto the stack to display your first screen.

然后您使用以下命令访问 UINavigationController:

Then you access the UINavigationController using:

self.navigationController

从您的视图控制器(将为您设置 navigationController 属性),您可以使用 setViewControllers 推送新的 VC、弹出 self 或设置新的 VC 配置.

from your view controller (the navigationController property will be set for you) and you can push a new VC, pop self, or set a new VC configuration using setViewControllers.

这篇关于如何构建 UINavigation 控制器和关联的视图控制器以进行事件处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆