Create Singleton of a Viewcontroller in Swift 3

Create singleton of a viewcontroller in swift 3

If you really wanted to have singleton for a view controller corresponding to some scene, you'd probably do something like:

class SecondViewController: UIViewController {

static let shared = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Foo")

}

In this example, the storyboard was Main.storyboard and the storyboard identifier for the scene in question was Foo. Obviously, replace those values for whatever was appropriate in your case.

Then your other view controller that was invoking this could do something like:

@IBAction func didTapButton(_ sender: Any) {
let controller = SecondViewController.shared
show(controller, sender: self)
}

I wouldn't recommend singletons for view controllers. View controllers (and their views) should be created when needed and be allowed to be deallocated when they're dismissed. And you're losing many storyboard benefits (by which you see the logical flow between scenes with segues between them). And, if you use this view controller in different contexts, you're inviting problems stemming from the view controller hierarchy falling out of sync with the view hierarchy. I really would discourage you from using singletons for view controllers.

But if you were going to do it, you could do something like that...

How do I create a singleton view used in more than one view in swift 5 xcode 10

I thought I would post the answer to my own question. I created a header view (RaceSelView) that loads from xib that has several controls in it. Make a UIView in both views where you want the singleton picker to appear. The header view is placed at that location. Whenever the tab is changed, or the controllerviews are loaded, find that view and add the singleton picker as a subview.

protocol HasHeader {
func getHeaderLoc() -> UIView
func reload()
}
class BaseXCView: UIViewController,HasHeader {

override func viewDidAppear(_ animated: Bool) {
setRacePicker() // after layout has happened so that the headerLoc will be positioned correctly
reload()
}
func setRacePicker() {
var hv = globals.raceSelView
if hv == nil {
hv = RaceSelView(frame: getHeaderLoc().frame)
globals.raceSelView = hv
}
view.addSubview(hv!)
}
func reload() {
fatalError("Must Override")
}
func getHeaderLoc() -> UIView {
fatalError("Must Override")
}
}

class XCTabBarController: UITabBarController, UITabBarControllerDelegate {
override func viewDidLoad() {
self.delegate = self
}
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
// this is called whenever the tab changes
if let vc = viewController as? BaseXCView {
vc.setRacePicker()
}
}
}

This seems better than having separate instances of the picker. The alternative would involve copying the state to/from each instance when they become active, and keeping track of which is active in order to know which one to tell to reload/redraw when the underlying data changes.

I am not certain this is the best approach, comments are welcome.

How to make Singleton in Swift

Correct method in singleton

func loadAdReward(parentViewController: UIViewController) {
adReward = VAMP()
adReward.setPlacementId("26812") //test ID
adReward.delegate = self
adReward.setRootViewController(parentViewController)
adReward.load()
}

How to make iOS UIViewController as singleton?

AppDelegate.h
@property (strong, nonatomic) UIViewController *sc;
AppDelegate.m
_sc = nil;
RootViewController.m
- (IBAction)tiaozhuian:(UIButton *)sender {
AppDelegate *appDelegate=[[UIApplication sharedApplication] delegate];
if(appDelegate.sc == nil)
{
appDelegate.sc=[self.storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
[self.navigationController pushViewController:appDelegate.sc animated:YES];
NSLog(@"%@",appDelegate.sc);
}else{
[self.navigationController pushViewController:appDelegate.sc animated:YES];
}
}

//Define a global variable sc, and sc = nil, sc = appDelegate.sc=[self.storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"]when first turn to SecondView , return to RootView and turn to SecondView again whether the sc is nil, and if not,turn to SecondView.

Using the Swift Singleton

There is a lot of info available on singletons in Swift. Have you come across this article with your Google prowess? http://krakendev.io/blog/the-right-way-to-write-a-singleton

But to answer your question, you can simply define anything you'd like to use normally.

class Singleton {
static let sharedInstance = Singleton() // this makes singletons easy in Swift
var stringArray = [String]()

}

let sharedSingleton = Singleton.sharedInstance

sharedSingleton.stringArray.append("blaaaah") // ["blaaaah"]

let anotherReferenceToSharedSingleton = Singleton.sharedInstance

print(anotherReferenceToSharedSingleton.stringArray) // "["blaaaah"]\n"


Related Topics



Leave a reply



Submit