在 UINavigationController 中推送另一个视图?

发布于 2024-11-24 21:51:53 字数 1283 浏览 1 评论 0原文

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

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

这是我的代码

@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;
}

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

我已经尝试了你们的建议,但仍然没有任何结果,按钮(无论它们是在 Interface 中创建的Builder 或以编程方式)的行为就像它们没有链接到任何方法一样。

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/

This is exactly what I need, I have an UITabBarController with 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?

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/

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜心小果奶 2024-12-01 21:51:53

将其放在您要创建按钮的位置:(例如 Root VC 的 -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];

并实现此方法:

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

Put this where you want to create the button: (e.g. the 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];
}
本宫微胖 2024-12-01 21:51:53

如果您使用 Storyboard,请手动创建 Segue

在 Storyboard 上:

  • 单击视图,
  • 然后在右侧边栏顶部菜单中选择“触发的 Segues”中的“显示连接检查器”
  • ,单击并将“手动”触发器拖动到您要查看的视图。想要推送
  • 在出现的弹出菜单中选择“推送”

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

现在您可以使用代码:

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

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

If you use storyboard, create a segue manually

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

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

Now you can use the code:

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

now you can wire the action to your button

淡写薰衣草的香 2024-12-01 21:51:53

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

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

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

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 IBAction method on Touch Up Inside event.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文