如何使用代码推送到新的视图控制器?

时间:2013-02-10 01:06:46

标签: iphone ios uiviewcontroller xcode4.5

应用应根据数据库中的某些数据转发到另一个视图控制器。数据库工作正常,但我无法让应用程序转发到第二个视图控制器。 这是我现有的代码。

ssSecondViewController *tempView =[[ssSecondViewController alloc] init];
[self.navigationController pushViewController:tempView animated:YES];

我没有收到任何错误,但是当我运行代码时,屏幕会转发到空白的黑色视图?

3 个答案:

答案 0 :(得分:3)

如果你已经在storyboard上有你的viewController,那么你可以通过下面的代码&你必须在storyboard上为viewControler设置标识符

ssSecondViewController *tempView =[self.storyboard instantiateViewControllerWithIdentifier:@"yourIdentifier"];

在这里设置你的viewController id,在下面的图像标识符是“MenuViewController”

enter image description here

如果是.Xib文件,那么

ssSecondViewController *tempView =[[ssSecondViewController alloc] initWithNibName:@"yourIdentifier" bundle:nil];

如果你只是想在没有使用已经可用的viewController的情况下以程序方式添加新的viewController,那么你必须用initWithFrame初始化它然后它显示viewController

ssSecondViewController *tempView =[[ssSecondViewController alloc] initWithFrame:CGRectMake(0,0,320,480)];

ssSecondViewController *tempView =[[ssSecondViewController alloc] init];这一行只是分配了viewController,它没有任何绘制它的位置。

答案 1 :(得分:0)

您需要使用

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

因此,对于您的示例,它将是:

ssSecondViewController *tempView =[[ssSecondViewController alloc] initWithNibName:@"ssSecondViewController" bundle:nil];

答案 2 :(得分:0)

通过故事板,您通常不会进行推送,您可以在故事板中使用segueIdentifiers设置进​​行调整:

[self performSegueWithIdentifier:@"ssSecondSegue" sender:self];

然后处理prepareForSegue

中的数据传递和其他设置者