gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 20:17:20 25 4
gpt4 key购买 nike

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

我有一个漂亮的应用程序,它运行良好,是用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com