xcode 在不使用 uinavigationcontroller 的情况下向导航栏添加“完成”按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6737721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:31:08  来源:igfitidea点击:

add "done" button to navigation bar without using uinavigationcontroller

iphoneobjective-cxcodeuinavigationcontrolleruinavigationbar

提问by TommyG

I have a view controller that I would like to be able to dismiss with a upper right "done" button on the nav bar. I am not using uinavcontoller, but just added the nav bar like this in my viewDidLoad:

我有一个视图控制器,我希望能够通过导航栏上的右上角“完成”按钮关闭它。我没有使用 uinavcontoller,而是在我的 viewDidLoad 中添加了这样的导航栏:

  bar = [[UINavigationBar alloc] init];

in .h:

在.h:

IBOutlet UINavigationBar *bar;

And of course, connected the nav bar on IB.

当然,还连接了 IB 上的导航栏。

Then I tried to add a nav item also within viewDidLoad but nothing happens:

然后我尝试在 viewDidLoad 中添加一个导航项,但没有任何反应:

rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonSystemItemDone target:nil action:nil];
UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:@"Title"];
item.rightBarButtonItem = rightButton;
item.hidesBackButton = YES;
[bar pushNavigationItem:item animated:NO];

Please note - there are a few questions like that here, but none of them answers this exact question.

请注意 - 这里有一些类似的问题,但没有一个能回答这个确切的问题。

thanks for the help!

谢谢您的帮助!

回答by justin

You won't want to alloc/init the bar in your code if you already have it in IB. That will create a second version of it and messages can get skewed. If you are placing the bar into your view in IB, it would be easiest to place your Done button in there as well. First, you'll want to use UINavigationItem instead of UINavigationBar. Drag a UIBarButtonItem onto the right side of your UINavigationItem, change the text from Item to Done, and select the style. Then you will create an IBAction method to do what you want the button to do (dismiss the view). Then it's just a matter of hooking up the selector of your button (in IB) to the IBAction method and that will do what you want.

如果您已经在 IB 中拥有它,您将不想在代码中分配/初始化该栏。这将创建它的第二个版本,并且消息可能会出现偏差。如果您在 IB 中将栏放置在您的视图中,那么将完成按钮也放置在那里是最容易的。首先,您需要使用 UINavigationItem 而不是 UINavigationBar。将 UIBarButtonItem 拖到 UINavigationItem 的右侧,将文本从 Item 更改为 Done,然后选择样式。然后您将创建一个 IBAction 方法来执行您希望按钮执行的操作(关闭视图)。然后只需将按钮的选择器(在 IB 中)连接到 IBAction 方法,这将执行您想要的操作。

If you want/need to do this programmatically, you will just use the following.

如果您想/需要以编程方式执行此操作,您只需使用以下内容。

rightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissView:)];
navigationItem.rightBarButtonItem = rightButton;
[rightButton release];

You'll need a dismissView:method, too.

你也需要一个dismissView:方法。

- (IBAction)dismissView:(id)sender {
    // do something;
}

EDIT: to do this all programmatically, you'll start by adding a navigation bar to your header file

编辑:要以编程方式完成这一切,您将首先向头文件添加导航栏

UINavigationBar *navBar;

You'll only really need to do this if you plan to allow orientation changes. Since you're not using IB, there's no need to make this an outlet, so we're done in the header.

如果您计划允许方向更改,您只需要这样做。由于您没有使用 IB,因此无需将其设为插座,因此我们已在标题中完成。

In the implementation file, you want to call the following:

在实现文件中,您要调用以下内容:

- (void)viewDidLoad {
    navBar = [[UINavigationBar alloc] init];
    UINavigationItem *navItem = [[[UINavigationItem alloc] initWithTitle:@"some title"] autorelease];
    UIBarButtonItem *done = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissView:)] autorelease];
    navItem.rightBarButtonItem = done;
    navBar.items = [NSArray arrayWithObject:navItem];
    [self.view addSubview:navBar];
}

- (void)viewDidAppear:(BOOL)animated {
    navBar.frame = CGRectMake(0, 0, self.view.frame.size.width, 44);
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    navBar.frame = CGRectMake(0, 0, self.view.frame.size.width, 44);
}

- (void)dealloc {
    [navBar release];
}

This starts by creating the instances and placing them in your view. Once the view appears and the frame size is known to the program, it resizes the navigation bar (which in turn resizes its subviews) to its proper size. Then any time the orientation changes, it resizes accordingly. I've tested this just to make sure all of it is good info, and it works fine for me, so it should be good for you as well. Hopefully it helps you out

首先创建实例并将它们放置在您的视图中。一旦视图出现并且程序知道框架大小,它就会调整导航栏的大小(进而调整其子视图的大小)到合适的大小。然后,只要方向发生变化,它就会相应地调整大小。我已经对此进行了测试,以确保所有信息都是好的信息,并且它对我来说很好用,所以它也应该对你有好处。希望能帮到你