I am attempting to disconnect from a peripheral when ever a user leaves the current view controller. This would normally be easy by using prepare for segue in the following fashion:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
manager.cancelPeripheralConnection(peripheral)
}
This doesnt work, however; because the view controller is inside a container view. Does anybody know how to call this method when ever the container view is change from this view controller to a different one?
You could put it in func viewWillDisappear(_ animated: Bool) or func viewDidDisappear(_ animated: Bool). Both will get called when the user navigates away from the view controller for any reason.
Related
I am using the following code snippet:
let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "secondViewController") as! secondViewController
self.navigationController.pushViewController(secondViewController, animated: true)
I want to execute this code in the viewDidLoad. I will be using something like this in a series of if statements.
I used some code I found here that utilized a label. It did not work. Can a program move from one viewController without using a button? Almost every example, youtube video uses a button. I will be using a button when it is appropriate
You can not push view controllers before your current view controller loads. You should call your code in your viewDidAppear.
If you plan on directing to a particular view when the app launches I suggest you moving your logic to the AppDelegate in the method: didFinishLaunching. This way you don't need to unnecessarily load views.
Example of a ViewController being pushed in the viewDidAppear:
///
/// FUNCTION - VIEW DID APPEAR
///
override func viewDidAppear(_ animated: Bool) {
// GET VIEW CONTROLLER
let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "secondViewController") as! secondViewController
// PUSH VIEW ONTOP OF CURRENT NAVIGATION CONTROLLER
self.navigationController.pushViewController(secondViewController, animated: true)
} // END - FUNCTION
I want to pass a variable from one view controller to another.
First View Controller looks like this and the segue gets triggered correctly and the new view controller gets displayed.
#IBAction func testButton(_ sender: Any) {
let timerVC = TimerViewController()
timerVC.secondsPassed = textfield_seconds.text!
navigationController?.pushViewController(timerVC, animated: true)
}
On the next View Controller (TimerViewController) I declared a variable in the class header
var secondsPassed:String!
and in viewDidLoad I just want to print the value to the console and all I receive is a 'nil'.
I looked through various tutorials but I seem not to find the correct answer for this to get it work.
Anyone around with a clue?
Thanks in advance...
If you already are using a segue, you can remove this line that presents the controller (you don't seem to have a navigationController anyway):
navigationController?.pushViewController(timerVC, animated: true)
Now the correct way to pass data to another VC using segues is this:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc = segue.destination as? TimerViewController {
vc.secondsPassed = textfield_seconds.text!
}
}
I have a Table View Controller as a tab. In it, there's a button that takes you to a Facebook profile:
func didTapFacebook() {
let url = URL(string: "http://www.facebook.com/" + myFacebookId)
if UIApplication.shared.canOpenURL(url!) {
UIApplication.shared.open(url!, options: [:], completionHandler: nil)
}
}
Works fine every time you press the button.
From another tab, there's a button that presents modally a navigation controller with Table View Controller as its root:
func segueToTable(_ sender: UIViewController, tvc: MyTableViewController, completion: #escaping ((_ done: Bool) -> Void)) {
let nc = MyNavigationController(rootViewController: tvc)
sender.present(nc, animated: true, completion: {
completion(true)
})
}
…
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let tvc = storyboard.instantiateViewController(withIdentifier: "TVC") as! MyTableViewController
segueToTable(self, tvc: tvc, completion: { done in
print(“segue complete”)
})
Now once you tap the same button to go to Facebook profile (or Twitter button, basically anything that causes the app to go to background), this warning occurs (there is no keyboard displayed on the screen at this time):
Cannot snapshot view (>) with afterScreenUpdates:NO, because the view is not in a window. Use afterScreenUpdates:YES.
Returning to the app and pressing the button again causes this warning to appear (and every attempt after that):
Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates.
I realize this is related to snapshotting a view that automatically occurs prior to entering the background. It appears for some reason it doesn't like doing that within a view that was presented modally. I've tried a variety of things based on other posts but cannot get the warnings to go away.
Any help is appreciated.
There is the Main Storyboard, from there three other storyboards with Navigation Controller, each one connected to a 4 view controllers so they use the back button to go back to their respective storyboard. Each Storyboard has a Storyboard reference to return to the Main Storyboard. Problem: each time the secondaries storyboards return to the main storyboard appears a Navigation Bar with
I used the following code in viewDidLoad():
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBarHidden = true
}
and also on viewWillAppear:
override func viewWillAppear(animated: Bool) {
self.navigationController?.navigationBarHidden = true
}
I'm having a problem in Swift 3.0 right now.
As attached picture (a), I embedded navigationController into ViewController.swift. ViewController.swift have button to connect to UITableCell MovementStatusVC.swift. And MovementDetailsVC.swift is to display all value from table cell.
The problem that i'm facing right now is when I create button in MovementDetailsVC.swift and perform segue to pass the data inside "EditData" function, Page display twice.
MovementDetailsVC.swift
import UIKit
class MovementDetailsVC: UIViewController {
#IBAction func EditData(_ sender: Any) {
performSegue(withIdentifier: "EditMovementDetails", sender: self)
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
The EditMovementDetailsVC.swift controller display twice when I tap Edit button in MovementDetailsVC.swift as shown as in picture below.
I'm using perform segue and prepare segue in MovementDetailsVC.swift
How to fix it so the view only perform one time ?
Thanks.