Get attribute of viewcontroller created with storyboard
Right after instantiating the controller the outlets are not connected yet, you have to declare temporary variables and set the outlet properties in viewDidLoad()
...
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "NewsView") as! SimpleNewsViewController
controller.tempImage = UIImage(named: "image.jpg")
controller.tempLabel = "this is an example that can be really long"
controller.tempText = "this is a title example
self.navigationController?.pushViewController(controller, animated: true)
...
class SimpleNewsViewController: UIViewController {
@IBOutlet weak var myImage: UIImageView!
@IBOutlet weak var myLabel: UILabel!
@IBOutlet weak var myText: UITextView!
var tempImage : UIImage?
var tempLabel = ""
var tempText = ""
var event: Events!
override func viewDidLoad() {
super.viewDidLoad()
myImage.image = tempImage
myLabel.text = tempLabel
tempText.text = tempText
}
How to reference a ViewController in storyboard with PresentViewController
You have to create instance of controller that you want to present:
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil];
UIViewController *controller = [mainStoryboard instantiateViewControllerWithIdentifier: @"<Controller ID>"];
This will create controller with preconfigured views from storyboard.
@"Controller ID" == "Storyboard ID" attribute in the Interface Builder. (Inspector tab when controller is selected)
And present it as you want.
[self presentViewController:controller animated:YES completion:nil];
Instantiate and Present a viewController in Swift
This answer was last revised for Swift 5.4 and iOS 14.5 SDK.
It's all a matter of new syntax and slightly revised APIs. The underlying functionality of UIKit hasn't changed. This is true for a vast majority of iOS SDK frameworks.
let storyboard = UIStoryboard(name: "myStoryboardName", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "myVCID")
self.present(vc, animated: true)
Make sure to set myVCID
inside the storyboard, under "Storyboard ID."
How do I get a Storyboard-created UIViewController without using seagues into a UIPopover?
Call this method on UIStoryboard:
+ (UIStoryboard *)storyboardWithName:(NSString *)name bundle:(NSBundle *)storyboardBundleOrNil
probably like this if your view controller live in 'MainStoryboard.storyboard':
UIViewController *viewControllerForPopover =
[[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]instantiateViewControllerWithIdentifier:@"viewForPopover"];
How to manually add a root view controller arrow after new a storyboard?
Do you mean how can you designate a view controller as the storyboard's root view controller? Select the view controller, open the Attributes Inspector, and select Is Initial View Controller
.
Using Storyboard and custom View Controller init
View controllers in storyboards are always initialised using
init?(coder aDecoder: NSCoder)
there's no way around that.
An alternative would be to have…
class DummyVC: UIViewController {
private var dummyManager: DummyManager!
func configure(dummyManager: DummyManager) {
self.dummyManager = dummyManager
}
}
and then…
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let dummyVC = mainStoryboard.instantiateViewControllerWithIdentifier("DummyVC") as! DummyVC
dummyVC.configure(dummyManager: DummyManager())
or
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
switch segue.destination {
case let dummyVC as DummyVC:
dummyVC.configure(dummyManager: DummyManager())
default:
break
}
}
Whilst not perfect (using let
rather than var
) the property being private
and an implicitly unwrapped optional means it must be set (or the app will crash on use), and that can only happen from within the containing class.
I've adopted this throughout my apps, and find it quite a nice way to ensure all properties are set. Just remember to update the configure
func when a property is added to a class.
UIViewController Title attribute in Storyboard
You can set the title of the UINavigationBar
in Storyboard by double clicking the actual navigationBar and typing in a title right there. This only sets the title for the UINavigationBar
.
Setting the title in code offers some different possibilities.
self.title = @"Your title";
will set the title of a navigationBar and also cause the title to cascade down to a UITabBarItem, if present.
self.navigationItem.title = @"Your title";
will only set the title of the navigationBar, assuming a UINavigationController is present, and NOT affect a UITabBarItem.
self.navigationController.title = @"Your title";
will set the title of a UITabBarItem but NOT the UINavigationBar.
Related Topics
What Privacy-Violating or Device-Changing Things How to Do on an Iphone
Opening a PDF Document When Clicking a Button
Uicollectionview Horizontal Paging - How to Use Flow Layout
iOS 8 - Screen Blank After Dismissing View Controller with Custom Presentation
Logging Method Signature Using Swift
How to Use Cocoapods with Playground
How to Custom Modal View Controller Presenting Animation
How to Detect If Code Is Running in Main App or App Extension Target
Custom Url Scheme for New Facebook iOS App
iOS - Detect Blow into Mic and Convert the Results! (Swift)
iOS Simulator Display Scaled Incorrectly
Change Button Image When Play Completes Swift
How to Deselect a Selected Uitableview Cell
How to Ask User for Camera Access After They Have Already Denied It on iOS
Adding Background Image to Uilabel
How to Make Sure API Requests Come from Our Mobile (Ios/Android) App