In my Restaurant model I have a function, that returns UINavigationController, I've implemented it to make my code more readable. But I am curious if it is ok to do it this way, or should I make a function of UIViewController, that takes Restaurant as a parameter.
func reserveTable(timeIndex: Int) -> UINavigationController {
let storyBoard = UIStoryboard(name: "Reservations", bundle: nil)
let targetVC = storyBoard.instantiateViewController(withIdentifier: "bookingNC") as! UINavigationController
let destinationVC = targetVC.topViewController as! BookingViewController
destinationVC.availabeleDates = self.schedule
destinationVC.timeRange = self.avaliableTimes
destinationVC.restaurantId = self.id
destinationVC.requestedTimeIndex = timeIndex
destinationVC.reservationType = "Бронирование"
return targetVC
}
I would do this differently, your model is supposed to handle data, not Views.
There are many ways in which you could do this, personally I would have a property on the booking controller for the reservation. Then you could just do something like:
let vc = BookingViewController()
vc.reservation = reservation
self.present(vc, animated: true)
wherever you needed to present the booking controller. Ensuring you have a reservation first.
In your init/viewDidLoad method of the BookingViewController you can do alot of the work above...
override func viewDidLoad() {
super.viewDidLoad()
if let reservation = self.reservation {
self.availabeleDates = reservation.schedule
self.timeRange = reservation.avaliableTimes
self.restaurantId = reservation.id
self.requestedTimeIndex = timeIndex
self.reservationType = "Бронирование"
}
}
Related
I have an array UIButtons that have a unique tag value. When a given button is pressed, I want to load and present a UIViewController that is also stored in an array of equal length.
The UIViewControllers to be presented are subclasses of a subclass of UIViewController:
class AViewController: UIViewController {}
class BViewController: AViewController {}
class CViewController: AViewController {}
class DViewController: AViewController {}
// ... etc.
I've tried storing the array of AViewController subclasses using the following:
private var array: [AViewController] = [BViewController.self, CViewController.self, DViewController.self]
but I get the error Cannot convert value of type '[BViewController].Type' to specified type '[AViewController]' for the first element.
I will then present BViewController (for instance) using the following:
let ViewController = array[button.tag].self
var viewController: AViewController
viewController = ViewController.init()
viewController.transitioningDelegate = self
viewController.modalPresentationStyle = .custom
present(viewController, animated: true)
Please let me know if this is the incorrect thought process for doing something like this please and thanks for any help.
You need instances
private var array: [AViewController] = [BViewController(), CViewController(), DViewController()]
if the vcs are in IB , then you would do
let b = self.storyboard!.instantiateViewController(withIdentifier: "bID") as! BViewController
let c = self.storyboard!.instantiateViewController(withIdentifier: "cID") as! CViewController
let d = self.storyboard!.instantiateViewController(withIdentifier: "dID") as! DViewController
Then
array = [b,c,d]
lazy var VCArray :[UIViewController] = {
return [VCinst(name: "firstVC"),VCinst(name: "secondVC"), VCinst(name: "thirdVC")]
}()
func VCinst(name:String) -> UIViewController {
return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(identifier: name)
}
Alternate answer if you want to store array of view controllers as a constant.
struct AppConstant {
static let yourViewControllers: [AnyClass] = [AViewController.self,
BViewController.self, CViewController.self]
}
I am using Pulley where I am using below code to open the PulleyViewController.
func openEventInfoPage(event : EventsModel,indexPath:NSIndexPath){
let vc = storyboard?.instantiateViewController(withIdentifier: "PulleyViewController")
as! PulleyViewController
navigationController?.pushViewController(vc, animated: true)
}
Here, while opening the PulleyViewController I also need to send data to both DrawerContentViewController and PrimaryContentViewController. How should I do that?
Its nowhere mentioned on Github, I tried making object of both the classes and sending data before Pushing PulleyViewController but it didn't work. Thanks in advance.
I came out with the following solution and it worked perfectly.
let mainContentVC = storyboard?.instantiateViewController(withIdentifier: "PrimaryContentViewController") as! PrimaryContentViewController
mainContentVC.event = event
mainContentVC.indexPath = indexPath
let drawerContentVC = storyboard?.instantiateViewController(withIdentifier: "DrawerContentViewController") as! DrawerContentViewController
drawerContentVC.event = event
let pulleyDrawerVC = PulleyViewController(contentViewController: mainContentVC, drawerViewController: drawerContentVC)
navigationController?.pushViewController(pulleyDrawerVC, animated: true)
Add a property on DrawerContentViewController. like - aProperty as open property. And assign value of aProperty on your DrawerContentViewController -
func openEventInfoPage(event : EventsModel,indexPath:NSIndexPath){
let vc = storyboard?.instantiateViewController(withIdentifier: "PulleyViewController") as! PulleyViewController
let vc2 = storyboard?.instantiateViewController(withIdentifier: "DrawerContentViewController") as! DrawerContentViewController
vc2.aProperty = "value"
navigationController?.pushViewController(vc, animated: true)
}
I have 2 controllers, DashboardController and LocateVehicleController. LocateVehicleController has UITableViewController.
In DashboardController, On button press I am doing API call and getting data. And sending array to LocateVehicleController.
let locateVehicleStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let locateVehicleController = locateVehicleStoryboard.instantiateViewController(withIdentifier: "tempID") as? LocateVehicle
self.present(locateVehicleController!, animated: true, completion: nil)
locateVehicleController?.dataArray = self.locateVehicleDataArr
locateVehicleController?.tableView.reloadData()
In LocateVehicleController I have refresh button, If I press refresh button I need to update the tableview controller data which I have used API call data from DashboardController.
As per my understanding when I press refresh button, same API call will invoke. Please help to solve this. Thanks in advance.
I would move the logic of calling of this API to another class. Inject this class into DashboardController and LocateVehicleController and use it to fetch you data. Something like this:
struct Vehicle {
}
protocol VehicleDataFetcherProtocol {
func getVehicleData(completionHandler:([Vehicle])->()) // assuming the data is in form of an Array of a class/struct Vehicle
}
class VehicleDataFetcher: VehicleDataFetcherProtocol {
// Hit API, get data and call the completion hanlder
func getVehicleData(completionHandler:([Vehicle])->()) {
}
}
class DashboardController:UIViewController {
private var vehicleDataFetcher: VehicleDataFetcher!
func injectVehicleDataFetcher(dataFetcher:VehicleDataFetcher) {
self.vehicleDataFetcher = dataFetcher
}
func presentLocateVehicle() {
let locateVehicleStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let locateVehicleController = locateVehicleStoryboard.instantiateViewController(withIdentifier: "tempID") as? LocateVehicleController
self.present(locateVehicleController!, animated: true, completion: nil)
locateVehicleController?.injectVehicleDataFetcher(dataFetcher: self.vehicleDataFetcher)
locateVehicleController?.dataArray = self.locateVehicleDataArr
locateVehicleController?.tableView.reloadData()
}
}
class LocateVehicleController:UIViewController {
private var vehicleDataFetcher: VehicleDataFetcher!
func injectVehicleDataFetcher(dataFetcher:VehicleDataFetcher) {
self.vehicleDataFetcher = dataFetcher
}
}
My mission
When app receive notification and user taps on the notification i want to redirect the user to the correct View. In my case, SingleApplicationViewController.
Current code
PushNotification.swift - A class with static functions to handle behaviors when receiving Push Notifications
The __getNavigationController returns a specific NavigationController based on a tab -and viewIndex from TabBarController.
internal static func __getNavigationController(tabIndex: Int, viewIndex: Int) -> UINavigationController {
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let window:UIWindow? = (UIApplication.sharedApplication().delegate?.window)!
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyBoard.instantiateViewControllerWithIdentifier("MainEntry")
window?.rootViewController = viewController
let rootViewController = appDelegate.window!.rootViewController as! UITabBarController
rootViewController.selectedIndex = tabIndex
let nav = rootViewController.viewControllers![viewIndex] as! UINavigationController
return nav
}
The applicationClicked is being called when user click on notification and that method calls on __getApplication to fetch the application from the db with the objectId received in the push notification and then instantiate a GroupTableViewController to perform segue to the SingleApplicationViewController.
(TabbarController -> Navigation Controller -> GroupTableViewController -> SingleApplicationViewController)
What is a bit strange is when I set tabIndex to 0 and viewIndex to 1. The GroupView however is on second tab (tab 1) and the view controller should be the first (0). But when I set them to the corresponding numbers, I receive nil and the application crashes.
I read that you will force the view controller to load when doing _ = groupTableViewController.view and which it actually does. When this is being called, the viewDidLoad -function is being called.
/************** APPLICATION ***************/
static func applicationClicked(objectId: String) {
__getApplication(objectId) { (application, error) in
if application != nil && error == nil {
let nav = __getNavigationController(0, viewIndex: 1)
let groupTableViewController = nav.viewControllers.first as! GroupsTableViewController
_ = groupTableViewController.view
groupTableViewController.performSegueWithIdentifier("GroupTableToApplicationToDetailApplication", sender: application!)
} else {
// Hanlde error
}
}
}
GroupTableViewController.prepareForSegue()
Here I create a new instance of the ApplicationTableViewController, which is a middle step before getting to SingleApplicationViewController
} else if segue.identifier == "GroupTableToApplicationToDetailApplication" {
let navC = segue.destinationViewController as! UINavigationController
let controller = navC.topViewController as! ApplicationViewController
controller.performSegueWithIdentifier("ApplicationsToSingleApplicationSegue", sender: sender as! Application)
}
So, what's not working?
Well, the prepareForSegue in GroupTableViewController is not being called. I use the same code structure on my TimeLineViewController, and almost the exact same code, when getting another Push Notification and it works perfectly. In that case I use tabIndex 0 and viewIndex 0 to get the proper NavigationController.
Please, any thoughts and/or suggestions is more than welcome!
There is change in following method..
internal static func __getNavigationController(tabIndex: Int) -> UINavigationController {
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let window:UIWindow? = (UIApplication.sharedApplication().delegate?.window)!
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyBoard.instantiateViewControllerWithIdentifier("MainEntry")
window?.rootViewController = viewController
let rootViewController = appDelegate.window!.rootViewController as! UITabBarController
rootViewController.selectedIndex = tabIndex
let nav = rootViewController.selectedViewController as! UINavigationController //This will return navigation controller..
//No need of viewIndex..
return nav
}
you have written
let nav = rootViewController.viewControllers![viewIndex] as! UINavigationController
change to rootViewController.selectedViewController give you UINavigationController.
Here you get navigavtion controller object..In your applicationClicked method nav object might be nil so it can not execute further performsegue code.
Check following method.
/************** APPLICATION ***************/
static func applicationClicked(objectId: String) {
__getApplication(objectId) { (application, error) in
if application != nil && error == nil {
let nav = __getNavigationController(0)//0 is your tab index..if you want 1 then replace it with 1
let groupTableViewController = nav.viewControllers.first as! GroupsTableViewController //Rootview controller of Nav Controller
groupTableViewController.performSegueWithIdentifier("GroupTableToApplicationToDetailApplication", sender: application!) //Perform seque from Root VC...
} else {
// Hanlde error
}
}
}
I've two storyboards and need to pass messages and objects. I know how to do it in the same storyboard and with .xib files, but not with two different storyboards.
My code is:
var storyboard = UIStoryboard(name: "RecibosStoryboard", bundle: nil)
var controller = storyboard.instantiateViewControllerWithIdentifier("RecibosStoryboard") as! UINavigationController
self.presentViewController(controller, animated: true, completion: nil).
// If i do: var controller = storyboard.instantiateViewControllerWithIdentifier("RecibosStoryboard") as! = TableRecibosViewController -> fails ->cannot convert TablaRecibosViewController to UINavigationController
// If i do:
/* var controller = storyboard.instantiateViewControllerWithIdentifier("RecibosStoryboard") as! UINavigationController
let vartest: TablaRecibosTableViewController = TablaTablaRecibosTableViewController()
prueba.inicioPrueba(str, strPrueba2: str2) -> two objects are nill
self.presentViewController(controller, animated: true, completion: nil).
Two objects are nill*/
My second storyboard is "RecibosStoryboard" and only has 1 view who class is TablaRecibosViewController and has a contructor method:
class TablaRecibosTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet var tablaRecibos: UITableView!
var pruebaMansajes: String?
var pruebaMansajes2: String?
var arrayOfObjectsCellRecibos: [ObjectTableRecibos] = [ObjectTableRecibos] ()
override func viewDidLoad() {
super.viewDidLoad()
tablaRecibos.dataSource = self
tablaRecibos.delegate = self
println("Pruebas satisfactorias1 \(pruebaMansajes) \(pruebaMansajes2)")
}
func inicioPrueba(strprueba1:String, strPrueba2:String){
pruebaMansajes = strprueba1
pruebaMansajes2 = strPrueba2
}
When i execute the App or crash or print two objects = nil
I don't find the way to do. Thanks a lot.
The problem is the following "cannot convert TablaRecibosViewController to UINavigationController". Your TablaRecibosViewController is embedded inside a UINavigationController. Try the following:
if let recibosViewController = (storyboard.instantiateViewControllerWithIdentifier("RecibosStoryboard") as? UINavigationController).viewControllers.first as? TablaRecibosTableViewController {
recibosViewController.pruebaMensajes = "Hola mundo"
}
You were creating a new instance of TablaTablaRecibosTableViewController() which was not associated to the Storyboard, therefore the table view was nil and it crashed.