When I click on the newPostButton, I want to segue programmatically to the NewPostVC (which is that view on the right side).
However, the view does not show up on the simulator, as you can see, it's completely black.
What is the issue, why isn't the view showing up?
And how to fix that?
Thank you guys
If I understand correctly you are trying present view controller on your Navigation stack so you need to do two thing set storyborad ID and set push viewController here is code :
Set 1:
set Storyboard ID : NewPostVC to your NewPostVC Storyborad
Step 2:
#IBAction func newPostClicked(_ sender: UIButton) {
let storyborad = UIStoryboard(name: "Main", bundle: nil)
if let vc = storyborad.instantiateViewController(withIdentifier: "NewPostVC") as? NewPostVC {
self.navigationController?.pushViewController(vc, animated: true)
}
}
Related
I have two view controllers, first view controller and the second view controller, when the user segue from the first view controller to the second view controller, the first view controller still visible and sits behind the second view controller. The user can swipe the first view controller and see the entire first view controller and then they got stuck there. I noticed that this only happens after the launch of iOS 13.
performSegue(withIdentifier: "showSecond", sender: self)
Follow these two steps:
Click on the segue you created on the Storyboard
Go to Attributes Inspector
Set the Presentation to "Full Screen"
#Maysam is 100% right! But you can also do it programmatically through this:
// Go to next View Controller
#IBAction func nextTextVC(_ sender: UIButton) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let nextInfo = storyboard.instantiateViewController(identifier: "socialOptionsIntroViewController")
self.navigationController?.pushViewController(nextInfo, animated: true)
}
There is a more detail thread could answer this question.
The follow solution from Pratik Sodha
let controller = UIViewController()
let navigationController = UINavigationController(rootViewController: controller)
navigationController.modalPresentationStyle = .overCurrentContext
self.navigationController?.present(navigationController, animated: true, completion: nil)
This question already has answers here:
Segue to another storyboard?
(6 answers)
Closed 3 years ago.
Here is my code so far
joinButton.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
}
#objc func buttonAction(_sender: UIButton) {
print("I want to go to a storyboard here")
}
I have everything coded (never used storyboard), but now I want to actually manually play with the UI now. How can I manually play with the storyboard? Or even render out to a different viewcontroller? Thanks!
You can do it programatically this way:
Swift 3+
let storyboard = UIStoryboard(name: "StoryboardName", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "ViewControllerID") as UIViewController
present(vc, animated: true, completion: nil)
You need to assign a Storyboard ID to the view controllers you are going to use programatically:
Select storyboard
Go to Identity Inspector (⌥⌘3)
Assign Storyboard ID
If you want to add manual segue in Storyboard (Note: you can have multiple manual segues):
Select storyboard
Select the source view controller
Go to Connections Inspector (⌥⌘6)
In the Triggered Segues, drag the dot beside manual to the target view controller and select the desired method
In Document Outline, select "Show segue..." and go to Attributes Inspector (⌥⌘4) and assign an Identifier
In your button function, you can simply use the segue by:
self.performSegue(withIdentifier: "<IdentifierForSegue>", sender: self)
Or if you do not wish to use a manual segue:
// If view controllers are in the same Storyboard
let storyboard = self.storyboard!
// If view controllers are in different Storyboards
// let storyboard = UIStoryboard.init(name: "<NameOfYourStoryboard>", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "<IdentifierForTargetViewController>")
// If you don't have a navigation controller
self.present(viewController, animated: true, completion: nil)
// If you have a navigation controller
// self.navigationController?.pushViewController(viewController, animated: true)
I dont know how create segue from storyboard 2 to storyboard 3(image: https://i.stack.imgur.com/NLhJY.png). Afterwards user tap save button(storyboard 2), i need save item and open saved item on storyboard 3. I try use Unwind Segue, but this doesnt work.
I try use instantiateViewController. I set on storyboard 3 Storyboard ID("Item") and then add this code to save button:
let viewController = self.storyboard?.instantiateViewController(withIdentifier: "Item") as! ItemViewController
self.present(viewController, animated: false, completion: nil)
But when Storyboard 3 open, after tap Save Button on Storyboard2, navigationbar on Storyboard 3 doesnt showed. Anyone can help find me solution to this problem?
You need to specify which storyboard you want to use exactly along with your ViewController identifier.
Try this:
let storyboard: UIStoryboard = UIStoryboard(name: "YOUR_OTHER_STORYBOARD_NAME", bundle: nil)
let viewController = self.storyboard?.instantiateViewController(withIdentifier: "Item") as! ItemViewController
self.show(viewController, sender: self)
In my app I use side bar as in facebook. when the user slides out the side bar a uiimageview is displayed. when user taps on the image it takes hm to a different viewcontroller. the problem i am facing is that I have created sidebar programatically and the other view to which I want to navigate the user is created using storyboard. So my source view is created programatically and destination view is created using storyboard. So can someone explain me if there is any way of using "Segue" in this scenario. Since i can not create segue using storyboard I need to do it programatically but even after a lot of googling i could not find the answer.
Well, to get another instance of another storyboard programmatically you can use something like:
let newController = UIStoryboard(name: "MyStoryboard", bundle: nil).instantiateViewControllerWithIdentifier("MyIdentifier") as! MyViewController
and then you push to your navigation controller, or add as a child view controller or something...
If you don't wanna bother with identifiers you can just use the instantiateInitialViewController instead of instantiateViewControllerWithIdentifier
Might help
"userSB" is the viewcontroller storyboard identifier
#IBAction func tapSearchCriteria(_ sender: Any?) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let aVC = storyboard.instantiateViewController(withIdentifier: "userSB") as? AViewController
aVC?.modalPresentationStyle = UIModalPresentationStyle.custom
aVC?.transitioningDelegate = self
aVC?.udelegate = self
self.present(aVC!, animated: true, completion: nil)
}
So, I created a login page with basic logic. If username and/or password are empty, it displays an error message. If they're full, then it transitions to the next page (home). I created a second ViewController on the storyboard, and created a SecondViewController". I then defined the respective ViewController class to "SecondViewController".
On the first ViewController.swift file, i used this code:
func transition(Sender:UIButton!)
{
let secondViewController: SecondViewController = SecondViewController()
self.presentViewController(secondViewController, animated:true, completion:nil)
}
When I tested it out however, pressing the login button (when both the username and password are filled) transfers me to a black screen instead of the second View Controller. Any ideas on what to do?
If you want the view to contain things added to it on the storyboard, you could give it a Storyboard ID (a couple of boxes below where you set the class of the view controller in the storyboard).
If you give it for example the ID "secondVC" you can then create it using the following:
let secondViewController = storyboard?.instantiateViewControllerWithIdentifier("secondVC") as? SecondViewController
You can't initialize a view controller with just the default init method.
You should probably create a view controller scene in your storyboard, add a unique identifier on it, instantiate it using instantiateViewControllerWithIdentifier:, and then display it like you are doing.
You'll want to make a segue between the view controllers in your Storyboard, and call self.performSegueWithIdentifier().
Or you can give your `UIViewController an identifier and present it programmatically too:
let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
let secondViewController storyboard.instantiateViewControllerWithIdentifier("") as? SecondViewController
if let secondViewController = secondViewController {
self.presentViewController(secondViewController, animated:true, completion:nil)
}
There are basically three ways to do this
1)Creating segue in storyboard,though it will only work if you have single segue from that item
2)using prepareforSegue and performSegue Methods in your presenting view controller
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "YourIdentifier" {
let controller = segue.destinationViewController as!secondViewController
}
}
Then use this in your IBACtion
performSegueWithIdentifier("YourIdentifier", sender:sender)
3)(Recommended One)
let controller = self.storyboard?.instantiateViewControllerWithIdentifier("ContactsVC")! as! ContactsVC
self.presentViewController(controller, animated:true, completion:nil