Swift app crashes when popToRootViewController called - Exc Bad Access - swift

My Swift app crashes when I have about 4 view controllers on my navigation stack and then at the 4th I call popToRootViewController. It pops, so I know the UINavigationController exists, but the app crashes without any error other than Exc Bad Access. It also crashes when I dismiss the UINavigationController instead of popping to root view controller.
Note: This happens only when running app on my test device. When my it's ran while my test device is connected to Xcode, it never crashes.
This is the only code in my final view controller, other than viewDidLoad of course.
Code:
#IBAction func closeBtnPressed(sender: AnyObject) {
//dismissViewControllerAnimated(true, completion: nil)
self.navigationController?.popToRootViewControllerAnimated(true)
}
Any ideas? Thanks!

Related

Hide navigation bar in iOS16 + xcode 14 beta not working

We use Firebase's Crashlytics and in the last 2 days we've seen a spike in crashes with navigationController?.setNavigationBarHidden on iOS 16 devices. I was able to reproduce the crash after installing Xcode 14.5
I have a view controller (VC1) embedded in a navigational controller with the following code in
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
...
navigationController?.setNavigationBarHidden(false, animated: animated) // crash here
}
and after I push another view controller (VC2) using the following code
navigationController?.pushViewController(vc, animated: true)
the app crashes with the following error message
2022-08-10 15:07:04.866752-0700 autolist-dev[22341:3295980] *** Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to activate constraint with anchors <NSLayoutXAxisAnchor:0x600001a9d740 "_UITAMICAdaptorView:0x7fd6c70ddbe0.leading"> and <NSLayoutXAxisAnchor:0x600001466680 "_UINavigationBarTitleControl:0x7fd6c70dcb80.leading"> because they have no common ancestor. Does the constraint or its anchors reference items in different view hierarchies? That's illegal.'
It doesn't immediately crash the first time VC2 is pushed. After I go back to VC1 and then push VC2 again then the app crashes.
Does anyone have any ideas on how to resolve this? I can't seem to find anyone else having a similar issue

message sent to deallocated instance 0x600000147e80 error by clicking sub view event in container view OS X Swift

I am new in os x programming. I am using swift to developing my mac app. In my app home screen contains a Container view and some NSButtons. By clicking on the buttons the NSViewControllers (created in storyboard) appear on the ContainerView. This is done by the code below:
self.ContainerView.subviews.removeAll()
let myViewController = iR_DELEGATE.GoToPage("Questions",container: ContainerView)
self.ContainerView.addSubview(myViewController.view)
the GoToPage function code is:
func GoToPage(identify: String,container: NSView) -> NSViewController
{
let storyBoard = NSStoryboard(name: "Main", bundle: nil) as NSStoryboard
let myViewController = storyBoard.instantiateControllerWithIdentifier(identify) as! NSViewController
myViewController.view .setFrameSize(CGSize.init(width: container.frame.width, height: container.frame.height))
return myViewController
}
The problem is when I click on the button or select the table's view in the sub view controller that displayed in container view the app crash with error:
message sent to deallocated instance 0x600000147e80
I got this error by enable zombie objects.
Your crash is likely caused by the fact that the view controller is deallocated almost immediately after you created it.
A view controller holds a reference to its view, but not the other way around. When you add myViewController.view as a subview, it is retained and displayed. In the meantime, myViewController itself is not retained by anything, so it is removed from memory after the variable is last used.
You probably have your view controller as target to button's action. When you tap the button, it wants to send the action to the view controller, but it has been already deallocated, and it crashes.
To solve it, you need to keep the reference to myViewController to keep it from being deallocated earlier than needed. Either just store it in a variable, or consider using child view controllers for that purpose: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/NSViewController_Class/#//apple_ref/doc/uid/TP40005253-CH1-SW34

Game Center share achievement or leaderboard crashes on iPad iOS 8

Hello everyone I am trying to implement Game Center on my iOS8 app and have run into a crash on the iPad. I already have the achievements and leaderboards set up and can successfully hit the blue share button on the iPhone but when i press share on the iPad the GKGameCenterViewController crashes and gives me the following report:
Application Specific Information:
*** Terminating app due to uncaught exception 'NSGenericException', reason: 'UIPopoverPresentationController
(<_UIAlertControllerActionSheetRegularPresentationController:
0x7b1480a0>) should have a non-nil sourceView or barButtonItem set
before the presentation occurs.' terminating with uncaught exception
of type NSException abort() called CoreSimulator 110.4 - Device: iPad
Retina - Runtime: iOS 8.1 (12B411) - DeviceType: iPad Retina
Any help would be appreciated thank you!
I got the same crash. When I present the GKGameCenterViewController modally, it's important I provide the sourceView, using the code in this order:
gameCenterVC.modalPresentationStyle = .Popover
gameCenterVC.popoverPresentationController?.sourceView = view!
gameCenterVC.popoverPresentationController?.sourceRect = view!.frame
presentViewController(gameCenterVC, animated: true, completion: {})
Your error reads
"reason: 'UIPopoverPresentationController [...]"
which suggests your modalPresentationStyle = .Popover.
In that case, you will need to set the source view or button bar, like the rest of the error indicates.
The instance popoverPresentationController in non-nil because...
If you created the view controller but have not yet presented it, accessing this property creates a popover presentation controller when the value in the modalPresentationStyle property is UIModalPresentationPopover
according to popoverPresentationController
I am testing on an iPad Air with iOS 9.3.
I am getting it to run without crashing [using my solution in the above post], but it does not display the game center popups or view controllers with
modalPresentationStyle = .Popover
However, I do get my view controllers and sign in pop ups when I set
modalPresentationStyle = .Fullscreen

Modal segue not working from code

I've already spent the whole day in an issue and did not find a solution yet. I have a UITabBarViewController and I am trying to fire off a modal segue from one of its tabs. I've already created the view I want to segue to, created the segue itself (from the tab's view controller to the target's view controller) and the identifier. In order to test my segue I created a UIButton in my tab and wrote the following code on its action:
[self performSegueWithIdentifier:#"showProductInfo" sender:self];
The modal segue worked fine. But if I try to write the same code to be performed somewhere else (inside a specific method that is called after I receive a web service response) the segue simply does not work. I get the following error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Receiver () has no segue with identifier 'showProductInfo''
This is driving me crazy! Any help is more then welcome.
Editting:
Some additional findings:
If I take off the performSegue from the method I am currently using (which by the way is called by DidFinishLoading) and put it on ViewDidLoad the segue is performed successfully.
Thinking that it could be a thread related issue I included the performSegue inside a dispatch_async(dispatch_get_main_queue()... But the error is the same !
If your destination viewcontroller is inside a navigationcontroller, then take a look at this question:
http://stackoverflow.com/questions/8041237/how-to-set-the-delegate-with-a-storyboard
Furthermore, from the error it looks like the segue you are trying to perform is not attached
to the viewcontroller you are starting from. So,Try to remove the seque and drag it from the
viewcontroller.
The current view controller must have been loaded from a storyboard. If its storyboard property is nil, perhaps because you allocated and initialized the view controller yourself, this method throws an exception.
Make sure you have Storyboard ID for your destination VC (in this example myViewController).
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"myStoryboard" bundle:[NSBundle mainBundle]];
UIViewController *myViewController = [storyboard instantiateViewControllerWithIdentifier:#"MyController"];
[self.navigationController pushViewController:myViewController animated:YES];

prepareForSegue called after viewDidLoad in segue from ViewController to TableViewController

I am building an iOS 5 app with oauth integrated. My storyboard consists of a container NavigationController, with a rootViewController, and two viewControllers segueing from the rootView. When the app is launched, i perform a check to see if an access token is present, and direct the user accordingly.
// rootViewController.m viewDidAppear (i need to perform this check anytime the user is brought to the root view, say for e.g. he logs-out.)
if (accessToken) {
BOOL didAuth = [GTMOAuthViewControllerTouch authorizeFromKeychainForName:#"app name: service" authentication:accessToken
if (didAuth){
//perform segue to main User View (which is a TableViewController)
}
else{
//perform segue to sign-in controller, and direct the user to main view from there.
}
}
I have a couple of questions:
Is such a setup 'valid' as per Apple's Interface Guidelines?
I noticed that the prepareForSegue method in rootViewController gets called after the mainUserView's (which is a TableViewController) viewDidLoad. Is this standard behavior? I understand that this is the case for popovers, but for segues from a standard ViewController to a TableViewController?
Thanks!