Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 视图消失时调用的ViewController加载方法?_Objective C_Uiviewcontroller - Fatal编程技术网

Objective c 视图消失时调用的ViewController加载方法?

Objective c 视图消失时调用的ViewController加载方法?,objective-c,uiviewcontroller,Objective C,Uiviewcontroller,我在viewController中有以下代码。我在视图上有一个NavigationController(这是子视图-父视图的代码工作正常) 当我在父对象上选择一个选项时,将加载此viewController。用户可以从子viewController中选择一个选项,使用DocumentInteractionController打开PDF文件(该选项工作正常) 问题是,当我尝试返回到父视图控制器时,消息被发送到子视图控制器,就好像它仍然被分配一样。我在设置它时看到了类似的情况,因为在子viewCon

我在viewController中有以下代码。我在视图上有一个NavigationController(这是子视图-父视图的代码工作正常)

当我在父对象上选择一个选项时,将加载此viewController。用户可以从子viewController中选择一个选项,使用DocumentInteractionController打开PDF文件(该选项工作正常)

问题是,当我尝试返回到父视图控制器时,消息被发送到子视图控制器,就好像它仍然被分配一样。我在设置它时看到了类似的情况,因为在子viewController中有多个方法调用

有没有想过我做错了什么

#import "DetailViewController.h"

@interface DetailViewController ()

@end

@implementation DetailViewController

@synthesize node;
@synthesize replies;
@synthesize docController;

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tableView reloadData];
    [self.tableView setContentOffset:CGPointZero animated:NO];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.docController init];
    // Do any additional setup after loading the view from its nib.
}

- (void) dealloc
{
    [self.docController release];
    [super dealloc];
}

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

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.replies == nil)
    {
        self.replies = [[NSArray alloc] init];
        self.actions = [[NSArray alloc] init];
    }
    if(self.replies.count == 0)
    {
        self.replies = [self.node nodesForXPath:@"./question/reply/text" error:nil];
        self.actions = [self.node nodesForXPath:@"./question/reply/response/action" error:nil];
    }

    return self.replies.count;
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"QuestionCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Get the object to display and set the value in the cell
    NSString *cellText = [[replies objectAtIndex:indexPath.row] stringValue];
    cell.textLabel.text = cellText;
    return cell;
}

- (void) showOptionsMenu:(NSString *) fileName
{

    NSString *fileToOpen = [[NSBundle mainBundle] pathForResource:fileName ofType:@"pdf"];
    NSURL *fileURL = [NSURL fileURLWithPath:fileToOpen];

    self.docController = [self setupControllerWithURL:fileURL usingDelegate:self];

    bool didShow = [self.docController presentOptionsMenuFromRect:CGRectMake(0, 0, 150, 150) inView: self.view animated:YES];

    if(!didShow)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Sorry, app not found" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    }

}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *action = [[self.actions objectAtIndex:indexPath.row] stringValue];
    [self showOptionsMenu:action];
}

- (UIDocumentInteractionController *) setupControllerWithURL: (NSURL *) fileURL usingDelegate:(id <UIDocumentInteractionControllerDelegate>) interactionDelegate
{
    UIDocumentInteractionController *interactionController = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
    interactionController.delegate = interactionDelegate;
    return interactionController;
}


@end
编辑

我尝试过分析和查找僵尸,但当崩溃发生时,没有标记僵尸对象。它在控制台中抛出以下错误:

[UIView _forgetDependentConstraint:]: message sent to deallocated instance 0x1e8ab810

您的DetailViewController代码很好-实际上并不好,因为您正在泄漏self.repress和self.actions,[self.docController init]非常奇怪,可能是错误的(始终是alloc和init在一起)-但是这一端的生命周期代码看起来很好。几乎可以肯定的是,问题出在父视图控制器(如果您在那里创建保留周期,则可能是文档控制器)中。如果父视图控制器抓住指向详细视图控制器的指针,它实际上不会被释放,访问视图或其任何属性将导致再次调用-viewDidLoad。

据我所知,父视图控制器正在此处设置节点:

    dvc.node = selectedReply;
而且它永远不会从您的DetailViewController中释放

我假设DetailViewController标题中的GDataxmlement设置为“retain”

正如icodestuff指出的,存在一些泄漏问题

我以前也看过这个问题

回答:
关闭“自动布局”。

我猜错误是由于
ios
中名为
AutoLayout
的新功能引起的。看起来编译器创建了一些
NSLayoutConstraint
对象,由于某些原因,这些对象被释放的次数超出了它们应该释放的次数。删除和重新创建,强制
Xcode
重新构建约束。但是,我不是100%肯定


如果可以解决您的问题,请尝试

向子viewController发送的消息或调用的方法是什么?我正在试图弄清楚-输出窗口中有一些输出,但它没有告诉我发送的消息。这看起来就像是对孩子的回电。顺便说一句,我没有使用ARC。看起来好像有人调用了_forgetDependentConstraint,但我不知道它是从哪里来的…@Tim:你是在使用“自动布局”吗?听起来很值得一试。自动布局选项在哪里?(我对XCode比较陌生)@Tim:检查这里:这就是问题所在!现在我的代码按预期工作!谢谢,文!
    dvc.node = selectedReply;