2011-07-19 90 views
4

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

这正是我需要的,我有一个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或以编程方式)表现得像他们没有链接到任何方法。

+0

尝试张贴在这里你的代码,所以我们可以看到什么可能会错误。 –

+1

你是否遵循了教程的每一步? “它不起作用”太抽象了,无法理解你的问题。请张贴一些代码。 – Mat

回答

7

要在其中创建按钮将这个(如根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]; 
} 
1

要推一个视图导航控制器只是做:

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

现在,如果你希望它发生时,按下一个按钮,将按钮添加到您的笔尖文件,并将其连接到上触碰的IBAction方法内部事件。

3

如果你用故事板,手动创建SEGUE

在故事板:

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

它会创建一个SEGUE,现在选择SEGUE并给它一个标识符

现在你可以使用代码:

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

现在你就可以将作用到你的按钮

相关问题