Presenting UIViewController from programmed button issues - ios

I have a UIScrollView, with a button I created in my UIViewController that presents another view controller, but when I run it, it gives me an error that it cannot find storyboard name "AddVC" though my VC is named in the storyboard id "AddVC" I do have some pictures, so please take a look at it.
here is the compiler:
here is the code: Button on top with action, with also part of a button on the bottom, ignore middle (sorry for large photo):
And the storyboard:
Please look at the images to help tell me what I am messing up, it is probably a simple fix.
Thanks - Randy

It looks like you have give wrong name of your storyboard. Replace the name with your actual storyboard name:
let vc = UIStoryboard(name:"Main", bundle: nil).instantiateViewController(withIdentifier:"AddVC")

Actually for storyboard name you have given storyboard ID.
Try giving
let VC = UIStoryboard(name:<storyboard name>, bundle: nil).instantiateViewController(withIdentifier:"AddVC")

Related

How to navigate from one storyboard to another TabBarController?

I'm trying to create an onboarding page for my Application which view is like this :
The Not Now Button supposed to navigate from this Onboarding Storyboard to ForYou Storyboard which have a storyboard that embed in navigation controller and tab bar controller.
Based on my research, i can use this line of code to make it happened
let barViewControllers = UIStoryboardSegue.init(identifier: "ForYou", source: OnboardingMasterViewController(), destination: ForYouViewController()) as! UITabBarController
let nav = barViewControllers.viewControllers![0] as! UINavigationController
let destinationViewController = nav.topViewController as! ForYouViewController
It build successfully but when i run it on simulator, the apps crash and show this line of error that i never see or learn before. The error is Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
I tried to look to google but either i used a wrong keyword or my lack of understanding, i still can't solved this error. Hope one of you can give me a guidance in finding the solution.
Thank you.
I am guessing you have already given an identifier to the Segue that I can see on the Onboarding Storyboard to ForYou Storyboard.
If not, click on that Segue in your storyboard and give its identifier.
Now, on the Not Now button's action you can do the following instead of the code that you have
performSegue(withIdentifier: "your-segue-identifier", sender: nil)
OR
You can directly connect the Segue in the storyboard from your Not Now button to ForYou storyboard, that way you won't need to write any code to achieve this.

Crash when trying to push view controller in Swift

I am having a weird issue where my app crashes when I am trying to push a new view controller. I have set up a swipe gesture and want to segue to another view controller when a swipe is detected. When I run these 2 lines of code ...
let viewController:ViewController = ViewController()
self.navigationController?.pushViewController(viewController, animated: true)
The app crashes not specifically on either of those lines of code but rather in my ViewController class when in my viewDidLoad method I run this piece of code...
imageView.layer.masksToBounds = true
If I comment that out it crashes when I set the auto-correction type of my textField. What am I doing wrong?
First place I look when the view immediately crashes is in the Outlets for that ViewController in InterfaceBuilder. I look for anything that shows up with an exclamation mark. That usually means I renamed an outlet or broke a connection somehow. Delete anything broken by pressing the little x by the item that's messed up. I'll attach a photo so you can see.
It seems you're loading a ViewController that exists in storyboard with
let viewController:ViewController = ViewController()
which will result in nil outlets , so you have to use
let viewController = storyboard.instantiateViewController(withIdentifier: "VCID") as! ViewController
and give that vc a storyboard identifier like VCID
I think I have solved the issue but it has a weird side effect. Instead of using the line of code in #Sh_Khan's answer, I used ...
let viewController = nav?.storyboard!.instantiateViewController(withIdentifier: "mainVC") as! ViewController
The variable nav is equal to the navigation controller of the current view-controller. This seems to work without any hiccups but for some reason the back button does not disappear from the navigation controller after the segue is preformed. Does anybody know a solution to this, if so leave a comment and I will update my answer.
EDIT:
Another issue is that it wipes everything changed on that ViewController by the user clear. Is there another way to instantiate a ViewController without clearing it?
Follow what #Sh_Khan has said and in addition to that make sure that the view that you are making the push segue from is embedded in a Navigation controller.

Swift - Elements missing when programmatically instantiating a view controller

First of all, I'm brand new to swift and iOS dev! I've decided to get started with it, and so far so good. Except for today.
So I first created a single view app, following the tuto, then I decided to see what happens if a added another view. Using segue to navigate between views seemed to work fine, so I wanted to see how I could navigate programmatically, having in mind that the "destination view" may be conditional on more than just a user pressing a button. So I added a first view to become my initial view controller, and I've found the following code online to transition to my other view:
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("home")
self.presentViewController(nextViewController, animated:false, completion:nil)
There was no problem, no error or console message, except for one little problem: some UI elements of the "destination view" (nextViewController in my case) are missing when the view loads! And some of them (but not all) show up after I click on some other elements of the view.. Note that there was/is no problem if I set the nextViewController as the initial one.
I haven't been able to find any clue on the internet about that problem, and I would greatly appreciate it if someone could point me in the right direction..
Oh well, 5 more minutes and finally an error message were enough to fix that problem!
I've found my solution on the following thread:
whose view is not in the window hierarchy
As it's said, moving my code from the viewDidLoad method to the viewDidAppear one fixed the issue. And I had the same error as the OP mentioned.

Using Storyboard Reference

How to use Storyboard reference to connect 2 storyboards? Is it possible to connect storyboard with xib file? I want to use it via Navigation Controller.
if you want to reference two storyboards in Xcode 7, in your initial storyboard select the view controllers you want to use in the second storyboard, in the task bar select "Editor" and then "Refactor to Storyboard", then you have to name the new storyboard and thats all, i donĀ“t know if you can reference to an .xib file, notice that you can use the storyboard reference to call a view even in the same storyboard or even a view that is not connected to any other view, and in the reference you can specify which view you want to load (you do this in the identity inspector), or leave that space empty and the view loaded will be the initial view.
Better explained step by step:
If you want to create the new storyboard at the same time
select the views you want to move to the new storyboard
open the Editor's menu and select Refactor to Storyboard
Xcode will ask you the name you want to give to the new storyboard
and thats all
If you have already created the storyboard
Open the storyboard where you want to call the second storyboard
drag the Storyboard Reference that is located in the objects library
select the reference you added and open the identity inspector
in the identity inspector write the name of the storyboard that you want
to call (required) and the storyboardID of the view you want to load
(optional)
thats how you use storyboard references in Xcode 7, hope that is useful to you
You can load another storyboard simply like this your main storyboard..just replace the name of storyboard and viewcontroller
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"NewStoryboard" bundle: nil];
SomeViewController *someViewController = [storyboard instantiateViewControllerWithIdentifier:#"SomeViewController"];
[self presentViewController:someViewController animated:YES completion:nil];
or Swift
let storyboard = UIStoryboard(name: "NewStoryboard", bundle: nil)
let someViewController = storyboard.instantiateViewControllerWithIdentifier("SomeViewController") as! UIViewController
self.presentViewController(someViewController, animated: true, completion: nil)
Hi Friend this is really easy,
Just create a new Storyboard with file>newfile and get the new Storyboard from User Interface don't forget to name it properly ;)
Drag a Storyboard reference to your main story board and now connect it with button or navigation bar button or tab bar whatever (as per your requirement)and thats it...
Select Storyboard reference and go to Attribute Inspector you can see the dropdown of story board from that select your new Storyboard.
Create any new view controller in the empty Storyboard and make it initial view controller.
Run the code
This is how you do it my friend hope it helps :)
Tell me if you are not able to solve will update my answers with screen shots.

Performing a Segue from a UIView programmatically

I'm trying to create a custom UIView that will, when tapped, segue to a new view controller. I'd like to do this programmatically rather than in Interface Builder. How do I get a reference to the destination view controller when I'm doing this? I assume I don't just create one and set it as the next view or something, so where does the reference come from? Since when it's done in the storyboard you simply choose where you want the segue to have as the destination, it's not an issue there, however programmatically I don't understand where that reference comes from.
I'm doing this in Swift as well. I don't think that really makes a big difference, but it might change something.
I tried using a UIView with a tapGestureRecognizer added, which then performed the segue when tapped, however I was getting an unrecognized selector error.
If you declared your UIViewController in storyboard you have to set its Storyboard Id, and then, instead of performing segue you can use something like this (didn't run it, but done exact the same thing in obj-c, so I think it will do well):
let storyboard : UIStoryboard = UIStoryboard(name: "myStoryboardName", bundle: nil);
let vc : MyUIViewController = storyboard.instantiateViewControllerWithIdentifier("myVCStoryboardID") as MyUIViewController;
self.presentViewController(vc, animated: true, completion: nil);

Resources