xcode 在 UINavigationController 中推送另一个视图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6749271/
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:32  来源:igfitidea点击:

Push another View in UINavigationController?

iphonexcodeuinavigationcontrolleruitabbarcontroller

提问by ftwhere

How to push a view using a button in a navigation controller? I tried to follow this example: http://www.cimgf.com/2009/06/25/uitabbarcontroller-with-uinavigationcontroller-using-interface-builder/

如何使用导航控制器中的按钮推送视图?我试着按照这个例子:http: //www.cimgf.com/2009/06/25/uitabbarcontroller-with-uinavigationcontroller-using-interface-builder/

This is exactly what I need, I have an UITabBarControllerwith 4 tabs and one of them is an UINavigationController. I want to push that view containing the navigation controller from the first view- which is a simple UIView. It doesn't work, I tried also with a programmatically created button and nothing happens. Any pointers?

这正是我所需要的,我有一个UITabBarController带有 4 个标签的标签,其中一个是UINavigationController. 我想从第一个视图推送包含导航控制器的视图 - 这是一个简单的UIView. 它不起作用,我也尝试使用以编程方式创建的按钮,但没有任何反应。任何指针?

Here's the code I have

这是我的代码

@interface HomeViewController : UIViewController {

}

-(IBAction)pushViewController:(id)sender;

@end

---

#import "HomeViewController.h"
#import "NewViewController.h"

@implementation HomeViewController

-(IBAction)pushViewController:(id)sender {
        NewViewController *controller = [[NewViewController alloc] initWithNibName:@"NewViewController" bundle:nil];
    [[self navigationController] pushViewController:controller animated:YES];
    [controller release], controller = nil;
}

Here is also the screenshot with the connections http://imageshack.us/photo/my-images/847/screenshot20110719at620.png/

这也是带有连接的截图 http://imageshack.us/photo/my-images/847/screenshot20110719at620.png/

I've tried what you guys suggested and still nothing, the buttons( whether they are created in Interface Builderor programmatically ) act like they're not linked to any method.

我已经尝试了你们的建议,但仍然没有,按钮(无论它们是在中创建Interface Builder还是以编程方式创建)表现得好像它们没有链接到任何方法。

回答by Christian Schnorr

Put this where you want to create the button: (e.g. the Root VC's -viewDidLoad)

把它放在你想创建按钮的地方:(例如 Root VC's -viewDidLoad

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(100.0, 100.0, 100.0, 40.0);
[button setTitle:@"Press" forState:UIControlStateNormal];
[viewController.view addSubview:button];

[button addTarget:self action:@selector(didPressButton:) forControlEvents:UIControlEventTouchUpInside];

And implement this method:

并实现这个方法:

- (void)didPressButton:(UIButton *)sender
{
    UIViewController *viewController = [[[UIViewController alloc] init] autorelease];
    [self.navigationController pushViewController:viewController animated:YES];
}

回答by Adriano Spadoni

If you use storyboard, create a segue manually

如果您使用故事板,请手动创建一个 segue

On storyboard:

在故事板上:

  • click on the view
  • then on the right sidebar top menu select the "Show the connections inspector"
  • in the "Triggered Segues", click and drag the "manual" trigger to the view you want to push
  • chose "push" in the pop menu that will appear
  • 点击查看
  • 然后在右侧边栏顶部菜单上选择“显示连接检查器”
  • 在“Triggered Segues”中,单击“手动”触发器并将其拖动到要推送的视图
  • 在出现的弹出菜单中选择“push”

it will create a segue, now select the segue and give it a identifier

它将创建一个segue,现在选择segue并给它一个标识符

Now you can use the code:

现在您可以使用以下代码:

-(IBAction)doneAction:(id)sender
{
    [self performSegueWithIdentifier:@"identifier" sender:self ];
}

now you can wire the action to your button

现在您可以将操作连接到您的按钮

回答by Man of One Way

To push a view to the navigation controller just do:

要将视图推送到导航控制器,只需执行以下操作:

YourController *childController = [[YourController alloc] initWithNibName:@"YourControllerView" bundle:NSBundle.mainBundle];
[self.navigationController pushViewController:childController animated:YES];
[childController release];

Now if you want it to happen when pressing a button, add a button to your nib file and connect it to a IBActionmethod on Touch Up Inside event.

现在,如果您希望在按下按钮时发生这种情况,请在您的 nib 文件中添加一个按钮并将其连接到IBActionTouch Up Inside 事件的方法。