Ios 如何显示视图?

Ios 如何显示视图?,ios,objective-c,cocoa-touch,Ios,Objective C,Cocoa Touch,我是iOS开发新手 我有一个带按钮的ViewControllerViewController。当用户按下该按钮时,我想将视图切换到RegisterViewController ViewController.m包含以下代码: #import "ViewController.h" #import "RegisterViewController.h" @interface ViewController () @end @implementation ViewController @synthe

我是iOS开发新手

我有一个带按钮的ViewController
ViewController
。当用户按下该按钮时,我想将视图切换到
RegisterViewController

ViewController.m
包含以下代码:

#import "ViewController.h"
#import "RegisterViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize registerViewButton;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)registerViewButtonClick:(id)sender {
    NSLog(@"registerViewButtonClick called");

    RegisterViewController* controller = [[RegisterViewController alloc] init];
    [self.navigationController pushViewController:controller animated:YES];    
}

@end
根据调试输出,当我按下按钮时,实际上调用了方法registerViewButtonClick

但是RegisterViewController表示的视图不显示

应用程序的代码可用


按下按钮时,我需要更改什么才能使RegisterViewController的视图可见?

您可能需要使用nib名称初始化UIViewController

像这样


RegisterViewController*控制器=[[RegisterViewController alloc]initWithNibName:@“RegisterViewController”捆绑包:nil]

我运行了您的代码,发现代码中没有实现导航控制器,但您正在尝试推送registerViewController。尝试如下所示演示viewcontroller:

[self presentViewController:controller animated:YES completion:nil];
而不是

[self.navigationController pushViewController:controller animated:YES]; 
pushViewController
仅在有UINavigationController时工作。根据文档,UINavigationController类实现了一个专门的视图控制器,用于管理分层内容的导航。由于您的代码中没有UINavigationController(
self.navigationController
在本例中为
nil
),因此在尝试按下viewController时不会发生任何事情


当您想要维护一组ViewController时,UINavigationController也很方便,您可以根据需要在其中推送或弹出。这也会自动提供“后退”按钮。如果您只需要显示一个viewcontroller,那么
presentViewController:
可能是正确的选择。

如果您使用的是情节提要,则需要从情节提要中获取视图控制器,如

RegisterViewController *news=[[RegisterViewController alloc] initWithNibName:@"RegisterViewController" bundle:nil];
[self.navigationController pushViewController:news animated:YES];
[news release];
RegisterViewController *viewController = (RegisterViewController*)[[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"vc-identifier"];
vc标识符
替换为故事板中给定的标识符,并将
MainStoryboard
替换为故事板的名称

然后像您已经做的那样将其传递到导航控制器中,只要导航控制器不是
nil

[self.navigationController pushViewController:controller animated:YES];   
刚听说你不使用故事板,因为我的位置,我无法下载你的链接,所以我给出了一个关于故事板的解决方案。我的建议是使用故事板

将此内容留在此处,因为它可能会帮助其他有相同问题但正在使用故事板的人

  • 如果您想演示,则不需要UINavigationController
  • 但要推动UINAVIGATIONAL,控制器是必须的
  • 在情节提要中,必须添加一个UINavigationController

  • 在按钮操作中,使用nib名称正确初始化VC

你有导航控制器吗?换句话说:Is
self.navigationController
不是
nil
?如果您觉得慷慨,您可以编辑您的答案,并告诉OP为什么发送到
nil
的消息没有任何作用。@Geniewated非常感谢您的回答。请原谅我一个愚蠢的问题,但是为什么对
nil
对象的调用不会导致
NullReferenceExeption
或类似的东西(就像在Java和C中那样)?这实际上是一个有趣的问题。。这在这里有很好的解释。。。这都是关于C如何看待它的。
 RegisterViewController* controller = [[RegisterViewController alloc] initWithNibName:@"RegisterViewController" bundle:nil];
    [self.navigationController pushViewController:controller animated:YES];