我的应用程序包含2个视图。 MainView和ResultsView。 MainView包含需要填充的UITextField,然后如果触摸了某个按钮,则视图将更改为ResultsView。下面是处理该问题的代码的多汁部分。
// In CallerIDAppDelegate.h
@class MainViewController;
@class ResultsViewController;
@interface CallerIDAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
IBOutlet MainViewController *mainViewController;
IBOutlet ResultsViewController *resultsViewController;
NSString *phoneNumber;
NSMutableArray *parsingResults;
BOOL isSearchingForSaved;
}
[...]
// In CallerIDAppDelegate.m
[...]
- (void)applicationDidFinishLaunching:(UIApplication *)application {
/* Set up and display MainView */
MainViewController *mainController = [[MainViewController alloc] initWithNibName:@"MainView" bundle:[NSBundle mainBundle]];
self.mainViewController = mainController;
[window addSubview:mainController.view];
// Override point for customization after application launch
[window makeKeyAndVisible];
}
-(void)switchViews{
if(self.resultsViewController == nil){
ResultsViewController *resultsController = [[ResultsViewController alloc] initWithNibName:@"ResultsViewController" bundle:nil];
self.resultsViewController = resultsController;
[resultsController release];
}
if(self.mainViewController.view.superview == nil){
[resultsViewController.view removeFromSuperview];
[window insertSubview:mainViewController.view atIndex:0];
} else {
[mainViewController.view removeFromSuperview];
[window insertSubview:resultsViewController.view atIndex:0];
}
}
现在在ResultsView
我有一个包含一些内容的tableview。我想要做的是,当选择该表视图中的一行时,我想调用ABUnknownPersonViewController
,以便用户可以将联系人保存到地址簿。
所以在ResultsViewController.m
:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[...]
ABUnknownPersonViewController *unknownPersonView = [[[ABUnknownPersonViewController alloc] init] autorelease];
[unknownPersonView setUnknownPersonViewDelegate:self];
[unknownPersonView setDisplayedPerson:personRecord];
[unknownPersonView setAllowsAddingToAddressBook:YES];
UINavigationController *navigationController = [[[UINavigationController alloc] initWithRootViewController:unknownPersonView] autorelease];
[self presentModalViewController:navigationController animated:YES];
}
- (void)unknownPersonViewController:(ABUnknownPersonViewController *)unknownPersonView didResolveToPerson:(ABRecordRef)person {
CallerIDAppDelegate *delegate = (CallerIDAppDelegate *)[[UIApplication sharedApplication] delegate];
DLog("Called!");
[self dismissModalViewControllerAnimated:YES]; //caused crash
// Caused crash too
//[delegate.resultsViewController dismissModalViewControllerAnimate:YES];
}
选择行时,ABUnknownPersonViewController
会正确显示。它还包含通过personRecord
发送的所有正确信息,并在按下“添加到联系人”按钮时将联系人正确保存在地址簿中。但是,当在最后一步按下完成(或取消)按钮时,它崩溃了很糟糕!
问题是,我无法从调试器中理解任何内容,因为在启动时,它会说“加载43453堆栈帧......”,我停止了哪个程序。堆栈跟踪的顶部是:
[CALayer sublayers];
然后有很多的
[UIView(Hierarchy) _makeSubtreePerformSelector:withObject:withObject:copySublayers:]
请赐教。我确信这可能是一个有点愚蠢的事情,我做错了。我一直在努力解决这个问题,但我做不到。 :(
答案 0 :(得分:4)
修复它.....确实很愚蠢。
应该是:
- (void)unknownPersonViewController:(ABUnknownPersonViewController *)unknownPersonView didResolveToPerson:(ABRecordRef)person {
CallerIDAppDelegate *delegate = (CallerIDAppDelegate *)[[UIApplication sharedApplication] delegate];
DLog("Called!");
[unknownPersonView dismissModalViewControllerAnimated:YES];
}