I would like to change between two View Controllers. Both of the ViewControllers inherit from CenterViewController. The code below is to add one ViewController (CertificatenViewController) to the stack:
centerViewController = CertificatenViewController(nibName: "CertificatenViewController", bundle: nil)
centerViewController.delegate = self
centerNavigationController = UINavigationController(rootViewController: centerViewController)
view.addSubview(centerNavigationController.view)
addChildViewController(centerNavigationController)
centerNavigationController.didMoveToParentViewController(self)
This happens in the viewDidLoad and in the same class I have a switch statement to check which View has to be loaded, something like this:
switch menuItem.getNibname() {
case "CertificatenViewController":
print(menuItem.getNibname())
centerViewController = CertificatenViewController(nibName: menuItem.getNibname(), bundle: nil)
centerViewController.delegate = self
case "SettingsViewController":
print(menuItem.getNibname())
centerViewController = SettingsViewController(nibName: menuItem.getNibname(), bundle: nil)
centerViewController.delegate = self
default: break
}
I don't know exactly what the problem is, but it seems to be that the view is not reloaded. I have tried popViewControllerAnimated but this is not working. Maybe you can help me solving this problem.
Solution:
func changeView(menuItem: MenuItem){
self.centerNavigationController.viewControllers.removeAll()
switch menuItem.getNibname() {
case "CertificatenViewController":
print(menuItem.getNibname())
self.centerNavigationController.pushViewController(self.centerViewController, animated: false)
case "SettingsViewController":
print(menuItem.getNibname())
self.centerNavigationController.pushViewController(self.settingsViewController, animated: false)
case "MessagesViewController":
print(menuItem.getNibname())
self.centerNavigationController.pushViewController(self.messagesViewController, animated: false)
default: break
}
}
Generally, this is NOT the way to present view controllers:
view.addSubview(centerNavigationController.view)
addChildViewController(centerNavigationController)
centerNavigationController.didMoveToParentViewController(self)
Just remove that code.
I am not sure what exactly you are trying to do, but something like
self.presentViewController(centerNavigationController, animated: true, complettion: nil)
is typical. This makes the current view controller show the navigation controller (usually just on top of itself). Then the nav-controller will show the root you set up inside of itself (so the centerViewController).
When you want to get rid of it, you call dismissViewControllerAnimated:completion: on the view controller instance that did the presenting. It dismisses the navigation controller (do not call dismiss on the navigation controller)
Later, when you want to replace, where you do this:
centerViewController = CertificatenViewController(nibName: menuItem.getNibname(), bundle: nil)
centerViewController.delegate = self
Add:
centerNavigationController.setViewControllers([centerViewController], animated: true)
Related
I have a UINavigationController which includes 4 screens. Each has a back button respectively. Task: by pressing the back button on Controller4 get to Controller2, which will immediately switch to Controller1. I want to do this through a delegate (connection of 2 and 4 controllers), but I don’t understand how I can access the ViewController4.delegate = self property in Controller2. There is already an action for clicking and switching to 3Controller, to do something like
let storyboard = UIStoryboard(name: "VC4", bundle: nil)
guard let vc4 = story.instantiateViewController(withIdentifier: "VC4") as? VC4 else { return }
vc4.delegate = self
}
Please, help. How to link two controllers that are in completely different places in the project through a delegate (or something else)
P.S. This is my first time asking a question, sorry in advance if it's crooked
Example
protocol Controller2Delegate: AnyObject {
func showVC1()
}
class Controller2: UIViewController, Controller2Delegate {
// need to connect between 2 and 4 controller
let story = UIStoryboard(name: "VC4", bundle: nil)
guard let vc4 = story.instantiateViewController(withIdentifier: "VC4") as? VC4 else { return }
vc4.delegate = self //this is not work, and i already have same method for push to VC3
func showVC1() {
//any logic to call VC1 from VC2
//break point never called
}
class Controller4: UIViewController {
weak var delegate: Controller2Delegate?
func GoToVC1() {
//method who's call logic for pop to VC2
delegate?.showVC1
}// breakpoint is called
}
I have 3 view controllers say main1, main2 and child. I have added a menu item, on click of that it should open child view controller as modal.
Whenever user is in main1 VC, menu item should be enabled. If user in main2 VC, menu should be disabled. Right now I’ve added modal segue between menu item and child VC.
I followed following approaches to disable, but they are not working.
Method 1:
In main2 VC, I’ve added
func validateUserInterfaceItem(_ anItem: NSValidatedUserInterfaceItem) -> Bool {
return false
}
override func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
return false
}
Method 2:
override func viewDidLoad() {
super.viewDidLoad()
let mainMenu = NSApplication.shared().mainMenu!
let appMenu = mainMenu.item(at: 0)!.submenu
appMenu?.item(withTitle: someMenuTitle)?.isEnabled = false
}
If you use a modal segue it will be always activated.
To enable/disable dependent on the presented view controller I would add an action to the view controller to open the view controller manualy as modal. The menu item has to be connected to the action (openModalViewController) with the first responder.
#IBAction func openModalViewController(_ sender: AnyObject) {
let storyboard = NSStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateController(withIdentifier: "MyViewController") as! NSViewController
presentAsModalWindow(viewController)
}
Consider there must be at least one view able to get the first responder in main1/main2 that the menu item will activate. If this is not the case you would have to implement acceptsFirstResponder for the corresponding view.
override var acceptsFirstResponder: Bool{
return true
}
To implement validateUserInterfaceItem would be not required in this case, only if you want to control activation/deactivation dependent on an additional state as in the example below.
extension ViewController: NSMenuItemValidation {
func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
if menuItem.action == #selector(delete(_:)) {
return tableView.selectedRow < 0 ? false : true
}
return true
}
}
I'm developing an OSX app where I show first a login/register window if the user hasn't logged in yet.
After login success I show my main view controller.
If the user is already logged in (a token is stored), then the app has to launch directly with the main view controller.
I'm new to OSX development, I googled for this kind of scenario but couldn't find anything.
So I went up with what I think should work. It works sometimes, sometimes I get a blank window.
In the storyboard I let the Main Menu and the Window Controller. I removed the "contains" segue to my main view controller.
In AppDelegate, I put this:
func applicationDidFinishLaunching(aNotification: NSNotification) {
if loggedIn {
self.showViewController(NSStoryboard.mainViewController())
} else {
let loginController = NSStoryboard.loginViewController()
loginController.delegate = self
self.showViewController(loginController)
}
}
private func showViewController(viewController: NSViewController) {
if let mainWindow = NSApplication.sharedApplication().mainWindow {
mainWindow.contentViewController = viewController
} else {
print("Error: No main window!")
}
}
About half of the times the window is empty and I see in the console "Error: No main window!". I thought maybe I can use applicationDidBecomeActive but this is called basically when it comes to the foreground and this is not what I need.
Further, the times when it works, and I log in, then I want to show the main view controller:
func onLoginSuccess() {
self.showViewController(NSStoryboard.mainViewController())
}
And here I also get "Error: No main window!" (always) and nothing happens.
The docs say following about mainWindow being nil:
The value in this property is nil when the app’s storyboard or nib file has not yet finished loading. It might also be nil when the app is inactive or hidden.
But why is the storyboard not finished loading or the app inactive when I'm launching it? And on login success the app is definitely active and in the foreground and the main window is always nil.
What am I doing wrong? How can I implement this workflow? Alternatively I could create a "parent" view controller, have that one connected to the window in the storyboard, and add the login or main view controller as nested view controllers to that. But don't really like having to add a do nothing view controller.
I'm using XCode 7(beta 4), Swift 2, OSX 10.10.4
Edit:
The NSStoryboard methods come from an extension, it looks like this:
extension NSStoryboard {
private class func mainStoryboard() -> NSStoryboard { return NSStoryboard(name: "Main", bundle: NSBundle.mainBundle()) }
private class func signupStoryboard() -> NSStoryboard { return NSStoryboard(name: "LoginRegister", bundle: NSBundle.mainBundle()) }
class func mainViewController() -> ViewController {
return self.mainStoryboard().instantiateControllerWithIdentifier("MainViewController") as! ViewController
}
class func loginViewController() -> LoginViewController {
return self.signupStoryboard().instantiateControllerWithIdentifier("LoginViewController") as! LoginViewController
}
class func registerViewController() -> RegisterViewController {
return self.signupStoryboard().instantiateControllerWithIdentifier("RegisterViewController") as! RegisterViewController
}
}
To put the solution we found in the comments as an answer:
Apparently NSApplication.sharedApplication().mainWindow is a different window than my main window in the storyboard.
So, I created an NSWindowController subclass and assigned it to the window in the storyboard, using the identity inspector.
Then I moved the logic I had in app delegate to this NSWindowController. It looks like this:
class MainWindowController: NSWindowController, LoginDelegate {
override func windowDidLoad() {
if loggedIn {
self.onLoggedIn()
} else {
let loginController = NSStoryboard.loginViewController()
loginController.delegate = self
self.contentViewController = loginController
}
}
func onLoggedIn() {
self.contentViewController = NSStoryboard.mainViewController()
}
func onLoginSuccess() {
self.onLoggedIn()
}
}
* Thanks Lucas Derraugh for pointing me in the right direction!
enum Storyboards: String {
case main = "Main"
case settings = "Settings"
func instantiateVC<T>(_ identifier: T.Type) -> T? {
let storyboard = NSStoryboard(name: rawValue, bundle: nil)
guard let viewcontroller = storyboard.instantiateController(withIdentifier: String(describing: identifier)) as? T else { return nil}
return viewcontroller
}
}
//Need to use like this
//Make sure Storyboard Id and class-name are the same
if let windowController = Storyboards.main.instantiateVC(IDMainController.self) {
windowController.showWindow(nil)
//----- OR -----
self.contentViewController = windowController
} else {
print("Cannot find IDMainController")
}
I've created a simple NSViewController and want to add a split view with just one child view. The split view should be controlled by a NSSplitViewController, because I'd like to use the NSSplitItem's facilities for collapsing/expanding split items. After adding a child view controller, the split item is created, but no child view is added to the view tree.
override func viewDidLoad() {
super.viewDidLoad()
let splitViewController = NSSplitViewController()
view.addSubview(splitViewController.splitView)
let myController = MyController(nibName: "MyController", bundle: nil)
splitViewController.addChildViewController(myController)
printTree(view)
}
func printTree(view: AnyObject, _ n: Int = 1) {
if let view = view as? NSView {
NSLog("\(n): \(view)")
for child in view.subviews {
printTree(child, n + 1)
}
}
}
Output:
1: <NSView: 0x618000120140>
2: <NSSplitView: 0x6180001205a0>
Why does the split view have no child view?
To compare, here's the version without split view:
override func viewDidLoad() {
super.viewDidLoad()
let myController = MyController(nibName: "MyController", bundle: nil)
view.addSubview(myController.view)
printTree(view)
}
Output:
1: <NSView: 0x6100001203c0>
2: <NSView: 0x6000001208c0> <-- here's my child view
3: <NSButton: 0x600000140580>
And adding the child view directly as a subview to the split view doesn't work either:
A SplitView managed by a SplitViewController cannot have its subviews modified
So, my question is, why is the child view not added to the view tree inside the split view?
"You're doing it wrong"
You're using base class methods when NSSplitViewController has a very particular API.
See: https://developer.apple.com/library/prerelease/mac/samplecode/Exhibition/Listings/Exhibition_GalleryWindowController_swift.html for an example.
You want the addSplitViewItem: method.
I figured it out. My mistake was that I added the splitView instead of the view:
// this won't work:
self.view.addSubview(splitViewController.splitView)
// this will work:
self.view.addSubview(splitViewController.view)
BTW: using splitViewController.addChildViewController(myController) as I did before is just a shorter way of saying the following:
let item = NSSplitViewItem(viewController: myController)
splitViewController.addSplitViewItem(item)
which didn't work for me because of my mistake described above.
I have some vars in my Main VC and when user clicks a button in another VC the prepareForSegue passes along a new value to the Main VC and updates a label.
But when the user clicks again it's back to initial value, so it doesn't increment since the value is set back in the viewDidLoad?
MainVC:
var statsHealth:Int = 0
override func viewDidLoad() {
super.viewDidLoad()
healthLabel.text = String("Health: \(statsHealth)/10")
}
Another VC:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "startSegue") {
let startVC = segue.destinationViewController as ViewController
startVC.statsHealth += 1
}
It's displayed as 0, then 1 but then 0 again and then 1 instead of 2,3,4 etc.
Any ideas?
BR
Nils
Perhaps not the most 'Swift' way to do it, but certainly works well....
Create a file called Variables.swift which will hold all your 'universal' variables (if these are going to be on every page, I see no reason this isn't the 'best' way to do it - certainly it is the most simple to understand!)
in Variables.swift, hold all your universal variables
struct Variables {
static var statsHealth = 0
.....
}
Then, in each other page, access them at any time
healthLabel.text = String("Health: \(Variables.statsHealth)/10")
or set them
Variables.statsHealth += 1
So based on your description, I assume the view controller structure is like this:
AnotherVC -> MainVC
MainVC is presented on top of AnotherVC. When you go back to AnotherVC, did you dismiss MainVC completely? If so, then every time you go from AnotherVC to MainVC, it initiate a new ViewController, and the variables you saved before doesn't exist anymore.
If you want to keep this structure and change variables in MainVC, keep a reference of mainVC in AnotherVC. Then instead of connecting in storyboard, you may want to present it programmatically.
class AnotherVC {
var mainVC: MainVC?
func presentMainVC() {
var targetVC = UIViewController()
if self.mainVC != nil {
targetVC = self.mainVC
} else {
let storyboard = UIStoryboard(name: "Your-storyboard-name", bundle: nil)
targetVC: MainVC = storyboard.instantiateViewControllerWithIdentifier("The-main-VC-identifier") as MainVC
self.mainVC = targetVC
}
//you can change your variable here
mainVC.statsHealth += 1
self.presentViewController(self.mainVC, animated: true, completion: nil)
}
If you mainVC is on top of AnotherVC in any case, you can just revert the reference direction.