将故事板视图添加为子视图时,将消息发送到已解除分配的实例

时间:2013-07-08 19:36:33

标签: ios objective-c xcode memory-management uiviewcontroller

好的,我对这里发生的事情有一个基本的了解,但我在修复它时遇到了麻烦。我希望有人可以告诉我我在这里做错了什么......

我有一个非常棒的应用程序很好用,它是用故事板和自定义UIViewControllers构建来处理我的所有代码的。我做得很好,直到我需要通过将我放在特定视图中并加载一些数据来处理我的推送通知。我今天取得了很大的进展,并且陷入了困境。我现在收到一个objc_sendmsg错误,我知道它与我的内存管理有关。我从来没有用这种方式初始化一个视图,所以我想知道这是不是导致它的原因。基本上,我可以加载一个视图,但在此之后我永远不会按任何按钮或任何地方。

以下是代码:

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];
    }
}

我不确定我做错了什么,但无论发生什么事情,如果我点击该页面上的任何按钮或试图解除视图,它会尖叫我并生气...我已经尝试了一切我可以想到解决这个问题。

谢谢你们!

1 个答案:

答案 0 :(得分:3)

问题是你正在创建你的视图控制器,抓住它的视图,但让控制器超出范围(并且可能使用ARC,它会在你身上发布)。

在我的原始答案中,我认为目标只是考虑呈现标准初始视图控制器的不同方式。但事实并非如此。问题是如何在某些事件发生时呈现新场景(在我的示例中,我在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];