Giving a xib multiple file owners - swift

I am attempting to have a header view appear on multiple viewcontroller's tableviews. I do not know what I should set the xib files owner class to. I am trying to do this so that I can call a button on the header's action. I have already tried UIViewController and have had no luck. Any help would be appreciate.

The best (in my opinion) if you create a dedicated controller class for the view. You can load and add the loaded ViewController like:
let cmVC = ColorMenuVC(nibName: "ColorMenu", bundle: Bundle.main, textProperty: _textProperty!)
//font menu initialization & settings
cmVC.modalPresentationStyle = UIModalPresentationStyle.popover
//popover settings
let popover = cmVC.popoverPresentationController
popover?.delegate = self
popover?.barButtonItem = sender
popover?.permittedArrowDirections = UIPopoverArrowDirection.any
//presenting
present(cmVC, animated: true, completion: nil)
This example for Popovers, but I hope this will helpful. The ColorMenuVC is extending the UIViewController, but here is the original class how to init:
https://developer.apple.com/reference/uikit/uiviewcontroller/1621487-nibname

Related

Swift -injecting vars in ViewControllers

I am proudly new to iOS developing and I am trying to build my first app. I am doing a course on an online platform which does the following in the
AppDelegate -> application didFinishLaunchingWithOptions:
let navigationController = window?.rootViewController as! UINavigationController
let notebooksListViewController = navigationController.topViewController as! NotebooksListViewController
notebooksListViewController.dataController = dataController
This app has a Navigation controller which begins with an UIViewController.
I have 2 questions here, first is why this works, I mean, I am in AppDelegate, so the NotebooksListViewController (first view of the app) is not instantiated yet (I think), so why I am able to inject a variable in it?
On the other hand, the second question, is how can I do this in a different scene? I have a TabBarViewController as first scene, and the first tab is a UITableViewController and I want to inject the same way my dataController var, how can I accomplish this? I could not get to do it, neither understand it.
Thanks in advance.
It works, because of some Xcode magic:
In your Target Setting, General tab, the Main Interface entry specifies the name of the Storyboard to be loaded automatically when your app starts up:
In the storyboard, the Initial View Controller then will be instantiated. It seems like this is an UINavigationController.
Since this is done automatically, it just works - until you want to do something special :-)
If you want to start up with a different scene - maybe from a different view controller - you could just change either the Main Interface to another storyboard, the Initial View Controller (inside the storyboard) or both.
Or, you could just start up by yourself, by leaving the Main Interface empty and create your own view controller inside the app delegate (didFinishLaunchingWithOptions), something like
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
if let tabVC = mainStoryboard.instantiateViewControllerWithIdentifier("TabCtrl") as? UITabBarController {
self.window?.rootViewController = tabVC
// Access the subcontrollers, or create them
// Initialize their values
// tabVC.viewControllers[0].data = ...
} else {
// Ooops
}
self.window?.makeKeyAndVisible()
Answer to your first question
as the method name is self explanatory didFinishLaunchingWithOptions means your application is didfinish with launching with options and its about to enter in foreground so here application need to set rootViewController so in this method controller you want to set as view controller is initiated thats why you can inject variable in it
answer to second question
let navigationController = window?.rootViewController as! UITabbarController
let VC = navigationController.childViewController
//Now Using VC you can access all you controller of tabbar controller
let notebooksListViewController = navigationController.topViewController as!
NotebooksListViewController
notebooksListViewController.dataController = dataController
now as shown above you can use VC to access you view controllers
but be careful here because VC return viewcontroller array so you need make checks for perticular VC you want to access

Opening window + view from an other view controller

I've got a ViewControllerOne. ViewControllerOne is connected via Ctrl-Drag (in storyboard) to a menu-button mBtn (which means I don't know how it is implemented programmatically).
Clicking on this mBtn, a ViewOne appears (present modal). This ViewOne is bound to ViewControllerOne. ViewOne has a button btnOne.
Clicking on btnOne I want ViewOne to be dismissed and ViewTwo to be shown. ViewTwo belongs to ViewControllerTwo and to WindowControllerTwo.
The WindowControllerTwo-ViewControllerTwo-binding is the standard case as created on a new project.
I have the following code in the IBAction for button btnOne in ViewControllerOne:
#IBAction func onbtnOnePressed(sender: AnyObject){
let m_WindowControllerTwo = NSStoryboard(name: NSStoryboard.Name(rawValue: "Main"), bundle: nil).instantiateController(withIdentifier: NSStoryboard.SceneIdentifier("WindowControllerTwo")) as! NSWindowController // I have no custom class for the window controller as I don't really know what I can use it for ...
let m_ViewTwo = WindowControllerTwo.contentViewController as! ViewControllerTwo // my custom class for ViewTwo
m_ViewTwo.attributeIWantToPassToThisView = self.x // an attribute I want to pass from view a to view b
m_WindowControllerTwo.contentViewController = m_ViewTwo // passing the attribute from a to b
m_WindowControllerTwo.showWindow(self) // this does not work
self.dismiss(nil) // see NOTE
}
This code actually does not work. On debugging it step by step, I'm seeing the window/view flickering but not appearing...
NOTE: I could connect the button btnOne with a ctrl-drag to ViewControllerTwo. This works. But then the current ViewOne does not get dismissed!
Question: What am I doing wrong here? In iOS swift this also works. I don't quite get the WindowController stuff, so I'll need your advice on this.
Instead of this: m_WindowControllerTwo.showWindow(self)
use:
let application = NSApplication.shared()
application.runModal(for: wordCountWindow) //this will present WindowControllerTwo modally.
then to close your present controller add this line: PresentWindowControllerName.close()

Swift UITableView inside Tabbar

I'm new in Swift, and now I'm trying to develop an app. I need to show UITabBarController with 3 tabs. Each tab has the same ViewController (it is custom class, inherited from UIViewController).
Sequence of my actions:
1. Put TabBarController in storyboard, dropped Tab1 and Tab2 which were situateed in it by default, and pointed a custom class (ArchiveTabBarController) and Storyboard ID (ArchiveTabBarVC).
2. Put UITableViewController in storyboard, pointed custom class (ArchiveTableViewController), and pointed storyboard ID (archiveTableVC).
3. Made a custom cell in this TableViewController: pointed a custom class (ArchiveTableViewCell), and pointed identifier (archiveTableViewCell).
4. In my ArchiveTabBarController, I wrote such code:
override func viewWillAppear(_ animated : Bool) {
super.viewWillAppear(animated);
let item1 = ArchiveTableViewController();
item1.archiveType = "";
let item2 = ArchiveTableViewController();
item2.archiveType = "type1";
let item3 = ArchiveTableViewController();
item3.archiveType = "type2";
let controllers = [item1, item2, item3];
self.viewControllers = controllers;
}
override func viewDidLoad() {
super.viewDidLoad();
delegate = self;
}
In my ArchiveTableViewController, in viewDidLoad(), I make GET-Request to load data. Result depends on archiveType.
5. In my mainMenuViewController, I made transition to the ArchiveTabBarController.
And the problem is: when I'm trying to go to the ArchiveTabBar, it fails with error: unable to deque reuasble cell with identifier "archiveTableViewCell". But when I dropped TabBar, and tried to go to the ArchiveTableViewController, it worked fine.
What is my mistake?
So, I tried next:
in my ArchiveTableViewController, in viewDidLoad(), i put next:
tableView.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: "archiveTableViewCell")
And in cellForRowAt, I used standard text field for stanard cell. Everything works fine. So, the problem is in the cell's registration. Anybody can help with it?
Finally, I did this! I read questions custom UITableCellView programmatically using swift and Custom UITableViewCell register class in Swift, and more questions, and decided to try register XIB file. I created TableViewCell from scratch with XIB, and everything works.

How do I segueing to the UIViewController I'm in?

I'm trying to go from one view, filled with data from an object, to the same view but filled with a different object via segue.
Using a segue is necessary as apposed to switching the object and refreshing the view because my users need to be able to go back to the past view controller when they hit the back button.
Example:
(ThisView, with thisObject populating the view) -> (ThisView, with thisOtherObject populating the view)
What I've tried:
presentView Controller: This didn't work because it is not the default segue I'm trying to achieve.
let next: NewClubProfile = storyboard?.instantiateViewControllerWithIdentifier("clubProfile") as! NewClubProfile
presentViewController(next, animated: true, completion: nil)
Using Navigation Controller: Can't figure out how to segue the new object to populate the other view
let next: NewClubProfile = storyboard?.instantiateViewControllerWithIdentifier("clubProfile") as! NewClubProfile
self.navigationController?.pushViewController(next, animated:true)
Should have taken longer to solve this myself before posting this but I couldn't find an answer that fit what I needed. Anyway, solved it, here's what worked for me.
let next: ViewController = storyboard?.instantiateViewControllerWithIdentifier("viewName") as! ViewController
next.object = newObject
self.navigationController?.pushViewController(next, animated:true)

Instantiate View Controller not working

I'm trying to load another UIViewController so that I can get the data of the IBOutlets inside of it. I'm using Swift and Sprite-Kit. The UIViewController I am talking about is called GameViewController. It is the first thing that loads up in my game. Then it goes to my GameScene. This is where I am trying to instantiate my GameViewController. I want to do this because I am using a library called iCarousel and I want to animate it moving up from my GameScene. However, iCarousel can only be used on a UIViewController. So I want to call a function in GameViewController that will be called in GameScene. In this function, there is an NSLayoutConstraint that is used to move the carousel. Whenever I call this function, it says that its nil. Here is some of my code:
Inside my GameScene:
let storyboard: UIStoryboard = UIStoryboard.init(name: "Main", bundle: nil)
let firstViewController: GameViewController = storyboard.instantiateViewControllerWithIdentifier("Load-up") as! GameViewController
firstViewController.start()
Inside my GameViewController:
func start() {
// Constraint
top.constant = 100
UIView.animateWithDuration(2){
self.view.layoutIfNeeded()
}
}
If you need more information, feel free to ask me. Thanks in advance!
That is because your view controller hasn't loaded its view hierarchy yet. viewController loads its view hierarchy only when something sends it a view message. The system will do this by its own when to put the view hierarchy on the screen. And it happens after calls like prepareForSegue:sender: and viewWillAppear: , loadView(), self.view.
So here your outlets are still nil since it is not loaded yet.
Just try to force your VC to call self.view and then access the properties/outlets.
Edit: Added sample code.
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
let firstViewController = storyboard.instantiateViewControllerWithIdentifier("Load-up") as! GameViewController
debugPrint("Printing for the sake of initializing view of VC: \(firstViewController.view)")
firstViewController.start()