I want my app to check at start conditionaly if a variable is correct or not. Based on that it should either go to an intro screen (where he can select a variable in my case select a team) or it should start the main view. After searching i found this code and edited it. But there still seems to be problems. First of all I dont have two identifier. The Intro has one but not the main view. My main View is called WeatherViewController and the Intro screen is called FirstScreenViewController. I also added a picture of my Main.storyboard.
I also googled a lot about conditional UINavigationController but I can only understand with a video and did not found a video about it.
I tried to use the code from here.
var id = hello ? "goToIntro" : "???"
self.window = UIWindow(frame: UIScreen.main.bounds)
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let WeatherViewController: UIViewController = mainStoryboard.FirstScreenViewController(withIdentifier: WVC has no identifier??) as UIViewController
self.window?.rootViewController = WeatherViewController
self.window?.makeKeyAndVisible()
if hello {
self.performSegue(withIdentifier: "goToIntro", sender: self)
} else {
/here nothing should happen. It should open the Main View
self.performSegue(withIdentifier: "???", sender: self)
}
Note: This answer was referring to the original question (before any edits) that was attempting a segue inside loadView().
You're supposed to manually create your controller's view in loadView. You're currently doing nothing, hence the black screen. Furthermore, if you are using a Storyboard or a xib for managing this controller you shouldn't be overriding loadView at all.
Having said that it might be a better idea to move this branching logic a step back, to "something" (a container controller like a UINavigationController or a custom one, or even directly setting the root controller of your window if it makes sense) that will present (or set) A or B based on some condition and thus avoid to load this controller altogether (have in mind that in your code, the controller is initialized, it will be part of the hierarchy and all the lifecycle methods will be called even if you segued directly to another one)
Finally, if you decide for some reason to override loadView you don't have to call viewDidLoad yourself, the system will call this method.
Related
i have a problem with my App.
I´m not an experienced programmer, so maybe it´s just a simple solution. I thought this problem exists because i´m just trying things and play with my app so i made a new App and there´s the same problem.
When i push to a ViewController with navigationController?.pushViewController(PinkViewController(), animated: true).
I´m getting just a black screen. If i have a code like
Label.text = "String in viewdidload of the PinkViewController(). I get the following error at this line of code:
fatal error: Unexpectedly found nil while implicitly unwrapping an
Optional
I searched the web and i didn´t find any solutions for this problem. I hope you can help me.
The reason why it crashes is that your label is probably defined as an #IBOutlet that is connected to a UILabel in your storyboard's PinkViewController. However, when you instantiate PinkViewController with an empty constructor, you're not "using the storyboard-way" and your label outlet (which is non-optional, because it's likely to have an exclamation mark there) could not have been connected to the storyboard instance of your view controller.
So what you should do is one of these options:
If you defined PinkViewController in the storyboard, you'd have to instantiate it in a nib kind of way, for example:
In your Storyboard, select the view controller and then on the right side in the Identity Inspector, provide a Storyboard ID. In code you can then call this to instantiate your view controller and then push it:
let pinkViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "yourIdentifier")
Even better, you create a Segue in the storyboard by control-dragging from the current VC to PinkViewController. There's lots of tutorials for creating Segues online. Don't forget to provide an Identifier for the Segue in the Storyboard, if you want to trigger it programmatically. This can be done by calling
self.performSegue(withIdentifier: "yourIdentifier", sender: nil)
If you want to trigger that navigation upon a button click, you can even drag the Segue from the button to PinkViewController, that way you wouldn't even need to call it in code.
If you defined PinkViewController programmatically only (by creating a class named like that which conforms to UIViewController), you might wanna instantiate it with PinkViewController(nibName: nil, bundle: nil) (notice the arguments instead of an empty constructor) and then push it with your provided code.
Hope that helps, if not, please provide further code / project insight.
I'm using Xcode 8 and storyboards for mac development.
I have 2 storyboards with NSWindowController in each of them.
1). How can I display both windows when the app is launched?
2). How can I display window on pressing button on other window?
Edit:
Code:
func applicationDidFinishLaunching(_ aNotification: Notification)
{
let st = NSStoryboard(name: "Logs", bundle: nil) // Logs is my storyboard name
var logWindow: NSWindowController? = nil
logWindow = st.instantiateInitialController() as! NSWindowController?
if logWindow?.window?.isVisible == false
{
logWindow?.window?.setIsVisible(true)
}
}
Everything is correct but the problem is with logWindow.
I assume, you know that the local variable is dead after the execution of method.
As you can see logWindow is a local variable. When applicationDidFinishLaunching(_ aNotification: Notification) is executing, the variable is alive, a new window is created and also displayed.
When applicationDidFinishLaunching(_ aNotification: Notification) is finished executing, your variable logWindow is not alive. So, the allocated memory is de-allocated automatically (by ARC) and the window is destroyed/de-allocated.
All this is happening so fast and it is the reason you are not seeing the window inspite of isVisible() returning true(it returns true because the method is still in execution and logWindow is still alive).
So just make logWindow a variable of class(in what ever class you are trying to display the window) and you are good to go.
Both questions boil down to pretty much the same thing: How can I display a window from a storyboard? Whether you want to do it when the app is launched or when the user clicks a button shouldn't really affect what you need to do to display the window.
Take a look at the NSStoryboard class and you'll find methods for instantiating a window controller from a storyboard. So, create an instance of NSStoryboard if you don't already have one, and then use that to instantiate the window controller in question. For example, if your window controller is called "My Window Controller" and is located in the main storyboard...
let storyboard = NSStoryboard(name: "Main", bundle: nil)
let windowController = storyboard.instantiateController(withIdentifier: "My Window Controller") as! NSWindowController
Then do whatever you like with windowController. If you want to display two windows when the app is launched, then maybe your app delegate has two properties for keeping track of the corresponding window controllers. In your app delegate's applicationDidFinishLaunching(notification:) method, you could create two windows using code like I've shown and assign the window controllers to those windows. If you want to open a window when the user clicks a button, put code like the code above in the button's action, and store a reference to the resulting window controller in some appropriate object, possibly the window controller where the action appears.
Currently on my viewController : Upload, my button send the data to my database only if all the information are filled out, and I come back to the preview view (table View) with :
self.navigationController?.popViewControllerAnimated(true)
I would like, if it is possible, to come back to my main view on the tabBarController. I tried many things, like directly on the storyboard with Present modally segue to "TabBar controller", but I come back to the TabBar without sending my data to the database and without checking in the information are filled out..
How can I do it?
Thanks!
UITabBarController has a property selectedIndex with which you can switch the selected tab. So on completion after dismissing the UploadViewController you can run:
self.tabBarController?.selectedIndex = 0 // Index to select
It would probably be best to create a delegate for your UploadViewController to fire a function to do all the work in your previewVC on API call completion.
(Super late response...in case someone has similar questions, presumably in later version of Swift, such as mine which is Swift 5, iOS 13.2). Steps:
Be sure to set an id for your UITabBarController storyboard, e.g. "TabBarViewController"
Next, add the following to an action that has already been connected to a button:
let ID_TABBAR = "TabBarViewCOntroller"
#IBAction func returnToTabbar(_ sender: Any) {
let tabBarController = self.storyboard?.instantiateViewController(identifier:ID_TABBAR) as! UITabBarController
self.navigationController?.pushViewController(tabBarController, animated:true)
}
Referenced from one of the responses from this post.
Update: In case your Tab Bar View Controller also happens to be the root view controller, the two lines of the code in the returnToTabbar method above can be:
self.dismiss(animated:true, completion:nil);
self.navigationController?.popViewController(animated:true);
(ref.: See answer here, for Swift4 but works just fine in Swift5)
Here's what I'd like to do: use a SplitViewController but not have it be the first thing that shows up. I want an initialization view (which does some network stuff and then completes) which then punches over to the split view.
I've got this working. BUT not sure if what I'm doing makes sense and it has a weird behavior.
Here's what I think I know: to use a SplitViewController, it must be the rootViewController. Also, working with separate storyboards seemed the only way to get it to work. (I've tried having them all in one storyboard but couldn't get it to work).
My first storyboard's controller does it's init and then, to switch to the "SplitViewBoard" storyboard and launch the split view, does the following:
let mainStrbd = UIStoryboard(name: "SplitViewBoard", bundle: nil)
let splitController = mainStrbd.instantiateInitialViewController() as UIViewController
if splitController is UISplitViewController {
var mySplitController: UISplitViewController = splitController as UISplitViewController
mySplitController.delegate = appDel
appDel.window?.rootViewController = splitController
} else {
debugPrint("badness")
}
Now, this all works fine. With one exception. After setting the rootViewController to the splitController, there's an 8-10 second delay before the split view shows up on the screen! I'm imagining there's some way of telling the delegate (or whatever) to "refresh" or of just being more explicit but haven't found it yet. Is this way reasonable? Or it there a much better way?
Hey guys what does this line of code do?
AddRoleTVC *addRoleTVC = segue.destinationViewController;
I know the first part, with the AddRoleTVC *addRolveTVC, but i don't know what the segue.destinationViewController part does, i have found many different answers I'm just not sure which is the right one. Thanks in advance!
In here the segue.destinationViewController points to the ViewController we are going to navigate to and is of type ‘id’.
Because we know where we are navigating to i.e in this case AddRoleTVC , we can treat segue.destinationViewController as if it were our AddRoleTVC and call methods on it.
That’s how we pass data from one VC to another through the segue by calling set property methods directly on our new ViewController i.e addRoleTVC.
A segue is a transition from one view controller to another. The destinationViewController is "another", the one being transitioned to.
The typical use of this property is to do some extra setup on the destinationViewController before the segue happens. The from VC gets the message - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender. Then it can grab segue.destinationViewController and do extra init, like giving it it's model.