iOS App Error - Can't Add Self as Subview

iOS app error - Can't add self as subview

I will describe more details about this crash in my app and mark this as answered.

My app has a UINavigationController with the root controller is a UITableViewController that contains a list of note objects. The note object has a content property in html. Select a note will go to the detail controller.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//get note object
DetailViewController *controller = [[DetailViewController alloc] initWithNote:note];
[self.navigationController pushViewController:controller animated:YES];
}

Detail controller

This controller has a UIWebView, display the note content passed from the root controller.

- (void)viewDidLoad
{
...
[_webView loadHTMLString:note.content baseURL:nil];
...
}

This controller is the delegate of the webview control. If the note contains links, tap a link will go to the in-app web browser.

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
WebBrowserViewController *browserController = [[WebBrowserViewController alloc] init];
browserController.startupURL = request.URL;
[self.navigationController pushViewController:webViewController animated:YES];
return NO;
}

I received the above crash report everyday. I don't know where in my code caused this crash. After some investigates with the help of a user, I was finally able to fix this crash. This html content will cause the crash:

...

...

In the viewDidLoad method of the detail controller, I loaded this html to the webview control, right after that, the above delegate method was called immediately with request.URL is the iframe's source (google.com). This delegate method calls pushViewController method while in viewDidLoad => crash!

I fixed this crash by checking the navigationType:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if (navigationType != UIWebViewNavigationTypeOther)
{
//go to web browser controller
}
}

Hope this helps

Can't add self as subview crash (with sample code) when view not adding itself to its subview

Due to the incorrect code in viewDidLoad, self.view, self.tableView, and tableView are all the same view. Hence the error.

This is a UITableViewController. It already has a table view. Do not create another one. Update viewDidLoad as follows:

override func viewDidLoad() {
super.viewDidLoad()

tableView.separatorColor = UIColor(white:0.85, alpha:1)
tableView.register(UITableViewCell.self, forCellReuseIdentifier:"nephews")
}

You also do not need to add any of those init methods.

[iOS 13]: Fatal Exception: NSInvalidArgumentException Can't add self as subview ([NSCache init])

The app built with Xcode 11.3 & iOS SDK 13.3 doesn't have this crash anymore. I guess it was solved on the iOS SDK side.

UPDATE: 18.06.2020

For those, who continue having the issue, I'd recommend updating to the new Firebase Crashlytics from the old Fabric Crashlytics. The guide is here: https://firebase.google.com/docs/crashlytics/upgrade-sdk?platform=ios

Moreover, I got an email from Firebase about the old SDK deprecation:

We are writing to let you know that the Fabric Crashlytics SDK is now
deprecated and will continue reporting your app's crashes until
November 15, 2020. After this date, the Fabric Crashlytics SDK and
beta versions of the Firebase Crashlytics SDK will stop sending crash
reports for your app.

Uinavigation error while i press back button

maybe you want this I guess it will help you solve your problem
Note: -Username and Password are "praveen". I have attached the sample project to this link.
http://www.filedropper.com/tabbarapplicationwithlogin

Constraints Error while trying to add button as subview

Inside your custom viewController call this method

UIButton* skipBreakButton;


- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.

NSURL *url = [[NSURL alloc] initWithString:@""];

AVPlayer *player = [AVPlayer playerWithURL:url];
AVPlayerViewController *controller = [[AVPlayerViewController alloc] init];

[self addChildViewController:controller];
[self.view addSubview:controller.view];

controller.view.frame = CGRectMake(0,0,UIScreen.mainScreen.bounds.size.width,UIScreen.mainScreen.bounds.size.height);
controller.player = player;
controller.showsPlaybackControls = YES;
[player play];

NSLog(@"**** In setupBreakButton....");

skipBreakButton = [[UIButton alloc] initWithFrame:CGRectMake(50, 50, 200, 200)];
skipBreakButton.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:1.0f];
skipBreakButton.layer.cornerRadius = 6;

[skipBreakButton addTarget:self action:@selector(didTapButton:) forControlEvents:UIControlEventPrimaryActionTriggered];

[self.view addSubview: skipBreakButton];

skipBreakButton.translatesAutoresizingMaskIntoConstraints = NO;
/* Leading space to superview */
NSLayoutConstraint *leftButtonXConstraint = [NSLayoutConstraint
constraintWithItem:skipBreakButton attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual toItem:self.view attribute:
NSLayoutAttributeLeading multiplier:1.0 constant:30];
/* Top space to superview Y*/
NSLayoutConstraint *leftButtonYConstraint = [NSLayoutConstraint
constraintWithItem:skipBreakButton attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual toItem:self.view attribute:
NSLayoutAttributeTop multiplier:1.0f constant:258];
/* Fixed width */
NSLayoutConstraint *widthConstraint = [NSLayoutConstraint constraintWithItem:skipBreakButton
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:35];
/* Fixed Height */
NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:skipBreakButton
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:12];
/* 4. Add the constraints to button's superview*/

[self.view addConstraints:@[leftButtonXConstraint, leftButtonYConstraint, widthConstraint, heightConstraint]];
}

- (NSArray> *)preferredFocusEnvironments {

return @[skipBreakButton];
}

-(void)didTapButton:(UIButton *) sender {
NSLog(@"didtapbutton");
}


Related Topics



Leave a reply



Submit