【问题标题】:ViewController respondsToSelector:]: message sent to deallocated instanceViewController respondsToSelector:]: 消息发送到释放的实例
【发布时间】:2012-05-19 05:04:18
【问题描述】:

当我启用“僵尸对象”时,我收到此错误(“ViewController respondsToSelector:]: message sent to deallocated instance”)。我发现错误在哪里,但我不知道如何解决。

代码如下: ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UISearchDisplayDelegate, UISearchBarDelegate>{


    // The saved state of the search UI if a memory warning removed the view.
    NSString        *savedSearchTerm;
    NSInteger       savedScopeButtonIndex;
    BOOL            searchWasActive;
}

@property (nonatomic, copy) NSString *savedSearchTerm;
@property (nonatomic) NSInteger savedScopeButtonIndex;
@property (nonatomic) BOOL searchWasActive;

ViewController.m

...
// when I comment out "viewDidDisappear" everything is ok, how to solve this on different way?
- (void)viewDidDisappear:(BOOL)animated
{
    // save the state of the search UI so that it can be restored if the view is re-created
    self.searchWasActive = [self.searchDisplayController isActive];
    self.savedSearchTerm = [self.searchDisplayController.searchBar text];
    self.savedScopeButtonIndex = [self.searchDisplayController.searchBar selectedScopeButtonIndex];
}...

谢谢你的帮助

【问题讨论】:

  • 你解决过这个问题吗?我也有同样的问题。
  • @EliGregory - 是的。我评论 viewDidDisappear :)
  • 所以我没有重写那个方法,我得去别处看看
  • 万一它是相关的,请查看这个类似的问题:stackoverflow.com/questions/11170614/…

标签: iphone objective-c xcode


【解决方案1】:

你在做什么来“创造”这个错误?

在我的情况下,我将 ViewController 从导航堆栈中弹出,仍然有 NSNotifications 发送到这个 VC。我只是忘记删除我的 VC 的所有观察者。

【讨论】:

    【解决方案2】:

    您应该将 [super viewDidDisappear:animated] 放在覆盖的开头,因为这是默认实现。忘记那条线有时会导致问题。

    编辑:

    我认为您可能需要发布更多代码。关键可能在于您正在做或不做的其他事情。

    【讨论】:

      【解决方案3】:
      /*
      - (void)viewDidDisappear:(BOOL)animated
      {
          // save the state of the search UI so that it can be restored if the view is re-created
          self.searchWasActive = [self.searchDisplayController isActive];
          self.savedSearchTerm = [self.searchDisplayController.searchBar text];
          self.savedScopeButtonIndex = [self.searchDisplayController.searchBar selectedScopeButtonIndex];
      }
      */
      

      【讨论】:

        猜你喜欢
        • 2013-08-14
        • 2013-08-17
        • 2013-04-13
        • 1970-01-01
        • 2012-07-14
        • 1970-01-01
        • 2017-05-12
        • 1970-01-01
        • 2017-05-18
        相关资源
        最近更新 更多