Are there reasons to have global variables for storyboards in iOS
up vote
1
down vote
favorite
While looking at the code of various iOS apps posted online, I noticed 2 distinct patterns:
Create a storyboard instance where you need it, instantiate view, don't preserve either. E.g.:
func switchView() {
let storyboard = UIStoryboard(name: "Storyboard1", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "ViewController1") as! ViewController1
self.navigationController?.pushViewController(viewController, animated: true )
}
On opposite end are cases when people instantiate storyboard(s) early (e.g. as constants in AppDelegate) and refer to those global instances when they need a storyboard. Sometimes even some views are referred that way. E.g.:
class AppDelegate: UIResponder, UIApplicationDelegate {
static var shared: AppDelegate!
let storyboard1 = UIStoryboard(name: "Storyboard1", bundle: nil)
let storyboard2 = UIStoryboard(name: "Storyboard2", bundle: nil)
// etc
}
and then from elsewhere
let viewController = AppDelegate.shared.storyboard1.instantiateViewController(withIdentifier: "ViewController1")
It appears that they do it mostly for 3 reasons:
- They think time is saved on loading storyboards every time, especially for larger storyboards
- Potentially memory is saved since app doesn't need to hold multiple copies of the same storyboard in memory
- They guarantee that obvious dev mistakes (e.g. wrong storyboard name) are caught early, and are not made in multiple places
The last one is sort of true, but same could be achieved by using constants for storyboard names. And I could not find any indication that load time / memory claims are actually true. So, other than personal subjective preference, what are the objective advantages of this second approach over the first?
ios swift
add a comment |
up vote
1
down vote
favorite
While looking at the code of various iOS apps posted online, I noticed 2 distinct patterns:
Create a storyboard instance where you need it, instantiate view, don't preserve either. E.g.:
func switchView() {
let storyboard = UIStoryboard(name: "Storyboard1", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "ViewController1") as! ViewController1
self.navigationController?.pushViewController(viewController, animated: true )
}
On opposite end are cases when people instantiate storyboard(s) early (e.g. as constants in AppDelegate) and refer to those global instances when they need a storyboard. Sometimes even some views are referred that way. E.g.:
class AppDelegate: UIResponder, UIApplicationDelegate {
static var shared: AppDelegate!
let storyboard1 = UIStoryboard(name: "Storyboard1", bundle: nil)
let storyboard2 = UIStoryboard(name: "Storyboard2", bundle: nil)
// etc
}
and then from elsewhere
let viewController = AppDelegate.shared.storyboard1.instantiateViewController(withIdentifier: "ViewController1")
It appears that they do it mostly for 3 reasons:
- They think time is saved on loading storyboards every time, especially for larger storyboards
- Potentially memory is saved since app doesn't need to hold multiple copies of the same storyboard in memory
- They guarantee that obvious dev mistakes (e.g. wrong storyboard name) are caught early, and are not made in multiple places
The last one is sort of true, but same could be achieved by using constants for storyboard names. And I could not find any indication that load time / memory claims are actually true. So, other than personal subjective preference, what are the objective advantages of this second approach over the first?
ios swift
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
While looking at the code of various iOS apps posted online, I noticed 2 distinct patterns:
Create a storyboard instance where you need it, instantiate view, don't preserve either. E.g.:
func switchView() {
let storyboard = UIStoryboard(name: "Storyboard1", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "ViewController1") as! ViewController1
self.navigationController?.pushViewController(viewController, animated: true )
}
On opposite end are cases when people instantiate storyboard(s) early (e.g. as constants in AppDelegate) and refer to those global instances when they need a storyboard. Sometimes even some views are referred that way. E.g.:
class AppDelegate: UIResponder, UIApplicationDelegate {
static var shared: AppDelegate!
let storyboard1 = UIStoryboard(name: "Storyboard1", bundle: nil)
let storyboard2 = UIStoryboard(name: "Storyboard2", bundle: nil)
// etc
}
and then from elsewhere
let viewController = AppDelegate.shared.storyboard1.instantiateViewController(withIdentifier: "ViewController1")
It appears that they do it mostly for 3 reasons:
- They think time is saved on loading storyboards every time, especially for larger storyboards
- Potentially memory is saved since app doesn't need to hold multiple copies of the same storyboard in memory
- They guarantee that obvious dev mistakes (e.g. wrong storyboard name) are caught early, and are not made in multiple places
The last one is sort of true, but same could be achieved by using constants for storyboard names. And I could not find any indication that load time / memory claims are actually true. So, other than personal subjective preference, what are the objective advantages of this second approach over the first?
ios swift
While looking at the code of various iOS apps posted online, I noticed 2 distinct patterns:
Create a storyboard instance where you need it, instantiate view, don't preserve either. E.g.:
func switchView() {
let storyboard = UIStoryboard(name: "Storyboard1", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "ViewController1") as! ViewController1
self.navigationController?.pushViewController(viewController, animated: true )
}
On opposite end are cases when people instantiate storyboard(s) early (e.g. as constants in AppDelegate) and refer to those global instances when they need a storyboard. Sometimes even some views are referred that way. E.g.:
class AppDelegate: UIResponder, UIApplicationDelegate {
static var shared: AppDelegate!
let storyboard1 = UIStoryboard(name: "Storyboard1", bundle: nil)
let storyboard2 = UIStoryboard(name: "Storyboard2", bundle: nil)
// etc
}
and then from elsewhere
let viewController = AppDelegate.shared.storyboard1.instantiateViewController(withIdentifier: "ViewController1")
It appears that they do it mostly for 3 reasons:
- They think time is saved on loading storyboards every time, especially for larger storyboards
- Potentially memory is saved since app doesn't need to hold multiple copies of the same storyboard in memory
- They guarantee that obvious dev mistakes (e.g. wrong storyboard name) are caught early, and are not made in multiple places
The last one is sort of true, but same could be achieved by using constants for storyboard names. And I could not find any indication that load time / memory claims are actually true. So, other than personal subjective preference, what are the objective advantages of this second approach over the first?
ios swift
ios swift
asked 2 days ago
Kiril S.
5,33152041
5,33152041
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402740%2fare-there-reasons-to-have-global-variables-for-storyboards-in-ios%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown