当单元格是使用 XCode 4 的 UISCrollView 时,将单元格推送到 DetailView 时崩溃 [英] Crash when pushing cell to DetailView when it's a UISCrollView using XCode 4

查看:20
本文介绍了当单元格是使用 XCode 4 的 UISCrollView 时,将单元格推送到 DetailView 时崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此代码从 TableView 移动到 DetailView:

I use this code to move to DetailView from TableView:

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:TRUE];
  if(indexPath.row==0) {

      //Initialize the detail view controller and display it.
      Detail1ViewController *firstDetailController = [[Detail1ViewController alloc] initWithNibName:@"Detail1ViewController" bundle:nil];
      [self.navigationController pushViewController:firstDetailController animated:YES];
      [firstDetailController release];
}
else if(indexPath.row==1) {

    //2nd view
}
else if(indexPath.row==2) {

    //3rd view
}
}

Storyboard 中的 Detail1ViewController 只是一个 UIScrollView,后面没有 UIView.

The Detail1ViewController in the Storyboard is only a UIScrollView with no UIView behind it.

这是 Detail1ViewController.h:

This is the Detail1ViewController.h:

#import <UIKit/UIKit.h>

@interface Detail1ViewController : UIViewController {
 IBOutlet UIScrollView *detail1Scroller;
}

@end

并加载 .m:

- (void)viewDidLoad
{
[super viewDidLoad];
[detail1Scroller setScrollEnabled:YES];
[detail1Scroller setContentSize:CGSizeMake(320,1100)];
// Do any additional setup after loading the view.
}

我已经在 Connections Inspector 中建立了从detail1Scroller"到 UISCrollView 的连接,并在 Identity Inspector 中编写了Detail1ViewController"作为 Detail1ViewController 的类.但是当我转到详细信息视图时应用程序崩溃了!

I've made the connection from "detail1Scroller" to the UISCrollView in Connections Inspector, and wrote "Detail1ViewController" as class for the Detail1ViewController in Identity Inspector. But the app crashes when I'm going to the detail view!

我注意到在我正在关注的教程中,UIScrollView 被放置在一个 UIView 上,我在我的项目中没有这样做.所以我尝试在 UIView 中创建一个带有 UIScrollView 的 TestViewController,但是detail1Scroller"Outlet 没有像教程中那样显示在 Connections Inspector 中!有人可以帮我解决这个问题,并明确我是否应该使用带有 UIView 的 UIScrollView.

I've noticed that in the tutorial I'm following, the UIScrollView is placed upon a UIView, which I haven't done in my project. So I tried to create a TestViewController with the UIScrollView inside a UIView, but then the "detail1Scroller" Outlet isn't showing in the Connections Inspector as it does in the tutorial! Can someone please help me solve this, and also make it clear if I should use the UIScrollView with a UIView behind or not.

推荐答案

故事板是一种配置视图控制器实例的方法,并通过 segue 自动或手动使用代码反复实例化它们.当您在故事板中设计视图控制器时,您不是在设计类,而是在设计类的特定实例.在 Storyboard 中拥有相同视图控制器类的两个实例,具有不同的视图布局是合法的.

A storyboard is a way to configure instances of view controllers, and instantiate them over and over again either automatically through segues or manually with code. When you're designing a view controller in the storyboard, you're not designing the class, you're designing a specific instance of the class. It's legal to have two instances of the same view controller class in the storyboard, with different view layouts.

这意味着如果您直接分配视图控制器类的实例,它不会附带您在故事板中指定的所有 UI.这实际上是这段代码发生的事情:

That means that if you directly allocate an instance of your view controller class, it won't come with all of the UI that you specified in the storyboard. That's effectively whats happening with this code:

Detail1ViewController *firstDetailController = [[Detail1ViewController alloc] initWithNibName:@"Detail1ViewController" bundle:nil];
[self.navigationController pushViewController:firstDetailController animated:YES];
[firstDetailController release];

即使您没有在 Xcode 中设计 XIB/NIB,也有一点令人困惑,因为您指定了一个 NIB 名称.

There's also a bit of confusion there in that you're specifying a NIB name even though you didn't design a XIB/NIB in Xcode.

要分配您在故事板中设计的视图控制器的新副本,您应该使用 -[UIStoryboard instantiateViewControllerWithIdentifier:] 方法.标识符值应与故事板身份检查器中的值相匹配.

To allocate a new copy of the view controller that you designed in your storyboard, you should use the -[UIStoryboard instantiateViewControllerWithIdentifier:] method. The identifier value should match the value in the storyboard identity inspector.

您的代码应该如下所示:

Your code should then look like this:

Detail1ViewController *firstDetailController = [[self storyboard] instantiateViewControllerWithIdentifier:/* my identifier */];
[self.navigationController pushViewController:firstDetailController animated:YES];

如果您使用带有此代码的视图控制器的故事板,您还应该考虑使用 segue 来启动此转换.

If you're using a storyboard for the view controller with this code, you should also consider using a segue to initiate this transition.

这篇关于当单元格是使用 XCode 4 的 UISCrollView 时,将单元格推送到 DetailView 时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆