I am creating an app in which I am using ARSKView. I am seeing that it is not releasing memory on dismissing ViewController. I am not able to find any issue in my code.
Here is my code for using ARSKView.
class ARViewController: UIViewController, ARSKViewDelegate {
weak var sceneView: ARSKView?
override func viewDidLoad() {
super.viewDidLoad()
sceneView = ARSKView(frame: self.view.frame)
self.view.addSubview(sceneView!)
sceneView?.delegate = self
let scene = CustomScene(size: view.frame.size)
scene.sceneDelegate = self
sceneView?.presentScene(scene)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
activateARView()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
sceneView?.session.pause()
}
var configuration = ARWorldTrackingConfiguration()
func activateARView() {
configuration.worldAlignment = .gravityAndHeading
sceneView?.session.run(configuration)
}
}
The controller is just paused, you can try by setting the controller equal to "nil" just make sure all dependencies are cleared before that.
Related
When I try to change color or tool nothing happens and it still drawing with deafult black pen.
class ViewController: UIViewController {
#IBOutlet weak var canvasView: PKCanvasView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
setupCanvasView()
}
private func setupCanvasView() {
canvasView.drawingPolicy = .anyInput
let toolPicker = PKToolPicker()
toolPicker.setVisible(true, forFirstResponder: canvasView)
toolPicker.addObserver(canvasView)
canvasView.becomeFirstResponder()
}
}
Can't fix this problem. What should I do?
I am learning VIPER. I have successfully processed the data from View->Presnter->Interactor and returning the data from Interactor->Presenter->View. The data is successfully coming back to me and in that extension I am trying to reload the data.
When I try to reload the data, the TableView comes nil. Why is it nil? When I use MVC pattern I do not see the TableView coming nil and everything works fine. Any clue?
Here is my TableView
#IBOutlet weak var matchTable: UITableView!
It is connected to the File Owner.
override func viewDidLoad() {
super.viewDidLoad()
setup()
matchTable.delegate = self
matchTable.dataSource = self
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
presentor?.initiateFetch()
}
And here is the extension to ViewController.
extension MatchListingViewController: PresenterToViewProtocol{
func showMatches(match: MatchDetails?, banner: Banner?) {
matchD = match
bannerD = banner
matchTable.reloadData()
}
func showError() {
print("I am there")
}
}
Here when the function showMatches is called I get the data but when I reload it throws an error that matchTable is nil. Any help?
I think you forgot injection presentor or init protocol. The nil isn't come from tableView, need more code to know exactly which is wrong here.
in VC class:
extension MatchListingViewController: PresenterToViewProtocol
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.presentor?.initProtocol(self)
}
in presentor class:
func initProtocol(_ protocol: PresenterToViewProtocol) {
self.protocol = protocol
self.initiateFetch()
}
then
self.protocol?.showMatches(match: match, banner: banner)
Im using Xcode 8 and swift 3
I created a new project selecting new "Tabbed Application" when i created it. It provided two UIViewControllers embedded in one tab bar controller. I'm trying to pass two variables between the view controllers.
The problem is that the prepare for segue function is never called. I added a print statement in it that never prints. I added the prepare function in both view controllers and can tab back and forth with no prints.
Some code:
import UIKit
import MapKit
import CoreLocation
class FirstViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate{
#IBOutlet weak var mapView: MKMapView!
var locationManager:CLLocationManager?
var currentLocation:CLLocation?
var destPin: MapPin?
var isTracking: Bool? = false
override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
locationManager?.delegate = self
locationManager?.startUpdatingLocation()
locationManager?.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager?.requestAlwaysAuthorization()
updateTracking()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
self.currentLocation = locations[0]
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print(error)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func getMapView()-> MKMapView{
return mapView
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
if(self.isTracking!){
print("Istracking bool is true")
}
else{
print("Istracking bool is false")
}
updateTracking()
locationManager?.stopUpdatingLocation()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
print("Preparing")
let barViewControllers = segue.destination as! UITabBarController
let destinationViewController = barViewControllers.viewControllers![1] as! FirstViewController
destinationViewController.destPin = self.destPin
destinationViewController.isTracking = self.isTracking
}
}
The contents of the prepare function may very well be wrong as well but without the function even being called it hardly matters. The other view controller is also embedded in the tab bar controller and also has a prepare function that does not execute.
If you'd like, its in this github repo
It is easy to do:
The result:
For example, there are FirstViewController and SecondViewController embeded in tabViewController:
In your firstViewController.swift:
import UIKit
class FirstViewController: UIViewController {
var vc1_variable:String = "first vc's variable."
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
In your secondViewController.swift:
import UIKit
class SecondViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let first_vc:FirstViewController = self.tabBarController?.viewControllers![0] as! FirstViewController
print("\(first_vc.vc1_variable)")
}
}
I'm having trouble getting the following code to compile, any suggestions people?
import UIKit
class ViewController: UIViewController, UIScrollViewDelegate {
#IBOutlet weak var avatar: UIImageView!
#IBOutlet weak var scrollView: UIScrollView!
#IBOutlet weak var contentImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
avatar.layer.cornerRadius = 5.0
avatar.layer.borderWidth = 4.0
avatar.layer.borderColor = UIColor.whiteColor().CGColor
avatar.clipsToBounds = true
scrollView.delegate = self
contentImageView.clipsToBounds = true
}
override func viewDidAppear(animated: Bool) {
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("LoginScreen") as! UIViewController
self.presentViewController(vc, animated: true, completion: nil)
}
func scrollViewDidScroll(scrollView: UIScrollView) {
let yOffset = self.scrollView.contentOffset.y * 0.2
let availableOffset = min(yOffset, 60)
let contentRectYOffset = availableOffset / contentImageView.frame.size.height
contentImageView.layer.contentsRect = CGRectMake(0.0, contentRectYOffset, 1, 1);
}
}
The error is
'Downcast from 'UIViewController?' to 'UIViewController' only unwraps
optionals; did you mean to use '!'
This happens in the line
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("LoginScreen") as! UIViewController
You are force down casting an object which is already of same type. You just need to remove as! UIViewController and assuming the ViewController with identifier LoginScreen exists, you can force unwrap the vc variable when needed.
override func viewDidAppear(animated: Bool) {
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("LoginScreen")
self.presentViewController(vc!, animated: true, completion: nil)
}
Thanks R P, your solution worked a treat.
override func viewDidAppear(animated: Bool) {
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("LoginScreen")
self.presentViewController(vc!, animated: true, completion: nil)
}
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
var webView : WKWebView!
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
self.view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL(string: "http://boooo.com")
webView.loadRequest(NSURLRequest(URL: url!))
webView.allowsBackForwardNavigationGestures = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Everything works fine but allowsBackForwardNavigationGestures = true not working in xcode 7.1. can't go back and forward in wkwebview.
Look like it's interfering with navigationController's interactivePopGestureRecognizer (if you have one).
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true
}
So I just disabled this property at the time of using WKWebView in navigation stack!