ios - 将 Storyboard View 添加为 subview 时发送到已释放实例的消息

标签 ios objective-c xcode memory-management uiviewcontroller

好的,我对这里发生的事情有一个基本的了解,但我无法修复它。我希望有人可以引导我解决我在这里做错的事情......

我有一个漂亮的应用程序,它运行良好,是用 Storyboard和自定义 UIViewControllers 构建的,可以处理我的所有代码。我做得非常好,直到我需要通过将我拖放到特定 View 并加载一些数据来处理我的推送通知。我今天取得了很多进展,只是陷入了困境。我现在收到 objc_sendmsg 错误,我知道这与我的内存管理有关。我从来没有以这种方式初始化 View ,所以我想知道这是否是导致它的原因。基本上,我可以加载一个 View ,但之后我永远无法按下任何按钮或到达任何地方。

代码如下:

AppDelegate.m

    UIStoryboard* storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    UIViewController *detailVC = [storyBoard instantiateViewControllerWithIdentifier:@"Leads_Calls_SB"];
    [self.window addSubview:detailVC.view];

    [[NSNotificationCenter defaultCenter] postNotificationName:@"callReceived"
                                                            object:nil
                                                          userInfo:userInfo];

    [self.window makeKeyAndVisible];

Leads_CallsDetailViewController.h

@property (strong, nonatomic) IBOutlet UILabel *cName;
@property (strong, nonatomic) IBOutlet UILabel *cStart;
@property (strong, nonatomic) IBOutlet UILabel *cNumber;
@property (strong, nonatomic) IBOutlet UILabel *cStart2;
@property (strong, nonatomic) IBOutlet UILabel *cEnd;
@property (strong, nonatomic) IBOutlet UILabel *cDuration;
@property (strong, nonatomic) IBOutlet UILabel *cStatus;
@property (strong, nonatomic) IBOutlet UILabel *cProvider;
@property (strong, nonatomic) IBOutlet UILabel *cLineType;
@property (strong, nonatomic) IBOutlet UILabel *cCity;
@property (strong, nonatomic) IBOutlet UIView *innerView;
@property (strong, nonatomic, retain) IBOutlet UIButton *backStyle;


@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;

@property (strong, nonatomic, retain) NSString *cNotifID;

@property (strong, nonatomic, retain) NSString *cNameText;
@property (strong, nonatomic, retain) NSString *cNumberText;
@property (strong, nonatomic, retain) NSString *cStartText;
@property (strong, nonatomic, retain) NSString *cEndText;
@property (strong, nonatomic, retain) NSString *cCallStatusText;
@property (strong, nonatomic, retain) NSString *cLatitudeText;
@property (strong, nonatomic, retain) NSString *cLongitudeText;
@property (strong, nonatomic, retain) NSString *cCityText;
@property (strong, nonatomic, retain) NSString *cLineTypeText;
@property (strong, nonatomic, retain) NSString *cProviderNameText;
- (IBAction)back:(id)sender;
@property (strong, nonatomic) IBOutlet MKMapView *map;

- (IBAction)forward_lead:(id)sender;
- (IBAction)call_lead:(id)sender;
- (IBAction)add_lead:(id)sender;

@property (strong, nonatomic) IBOutlet UIButton *bottomMessage;
@property (strong, nonatomic) IBOutlet UIButton *bottomCalls;
@property (strong, nonatomic) IBOutlet UIButton *bottomReports;
@property (strong, nonatomic) IBOutlet UIButton *bottomHome;

.m

- (IBAction)back:(id)sender {
    if (self.cNotifID != nil)
    {
        [self.view removeFromSuperview];
    }
    else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

我不确定我做错了什么,但无论我点击该页面上的任何按钮或试图关闭该 View 会发生什么,它都会对我尖叫并生气......我已经尝试了一切我想办法解决这个问题。

谢谢大家!

最佳答案

问题是你正在创建你的 View Controller ,获取它的 View ,但是让 Controller 超出范围(并且可能使用 ARC 来释放它)。

在我最初的回答中,我认为目标只是考虑呈现标准初始 View Controller 的不同方式。但事实并非如此。问题是当某些事件发生时如何呈现新场景(在我的示例中,我是在 openURL 上做的,但您可能会这样做以响应通知等)。

无论如何,解决这个问题的一种方法是执行presentViewController。所以你可以这样做:

// determine the current controller (in case you've already done some modal segues)

UIWindow *window = [[UIApplication sharedApplication] keyWindow];
UIViewController *currentController = window.rootViewController;
while (currentController.presentedViewController)
    currentController = currentController.presentedViewController;

// load the controller for the new scene

UIStoryboard* storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *newController = [storyBoard instantiateViewControllerWithIdentifier:@"Leads_Calls_SB"];

// perform a modal transition to the new scene

[currentController presentViewController:newController animated:NO completion:nil];

关于ios - 将 Storyboard View 添加为 subview 时发送到已释放实例的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17534555/

相关文章:

ios - iOS 启动屏幕上的动画事件指示器

ios - 更新了适用于 iOS 7 的 TBXML : How to include TBXML in Xcode 5, 的指南

iphone - iphone 4的后台应用

objective-c - Xcode 7 (iOS 9) 通讯错误 : <OS_xpc_error: <error: 0x10a1abb40>

ios - XCode 7.01 创建文件时崩溃

ios - 如何保持 UITableView 单元格处于选中状态

javascript - html音频在Macbook和ios上不起作用,但在imac上起作用—很奇怪

ios - 如何将 Firebase 中的用户个人列表抓取到表格 View

ios - 关闭后弹出 View Controller

ios - 通过 NSString 过滤字典数组