簡體   English   中英

在將情節提要視圖添加為子視圖時,將消息發送到已解除分配的實例

[英]Message Sent to Deallocated Instance when adding storyboard view as a subview

好的,我對這里發生的事情有基本的了解,但是很難解決。 我希望有人可以引導我了解我在這里做錯了什么...

我有一個漂亮的應用程序,它運行良好,並且是使用情節提要和自定義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];
    }
}

我不確定自己在做什么錯,但是無論我按該頁面上的任何按鈕還是試圖消除該視圖,無論發生什么情況,它都會對我尖叫並生氣...我已經盡了一切努力來解決這個問題。

多謝你們!

問題是您正在創建視圖控制器,獲取其視圖,但是讓該控制器超出范圍(並可能使用在您發布它的地方使用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];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM