两个 UIViewController 的故事

发布于 2024-12-11 04:59:18 字数 13445 浏览 0 评论 0原文

所以我想我快疯了。

我已经更新到 XCode 4.2,所以我有 IOS 5 SDK。

我有一个应用程序在升级之前一直运行良好。现在,我有一个奇怪的问题。它无法在我的 IOS 5 iPad 2 上运行,也无法在 IOS 5 模拟器中运行。在 4.3 模拟器中运行良好。

出于这个问题的目的,我有两个基于 UIViewController 的类。他们不使用 NIB 文件。其中一个名为 HistoryBrowser,效果很好。另一个,NoteBrowseViewController,按照同样的思路构建,却没有。

来自 NoteBrowseView.Controller.h:

@interface NoteBrowseViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, NSFetchedResultsControllerDelegate, UIActionSheetDelegate,        UISearchDisplayDelegate, UITabBarDelegate, MKMapViewDelegate> {
  UITableView* tableView;
  ... buncha other vars ...
}
@property (retain, nonatomic) UITableView* tableView;
... buncha other properties ...

来自 NoteBrowseViewController.m:

@synthesize tableView

- (id)initWithEditing:(BOOL)inEditingMode inserting:(BOOL)inserting {
self=[super init];
if (self) {
    self.isInserting=inserting;
    self.isEditing=inEditingMode;
    self.viewIsMap=NO;
    self.insertType=defInsertTypeLink;

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSDictionary *appDefaults = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:0],@"viewSortOrder",
                                 [NSNumber numberWithInt:1],@"priorityView",
                                 [NSNumber numberWithBool:NO],@"simpleView",
                                 nil];

    [defaults registerDefaults:appDefaults];

    self.viewIsSimple=[[NSUserDefaults standardUserDefaults]boolForKey:@"simpleView"];
}
return self;
}

-(void)loadView {
self.view=[[UIView alloc]initWithFrame:[[UIScreen mainScreen] applicationFrame]];

UIButton* aButton;
UIImage* buttonImage;
UIBarButtonItem* spacer=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

UISegmentedControl* sc=[[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:@"Alpha",@"Edit", @"View", nil]];
[sc addTarget:self action:@selector(segmentedControlValueChanged:) forControlEvents:UIControlEventValueChanged];
[sc setWidth:55.0 forSegmentAtIndex:0];
[sc setWidth:55.0 forSegmentAtIndex:1];
[sc setWidth:55.0 forSegmentAtIndex:2];
sc.selectedSegmentIndex=[[NSUserDefaults standardUserDefaults] integerForKey:@"viewSortOrder"];
sc.segmentedControlStyle=UISegmentedControlStyleBar;

UISegmentedControl* prisc=[[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:@"Open",@"All", nil]];
[prisc addTarget:self action:@selector(prioritySegmentedControlValueChanged:) forControlEvents:UIControlEventValueChanged];
[prisc setWidth:55.0 forSegmentAtIndex:0];
[prisc setWidth:55.0 forSegmentAtIndex:1];
prisc.selectedSegmentIndex=[[NSUserDefaults standardUserDefaults] integerForKey:@"priorityView"];
prisc.segmentedControlStyle=UISegmentedControlStyleBar;

UIBarButtonItem* segmentedButton=[[UIBarButtonItem alloc]initWithCustomView:sc];
UIBarButtonItem* prioritySegmentedButton=[[UIBarButtonItem alloc]initWithCustomView:prisc];

buttonImage=[UIImage imageNamed:@"13-plus.png"];
aButton=[UIButton buttonWithType:UIButtonTypeCustom];
[aButton setImage:buttonImage forState:UIControlStateNormal];
aButton.frame=CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
UIBarButtonItem* addButton=[[UIBarButtonItem alloc]initWithCustomView:aButton];
[aButton addTarget:self action:@selector(addButton:) forControlEvents:UIControlEventTouchUpInside];

buttonImage=[UIImage imageNamed:@"187-pencil.png"];
aButton=[UIButton buttonWithType:UIButtonTypeCustom];
[aButton setImage:buttonImage forState:UIControlStateNormal];
aButton.frame=CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
UIBarButtonItem* editButton=[[UIBarButtonItem alloc]initWithCustomView:aButton];
[aButton addTarget:self action:@selector(editButton:) forControlEvents:UIControlEventTouchUpInside];

buttonImage=[UIImage imageNamed:@"21-circle-east.png"];
aButton=[UIButton buttonWithType:UIButtonTypeCustom];
[aButton setImage:buttonImage forState:UIControlStateNormal];
aButton.frame=CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
UIBarButtonItem* simplify=[[UIBarButtonItem alloc]initWithCustomView:aButton];
[aButton addTarget:self action:@selector(simplify:) forControlEvents:UIControlEventTouchUpInside];

buttonImage=[UIImage imageNamed:@"25-circle-west.png"];
aButton=[UIButton buttonWithType:UIButtonTypeCustom];
[aButton setImage:buttonImage forState:UIControlStateNormal];
aButton.frame=CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
UIBarButtonItem* complexify=[[UIBarButtonItem alloc]initWithCustomView:aButton];
[aButton addTarget:self action:@selector(complexify:) forControlEvents:UIControlEventTouchUpInside];

buttonImage=[UIImage imageNamed:@"243-globe.png"];
aButton=[UIButton buttonWithType:UIButtonTypeCustom];
[aButton setImage:buttonImage forState:UIControlStateNormal];
aButton.frame=CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
UIBarButtonItem* map=[[UIBarButtonItem alloc]initWithCustomView:aButton];
[aButton addTarget:self action:@selector(mapify:) forControlEvents:UIControlEventTouchUpInside];

buttonImage=[UIImage imageNamed:@"162-receipt.png"];
aButton=[UIButton buttonWithType:UIButtonTypeCustom];
[aButton setImage:buttonImage forState:UIControlStateNormal];
aButton.frame=CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
UIBarButtonItem* notes=[[UIBarButtonItem alloc]initWithCustomView:aButton];
[aButton addTarget:self action:@selector(showNotes:) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem* cancelButton=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(cancelButtonPressed:)];
self.doneToolbar=[NSArray arrayWithObjects:spacer, cancelButton, nil];

self.toolbar=[[[UIToolbar alloc]init]autorelease];

if(self.isEditing) {
    self.complexToolbar=[NSArray arrayWithObjects:simplify, spacer, segmentedButton, prioritySegmentedButton, spacer, cancelButton, nil];
    self.simpleToolbar=[NSArray arrayWithObjects:complexify, spacer, cancelButton, nil];
}
else {
    self.complexToolbar=[NSArray arrayWithObjects:simplify, map, spacer, segmentedButton, prioritySegmentedButton, spacer, addButton, editButton, cancelButton, nil];
    self.simpleToolbar=[NSArray arrayWithObjects:complexify, map, spacer, addButton, editButton, cancelButton, nil];
}
self.mapToolbar=[NSArray arrayWithObjects:notes, spacer, prioritySegmentedButton, spacer, cancelButton, nil];

if (self.viewIsSimple) {
    [self.toolbar setItems:self.simpleToolbar animated:YES];
}
else {
    [self.toolbar setItems:self.complexToolbar animated:YES];
}

self.toolbar.autoresizingMask=UIViewAutoresizingFlexibleWidth;        
[self.toolbar sizeToFit];

CGFloat toolbarHeight = [self.toolbar frame].size.height;
CGRect rootViewBounds=self.view.bounds;
CGFloat rootViewHeight = CGRectGetHeight(rootViewBounds);
CGFloat rootViewWidth = CGRectGetWidth(rootViewBounds);
[self.toolbar setFrame:CGRectMake(0,0, rootViewWidth, toolbarHeight)];

self.mapView=[[[MKMapView alloc]initWithFrame:CGRectMake(0, toolbarHeight, rootViewWidth, rootViewHeight-(self.isInserting?49:0)-toolbarHeight)]autorelease];
self.mapView.delegate=self;
self.mapView.zoomEnabled=YES;
self.mapView.scrollEnabled=YES;
self.mapView.autoresizingMask=UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;

self.tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, toolbarHeight, rootViewWidth, rootViewHeight-(self.isInserting?49:0)-toolbarHeight) 
                                           style:UITableViewStylePlain];
self.tableView.delegate=self;
self.tableView.dataSource=self;
self.tableView.autoresizingMask=UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;

self.view.autoresizesSubviews = YES; 
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

UISearchBar* searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 44, rootViewWidth, 44.0)];
searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
self.tableView.tableHeaderView = searchBar;

self.searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
self.searchDisplayController.delegate = self;
self.searchDisplayController.searchResultsDataSource = self;
self.searchDisplayController.searchResultsDelegate = self;

[self.view addSubview:self.tableView];
[self.view addSubview:self.toolbar];

if (self.isInserting) {
    UITabBarItem* item1=[[[UITabBarItem alloc]initWithTitle:@"Link" image:nil tag:defInsertTypeLink]autorelease];
    UITabBarItem* item2=[[[UITabBarItem alloc]initWithTitle:@"Contents Later" image:nil tag:defInsertTypeContentsLater]autorelease];
    UITabBarItem* item3=[[[UITabBarItem alloc]initWithTitle:@"Contents Now" image:nil tag:defInsertTypeContentsNow]autorelease];
    UITabBar* tabbar=[[UITabBar alloc]initWithFrame:CGRectMake(0, rootViewHeight-49, rootViewWidth, 49)];
    tabbar.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin;
    [tabbar setDelegate:self];
    [tabbar setItems:[NSArray arrayWithObjects:item1, item2, item3, nil] animated:YES];
    [tabbar setSelectedItem:item1];
    [self.view addSubview:tabbar];
}

if(self.viewIsSimple) {
    self.contentSizeForViewInPopover = CGSizeMake(200.0, 625.0);         
}
else {
    self.contentSizeForViewInPopover = CGSizeMake(450.0, 625.0); 
}
self.view.autoresizesSubviews = YES; 
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.note=nil;

[sc release];
[prisc release];
[addButton release];
[prioritySegmentedButton release];
[cancelButton release];
[segmentedButton release];
[spacer release];
[editButton release];
[map release];
[simplify release];
[complexify release];
}

最后,NoteBrowseViewController 是从另一个视图控制器实例化的:

self.noteBrowseViewController=[[NoteBrowseViewController alloc]initWithEditing:NO inserting:NO];
self.noteBrowseViewController.delegate=self;
self.popoverController = [[UIPopoverController alloc]initWithContentViewController:self.noteBrowseViewController];
self.popoverController.delegate = self;
[self.popoverController presentPopoverFromRect:((UIButton*)sender).frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

所以这就是发生的情况。如果我在调试中运行它,并按照执行,一旦执行到此行:

CGRect rootViewBounds=self.view.bounds;

程序崩溃并出现以下错误:

2011-10-20 11:42:02.703 ActionNote3[12332:15803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]:  object cannot be nil'
*** First throw call stack:
(0x1eb0052 0x2a60d0a 0x1e9d36e 0x1e9e220 0x1968cb6 0x5153b 0x50e11 0x50879 0x52e4a 0xdcc64e 0x4bcb3 0x72d1 0x1eb1ec9 0xd095c2 0xd0955a 0xdaeb76 0xdaf03f 0xdae2fe 0xd2ea30 0xd2ec56 0xd15384 0xd08aa9 0x20c7fa9 0x1e841c5 0x1de9022 0x1de790a 0x1de6db4 0x1de6ccb 0x20c6879 0x20c693e 0xd06a9b 0x2dbd 0x2d35)
terminate called throwing an exception

通过在调试期间观察事物,我知道 self.view 已正确设置,并且 loadView 是在应该被调用的时候被调用。导致失败的并不是这一行——而是任何引用 self.view 的东西!什么???

真正让我发疯的是,我有另一个子类视图控制器 HistoryBrowser,它以完全相同的方式加载......并且工作正常。

因此,除了对我无法弄清楚这一点感到不安之外,我还想了解:

  1. XCode 4.2(从 4.1 开始)中发生了什么变化会导致这种情况,或者是 这是 IOS 5 的问题吗?
  2. 这个错误是什么意思,我该怎么办 关于它?

编辑:

因此,根据下面 Abberant 的回答的建议,我:

  1. 将 initwithnib 更改为 init
  2. 删除了 loadView 方法
  3. 添加了完整的错误消息
  4. 在 init 方法中添加了更多相关代码

现在,引用 self.视图工作正常。但事情变得更奇怪了。

执行使其通过了对 self.view 的第一次引用,没有错误。然而,它现在停在:

self.tableView.tableHeaderView = searchBar;

init 方法中。

收到的错误消息与我之前收到的错误消息出奇地相似:

2011-10-20 12:34:04.818 ActionNote3[13487:15803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
*** First throw call stack:
(0x1eaf052 0x2a5fd0a 0x1e9c36e 0x1e9d220 0x1967cb6 0x50b4b 0x50421 0x4fe89 0x4d757 0x6641 0x1eb0ec9 0xd085c2 0xd0855a 0xdadb76 0xdae03f 0xdad2fe 0xd2da30 0xd2dc56 0xd14384 0xd07aa9 0x20c6fa9 0x1e831c5 0x1de8022 0x1de690a 0x1de5db4 0x1de5ccb 0x20c5879 0x20c593e 0xd05a9b 0x212d 0x20a5)
terminate called throwing an exception

因此,如果我注释掉该行,执行将继续进行,直到到达这组行,当它尝试执行 PerformFetch 时会失败。

    NSError *error = nil;
    if (![[self currentFetchedResultsController] performFetch:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

调试 [self currentFetchedResultsController] 的代码表明获取的结果控制器已正确设置且没有问题。

这里收到的错误与上面相同。第一个抛出调用堆栈中的数字发生变化,但除此之外错误是相同的。

所以我觉得我这里有一个更大的问题。我只是不知道如何追踪它。正如我在某处所说的那样,我有另一个视图,以相同的方式构建,负载良好。

编辑:

因此,NJones 在下面的回答中建议遵循 Apple 的指南,在 loadView 和 viewDidLoad 中放置适当的代码。所以我做到了。我将所有视图构造从 init 移出并移入 loadView,并在 viewDidLoad 中启动 fetchedResultsController。

该应用程序仍然在同一位置崩溃:

self.tableView.tableHeaderView = searchBar

出现与上述相同的错误。

谢谢!

So I think I'm going crazy.

I've updated to XCode 4.2 so I'd have the IOS 5 SDK.

I have an app that's been working great until the upgrade. Now, I have a strange problem. It does not work on my IOS 5 iPad 2, nor will it work in the IOS 5 simulator. Works fine in the 4.3 simulator.

For the purposes of this question, I have two classes based on UIViewController. They do not use NIB files. One, called HistoryBrowser, works great. The other, NoteBrowseViewController, constructed along the same lines, does not.

From NoteBrowseView.Controller.h:

@interface NoteBrowseViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, NSFetchedResultsControllerDelegate, UIActionSheetDelegate,        UISearchDisplayDelegate, UITabBarDelegate, MKMapViewDelegate> {
  UITableView* tableView;
  ... buncha other vars ...
}
@property (retain, nonatomic) UITableView* tableView;
... buncha other properties ...

From NoteBrowseViewController.m:

@synthesize tableView

- (id)initWithEditing:(BOOL)inEditingMode inserting:(BOOL)inserting {
self=[super init];
if (self) {
    self.isInserting=inserting;
    self.isEditing=inEditingMode;
    self.viewIsMap=NO;
    self.insertType=defInsertTypeLink;

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSDictionary *appDefaults = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:0],@"viewSortOrder",
                                 [NSNumber numberWithInt:1],@"priorityView",
                                 [NSNumber numberWithBool:NO],@"simpleView",
                                 nil];

    [defaults registerDefaults:appDefaults];

    self.viewIsSimple=[[NSUserDefaults standardUserDefaults]boolForKey:@"simpleView"];
}
return self;
}

-(void)loadView {
self.view=[[UIView alloc]initWithFrame:[[UIScreen mainScreen] applicationFrame]];

UIButton* aButton;
UIImage* buttonImage;
UIBarButtonItem* spacer=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

UISegmentedControl* sc=[[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:@"Alpha",@"Edit", @"View", nil]];
[sc addTarget:self action:@selector(segmentedControlValueChanged:) forControlEvents:UIControlEventValueChanged];
[sc setWidth:55.0 forSegmentAtIndex:0];
[sc setWidth:55.0 forSegmentAtIndex:1];
[sc setWidth:55.0 forSegmentAtIndex:2];
sc.selectedSegmentIndex=[[NSUserDefaults standardUserDefaults] integerForKey:@"viewSortOrder"];
sc.segmentedControlStyle=UISegmentedControlStyleBar;

UISegmentedControl* prisc=[[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:@"Open",@"All", nil]];
[prisc addTarget:self action:@selector(prioritySegmentedControlValueChanged:) forControlEvents:UIControlEventValueChanged];
[prisc setWidth:55.0 forSegmentAtIndex:0];
[prisc setWidth:55.0 forSegmentAtIndex:1];
prisc.selectedSegmentIndex=[[NSUserDefaults standardUserDefaults] integerForKey:@"priorityView"];
prisc.segmentedControlStyle=UISegmentedControlStyleBar;

UIBarButtonItem* segmentedButton=[[UIBarButtonItem alloc]initWithCustomView:sc];
UIBarButtonItem* prioritySegmentedButton=[[UIBarButtonItem alloc]initWithCustomView:prisc];

buttonImage=[UIImage imageNamed:@"13-plus.png"];
aButton=[UIButton buttonWithType:UIButtonTypeCustom];
[aButton setImage:buttonImage forState:UIControlStateNormal];
aButton.frame=CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
UIBarButtonItem* addButton=[[UIBarButtonItem alloc]initWithCustomView:aButton];
[aButton addTarget:self action:@selector(addButton:) forControlEvents:UIControlEventTouchUpInside];

buttonImage=[UIImage imageNamed:@"187-pencil.png"];
aButton=[UIButton buttonWithType:UIButtonTypeCustom];
[aButton setImage:buttonImage forState:UIControlStateNormal];
aButton.frame=CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
UIBarButtonItem* editButton=[[UIBarButtonItem alloc]initWithCustomView:aButton];
[aButton addTarget:self action:@selector(editButton:) forControlEvents:UIControlEventTouchUpInside];

buttonImage=[UIImage imageNamed:@"21-circle-east.png"];
aButton=[UIButton buttonWithType:UIButtonTypeCustom];
[aButton setImage:buttonImage forState:UIControlStateNormal];
aButton.frame=CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
UIBarButtonItem* simplify=[[UIBarButtonItem alloc]initWithCustomView:aButton];
[aButton addTarget:self action:@selector(simplify:) forControlEvents:UIControlEventTouchUpInside];

buttonImage=[UIImage imageNamed:@"25-circle-west.png"];
aButton=[UIButton buttonWithType:UIButtonTypeCustom];
[aButton setImage:buttonImage forState:UIControlStateNormal];
aButton.frame=CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
UIBarButtonItem* complexify=[[UIBarButtonItem alloc]initWithCustomView:aButton];
[aButton addTarget:self action:@selector(complexify:) forControlEvents:UIControlEventTouchUpInside];

buttonImage=[UIImage imageNamed:@"243-globe.png"];
aButton=[UIButton buttonWithType:UIButtonTypeCustom];
[aButton setImage:buttonImage forState:UIControlStateNormal];
aButton.frame=CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
UIBarButtonItem* map=[[UIBarButtonItem alloc]initWithCustomView:aButton];
[aButton addTarget:self action:@selector(mapify:) forControlEvents:UIControlEventTouchUpInside];

buttonImage=[UIImage imageNamed:@"162-receipt.png"];
aButton=[UIButton buttonWithType:UIButtonTypeCustom];
[aButton setImage:buttonImage forState:UIControlStateNormal];
aButton.frame=CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
UIBarButtonItem* notes=[[UIBarButtonItem alloc]initWithCustomView:aButton];
[aButton addTarget:self action:@selector(showNotes:) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem* cancelButton=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(cancelButtonPressed:)];
self.doneToolbar=[NSArray arrayWithObjects:spacer, cancelButton, nil];

self.toolbar=[[[UIToolbar alloc]init]autorelease];

if(self.isEditing) {
    self.complexToolbar=[NSArray arrayWithObjects:simplify, spacer, segmentedButton, prioritySegmentedButton, spacer, cancelButton, nil];
    self.simpleToolbar=[NSArray arrayWithObjects:complexify, spacer, cancelButton, nil];
}
else {
    self.complexToolbar=[NSArray arrayWithObjects:simplify, map, spacer, segmentedButton, prioritySegmentedButton, spacer, addButton, editButton, cancelButton, nil];
    self.simpleToolbar=[NSArray arrayWithObjects:complexify, map, spacer, addButton, editButton, cancelButton, nil];
}
self.mapToolbar=[NSArray arrayWithObjects:notes, spacer, prioritySegmentedButton, spacer, cancelButton, nil];

if (self.viewIsSimple) {
    [self.toolbar setItems:self.simpleToolbar animated:YES];
}
else {
    [self.toolbar setItems:self.complexToolbar animated:YES];
}

self.toolbar.autoresizingMask=UIViewAutoresizingFlexibleWidth;        
[self.toolbar sizeToFit];

CGFloat toolbarHeight = [self.toolbar frame].size.height;
CGRect rootViewBounds=self.view.bounds;
CGFloat rootViewHeight = CGRectGetHeight(rootViewBounds);
CGFloat rootViewWidth = CGRectGetWidth(rootViewBounds);
[self.toolbar setFrame:CGRectMake(0,0, rootViewWidth, toolbarHeight)];

self.mapView=[[[MKMapView alloc]initWithFrame:CGRectMake(0, toolbarHeight, rootViewWidth, rootViewHeight-(self.isInserting?49:0)-toolbarHeight)]autorelease];
self.mapView.delegate=self;
self.mapView.zoomEnabled=YES;
self.mapView.scrollEnabled=YES;
self.mapView.autoresizingMask=UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;

self.tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, toolbarHeight, rootViewWidth, rootViewHeight-(self.isInserting?49:0)-toolbarHeight) 
                                           style:UITableViewStylePlain];
self.tableView.delegate=self;
self.tableView.dataSource=self;
self.tableView.autoresizingMask=UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;

self.view.autoresizesSubviews = YES; 
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

UISearchBar* searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 44, rootViewWidth, 44.0)];
searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
self.tableView.tableHeaderView = searchBar;

self.searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
self.searchDisplayController.delegate = self;
self.searchDisplayController.searchResultsDataSource = self;
self.searchDisplayController.searchResultsDelegate = self;

[self.view addSubview:self.tableView];
[self.view addSubview:self.toolbar];

if (self.isInserting) {
    UITabBarItem* item1=[[[UITabBarItem alloc]initWithTitle:@"Link" image:nil tag:defInsertTypeLink]autorelease];
    UITabBarItem* item2=[[[UITabBarItem alloc]initWithTitle:@"Contents Later" image:nil tag:defInsertTypeContentsLater]autorelease];
    UITabBarItem* item3=[[[UITabBarItem alloc]initWithTitle:@"Contents Now" image:nil tag:defInsertTypeContentsNow]autorelease];
    UITabBar* tabbar=[[UITabBar alloc]initWithFrame:CGRectMake(0, rootViewHeight-49, rootViewWidth, 49)];
    tabbar.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin;
    [tabbar setDelegate:self];
    [tabbar setItems:[NSArray arrayWithObjects:item1, item2, item3, nil] animated:YES];
    [tabbar setSelectedItem:item1];
    [self.view addSubview:tabbar];
}

if(self.viewIsSimple) {
    self.contentSizeForViewInPopover = CGSizeMake(200.0, 625.0);         
}
else {
    self.contentSizeForViewInPopover = CGSizeMake(450.0, 625.0); 
}
self.view.autoresizesSubviews = YES; 
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.note=nil;

[sc release];
[prisc release];
[addButton release];
[prioritySegmentedButton release];
[cancelButton release];
[segmentedButton release];
[spacer release];
[editButton release];
[map release];
[simplify release];
[complexify release];
}

And finally, the a NoteBrowseViewController is instantiated thusly from another view controller:

self.noteBrowseViewController=[[NoteBrowseViewController alloc]initWithEditing:NO inserting:NO];
self.noteBrowseViewController.delegate=self;
self.popoverController = [[UIPopoverController alloc]initWithContentViewController:self.noteBrowseViewController];
self.popoverController.delegate = self;
[self.popoverController presentPopoverFromRect:((UIButton*)sender).frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

So this is what happens. If I run this in debug, and follow along, as soon as execution gets to this line:

CGRect rootViewBounds=self.view.bounds;

The program crashes with the following errors:

2011-10-20 11:42:02.703 ActionNote3[12332:15803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]:  object cannot be nil'
*** First throw call stack:
(0x1eb0052 0x2a60d0a 0x1e9d36e 0x1e9e220 0x1968cb6 0x5153b 0x50e11 0x50879 0x52e4a 0xdcc64e 0x4bcb3 0x72d1 0x1eb1ec9 0xd095c2 0xd0955a 0xdaeb76 0xdaf03f 0xdae2fe 0xd2ea30 0xd2ec56 0xd15384 0xd08aa9 0x20c7fa9 0x1e841c5 0x1de9022 0x1de790a 0x1de6db4 0x1de6ccb 0x20c6879 0x20c693e 0xd06a9b 0x2dbd 0x2d35)
terminate called throwing an exception

By watching things during the debug, I know self.view is being set up properly, and that loadView is being called when it should be. And it's not this line that makes it fail - it's anything that refers to self.view! what???

What really makes me crazy is that I have another subclassed view controller, HistoryBrowser, that is loaded in exactly the same manner... and it works fine.

So, aside from being upset that I cannot figure this out, i'd like to understand:

  1. What's changed in XCode 4.2 (from 4.1) that would cause this, or is
    this an IOS 5 issue?
  2. What does this error mean, and what can I do
    about it?

EDIT:

So based on suggestions from Abberant's answer below, I:

  1. changed the initwithnib to just be init
  2. removed the loadView method
  3. added the full error message
  4. added more relevant code to the init method

And now, referencing self.view works properly. But things have just gotten stranger.

Execution makes it past the first reference to self.view without error. However, it now halts at:

self.tableView.tableHeaderView = searchBar;

in the init method.

The error message received is eerily similar to the one I was receiving before:

2011-10-20 12:34:04.818 ActionNote3[13487:15803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
*** First throw call stack:
(0x1eaf052 0x2a5fd0a 0x1e9c36e 0x1e9d220 0x1967cb6 0x50b4b 0x50421 0x4fe89 0x4d757 0x6641 0x1eb0ec9 0xd085c2 0xd0855a 0xdadb76 0xdae03f 0xdad2fe 0xd2da30 0xd2dc56 0xd14384 0xd07aa9 0x20c6fa9 0x1e831c5 0x1de8022 0x1de690a 0x1de5db4 0x1de5ccb 0x20c5879 0x20c593e 0xd05a9b 0x212d 0x20a5)
terminate called throwing an exception

So, if I comment that line out, execution procedes until it reaches this group of lines, where it fails when it attempts to do the performFetch.

    NSError *error = nil;
    if (![[self currentFetchedResultsController] performFetch:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

Debugging into the code for [self currentFetchedResultsController] shows that the fetched results controller is being set up properly and without issue.

The error received here is the same as above. The numbers in the first throw call stack change, but otherwise the error is the same.

So I'm left thinking that I have a larger problem going on here. I just have no idea how to track it down. As I said way up there somewhere, I have another view, constructed in the same manner, that loads fine.

Edit:

So NJones in his answer below suggested following Apple's guidelines by placing the appropriate code in loadView and viewDidLoad. So I did. I moved all view construction out of init and into loadView, and fired up the fetchedResultsController in viewDidLoad.

The app still crashes in the same spot:

self.tableView.tableHeaderView = searchBar

With the same error as noted above.

Thanks!

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

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

发布评论

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

评论(4

反话 2024-12-18 04:59:18

我想说你应该使用苹果建议的加载模型。这是当您创建新的 UIViewController 子类时在 xCode 中生成的一些代码(经过删节以适应):

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
-(void)loadView{
}
*/

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

基于此,由于您是以编程方式创建视图,所以我想说您 init 方法中的大部分内容都属于加载视图方法。

I would say you should use the loading model apple suggests. This is some of the code generated in xCode when you create a new UIViewController subclass (abridged to fit):

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
-(void)loadView{
}
*/

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

Based on just this, Since you are creating your view programmatically, I would say that most of what you have in your init method belongs in the loadView method.

爱冒险 2024-12-18 04:59:18

所以我找到了答案,如果没有看到更多代码,任何人都不会猜到答案。但是,希望这个答案能够帮助其他可能遇到同样问题的人。

在我的 loadView 方法中(但最初在我的 init 方法中,并且仍然在上面的代码中),我有以下几行:

UISegmentedControl* sc=[[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:@"Alpha",@"Edit", @"View", nil]];
[sc addTarget:self action:@selector(segmentedControlValueChanged:) forControlEvents:UIControlEventValueChanged];
[sc setWidth:55.0 forSegmentAtIndex:0];
[sc setWidth:55.0 forSegmentAtIndex:1];
[sc setWidth:55.0 forSegmentAtIndex:2];
sc.selectedSegmentIndex=[[NSUserDefaults standardUserDefaults] integerForKey:@"viewSortOrder"];
sc.segmentedControlStyle=UISegmentedControlStyleBar;

segmentedControlValueChanged:将设置一个字符串 var(并将所选按钮的索引存储在用户默认值中) )。字符串 var 后来用于在启动 fetchedresultscontroller 时确定排序顺序。

在 IOS 5 之前,当我设置 sc.selectedSegmentIndex 时,选择器中标识的方法将被触发,并且所有内容都会正确设置。在 iOS 5 中,情况不再如此,并且字符串 var(设置排序顺序)将不会被设置。

有趣的是,执行此行后:

self.tableView.tableHeaderView = searchBar;

将导致 fetchedReultsController 被访问,并且通过访问它,它将被初始化。当它被初始化时,它将设置排序顺序并包含字段名称的空值,因此出现错误消息,现在这是有意义的。

所以问题的根本原因是,在 IOS 5 之前,在 UISegmentedControl 上设置 selectedSegmentIndex 会触发与该控件关联的操作。从 iOS 5 开始,情况不再如此。

这个问题的最初原因是我没有正确遵循 viewController 加载过程,几个人帮助我解决了这个问题。我没有使用 NIB 文件。 Apple 表示,如果不使用 nib 文件,则必须定义 loadView 来设置视图。如果没有定义loadView,则内部设置self.view,并调用viewDidLoad。应用程序在第一次引用 self.view 时崩溃的原因是我在 viewDidLoad 中有逻辑来设置 fetchedResultsController。当我将视图设置逻辑移至 loadView 时,对 self.view 的引用不再启动 fetchedresultscontroller。

So I found the answer, and it's not something anyone would have guessed without seeing a lot more code. But, hopefully this answer will help other people who might run into the same thing.

In what has become my loadView method (but was originally in my init method, and still is in the code above), I had the following lines:

UISegmentedControl* sc=[[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:@"Alpha",@"Edit", @"View", nil]];
[sc addTarget:self action:@selector(segmentedControlValueChanged:) forControlEvents:UIControlEventValueChanged];
[sc setWidth:55.0 forSegmentAtIndex:0];
[sc setWidth:55.0 forSegmentAtIndex:1];
[sc setWidth:55.0 forSegmentAtIndex:2];
sc.selectedSegmentIndex=[[NSUserDefaults standardUserDefaults] integerForKey:@"viewSortOrder"];
sc.segmentedControlStyle=UISegmentedControlStyleBar;

segmentedControlValueChanged: would set a string var (and store the index of the selected button in user defaults). The string var was later used to determine sort order when firing up my fetchedresultscontroller.

Prior to IOS 5, when I set sc.selectedSegmentIndex, the method identified in the selector would get fired, and everything would get set up properly. In IOS 5, this is no longer the case, and the string var (setting a sort order) would not get set.

What's interesting is that upon execution this line:

self.tableView.tableHeaderView = searchBar;

would cause the fetchedReultsController to be accessed and, by accessing it, it would be initialized. When it went to be initialized, it would set the sort order and include a null value for the field name, hence the error message, which now makes sense.

So the root cause of the problem was that prior to IOS 5, setting the selectedSegmentIndex on a UISegmentedControl would fire the action associated with the control. Starting with IOS 5, this is no longer the case.

The original cause of the problem, which several people helped me resolve, was that I was not following the viewController load process properly. I am not using NIB files. Apple says that if you dont use a nib file, you have to define loadView to set up the view. If loadView is not defined, self.view is set internally, and viewDidLoad is called. The reason the app crashed at the first reference of self.view, then, is that I had logic in viewDidLoad to set up the fetchedResultsController. When I moved the view setup logic into loadView, references to self.view no longer started up the fetchedresultscontroller.

執念 2024-12-18 04:59:18

我已经思考并寻找这方面的信息有一段时间了,但可能有很多事情。也许 self.view 在 loadView 之前被调用,也许是因为 initWithNibName 触发了你不想要的东西,也许是因为你的视图小于屏幕/窗口,这是不建议的,也许新的 iOS 有一个错误它......你的代码太少,无法调试并且错误消息不完整,我看不出是什么原因造成的。

但是,我可能有一个解决方法:

如何删除 loadView 覆盖,以便 viewController 创建自己的视图,然后将 mainView 添加到该默认视图?

Been thinking about and looking for information on this for a while, but it might be any number of things. Perhaps the self.view gets called before the loadView somewhere, maybe it's because initWithNibName triggers something you don't want, maybe it's because your view is smaller than the screen/window, which is not advised, maybe the new iOS has a bug in it... With so little of your code, no way to debug and an incomplete error message, I can't see what causes it.

However, I might have a workaround:

How about removing that loadView override so that the viewController makes its own view, then add your mainView to that default view?

屋檐 2024-12-18 04:59:18

异常表示您正在向数组添加 nil。我认为 tableViewHeader 可以被视为 objectAtIndex:0 因为它是行数组的第一行。尝试将此行放在其上方:

NSLog(@"searchBar = %@",searchBar;
self.tableView.tableHeaderView = searchBar;

编辑(1):
然后尝试这些:

NSLog(@"self = %@",self);
NSLog(@"self.tableView = %@",self.tableView);
NSLog(@"self.tableView.tableHeaderView = %@",self.tableView.tableHeaderView);
self.tableView.tableHeaderView = searchBar;

编辑(2):

if ([self.tableView respondsToSelector:@selector(setTableHeaderView:)]){
    NSLog(@"tableView does respond");
    [self.tableView setTableHeaderView:nil];
}

我认为这不会解决问题,但它应该有助于找出问题。

The exception says you are adding a nil to an array. I'm think tableViewHeader could be considered an objectAtIndex:0 since it's the first of an array of rows. Try putting this line above it :

NSLog(@"searchBar = %@",searchBar;
self.tableView.tableHeaderView = searchBar;

Edit(1):
Then try these:

NSLog(@"self = %@",self);
NSLog(@"self.tableView = %@",self.tableView);
NSLog(@"self.tableView.tableHeaderView = %@",self.tableView.tableHeaderView);
self.tableView.tableHeaderView = searchBar;

Edit(2):

if ([self.tableView respondsToSelector:@selector(setTableHeaderView:)]){
    NSLog(@"tableView does respond");
    [self.tableView setTableHeaderView:nil];
}

I don't think this will fix the problem, but it should help hunt down the problem.

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