I have an Swift app (xCode 7.1). I need send a value from a first viewController that contains a tableView to the two viewControllers into a TabBar Controller:
ViewController -> Tabbar -{ViewControllerTabBarView1
-{ViewControllerTabBarView2
I call the tabBar controller from didSelectRowAtIndexPath
let vc : AnyObject! = self.storyboard!.instantiateViewControllerWithIdentifier("myTab")
self.showViewController(vc as! UIViewController, sender: vc)
All this is correct, but the values to pass from click into row to respective viewController of TabBar not work calling from didSelectRowAtIndexPath. This is null
let viewController = self.storyboard!.instantiateViewControllerWithIdentifier("view1TabBar") as! EventoToDoController
viewController.finca = (parsed.valueForKey("id")[indexPath.row] as? String)!
How can I send this values?
Related
I have a view controller like below.
This view is attached with a tabBarController. The tabBarController has 5 viewControllers and I have to present the 5th viewController of tabBar from another page. So I used the below codes for present that viewController
#IBAction func onClickUserProfile(_ sender: Any) {
let navVc = self.storyboard?.instantiateViewController(withIdentifier: "ProfileVC")as! ProfileVC
navVc.userId = Int(self.userId)
navVc.navigationItem.hidesBackButton = true
navVc.tabBarController?.tabBar.isHidden = false
self.navigationController?.pushViewController(nxtVc, animated: true)
}
But after execute the code it resulting the view controller as the below image.
The view undergoes the tabBar. Anyone help me to push to a tabBar view.
You need to set the selected UIViewController from UITabBarController something like this should work .
self.tabBarController?.selectedViewController = self.tabBarController?.viewControllers![1]
where tabBarController?.viewControllers returns the array of current ViewControllers embedded in the UITabBarController .
Your code should be something like this.
#IBAction func onClickUserProfile(_ sender: Any) {
let vc = self.tabBarController?.viewControllers![1] as! ProfileVC // use your index
vc.userId = Int(self.userId)
self.tabBarController?.selectedViewController = vc
}
Note: Don't create an instance of the UIViewController as .instantiateViewController(withIdentifier:) use the already existed
ones in the array tabBarController?.viewControllers, creating new
instance will be treated as new one and gives the problem you have up
there .
I have a ViewController where I call segue to another ViewController:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == blogSegueIdentifier {
let destination = segue.destination as! Reply
let path = tableView.indexPathForSelectedRow
let cell = tableView.cellForRow(at: path!) as! QuestionsTabCell
destination.blogName = String(cell.labelQues!.tag)
}
}
In the opened segue (ViewController) there is a UITextView and a send button in the UINavigationBar. So user inputs textView and sends it to server tapping on the button and after this opened segue (ViewController) closes and user returns to first ViewController. How to call a method in first ViewController when user closes segue without using viewWillAppear?
Hello #Mr Jo You can use unwind segue for this purpose.
declare a method in your first viewController Like :-
//MARK: - - Unwind Segue get data from select categories
#IBAction func unwindToAssignCatController(segue: UIStoryboardSegue) {
//get back data from selectcategory class
if segue.identifier == "segueIdentifier"{
let controller = segue.source as! Reply
// get your values from second viewController and use in first viewController
}
After that you have to select your second ViewController
1: right Click on Controller and drag on exit then below method will appear
2: Select it and assign an identifier to it.
3: then perform segue on that action where you want in second viewController with same identifier.
If you have the NavigationController, try: (Objective-C)
FirstVC* firstVC = self.navigationController.viewControllers[self.navigationController.viewControllers.count
- 2]
last but one in a series of viewControllers in the navigation stack
If you are opening the second viewController without the NavigationController, you can get the instance of the first viewController like:
FirstVC* firstVC = self.presentingViewController
Please not that the instance of the presenting VC will be available in this property after the transition will finish, in the viewDidAppear, for example
I can swap from one UIViewController to another within a UITabBarController using tabBarController?.selectedIndex = targetIndex. I would like to know how to trigger a segue after the switch.
I have tried doing if let vc = tabBarController?.viewControllers?[0] as MyViewController {} and calling vc.performSegue() but it errors out, complaining that it's a UINavigationController. (I couldn't even get vc.prepare() to work but I can't work out how to create a UIStoryBoardSegue, which it requires as the first argument, from scratch using the segue identifier.) I think this is because all of my UIViewControllers in the UITabBarController are within Navigation Controllers.
How do I switch from index 0 to 1 in the tab bar controller, and then trigger a segue in the view controller embedded in the navigation controller? Note, I need to pass in a copy of the calling object (my UIViewController) or a payload for contextual purposes to the UIViewController being segued to.
Well like you said the viewController which is presented at the index is a UINavigationController. You should get the rootViewController of the UINavigationController and perform the Segue on the RootViewController.
Below code should work:
self.tabBarController?.selectedIndex = 1
guard let vc = self.tabBarController?.viewControllers?[1] as? UINavigationController else {
return
}
guard let rootVC = vc.viewControllers.first as? SecondViewController else {
return
}
rootVC.vc = self
rootVC.performSegue(withIdentifier: "test", sender: self)
I currently have a button set to go to a TableViewController but decided I wanted to embed that TableViewController in a TabBarController. Well before I did this I had this bit of code in the previous ViewController.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "showListSegue"){
let des = segue.destinationViewController as! TableViewController
des.jsonfile = self.jsonfile
}
}
Now when I try and click the button it give me this error.
Could not cast value of type 'UITabBarController' (0x111c818b0) to
'Forkit.TableViewController' (0x10d1b1490).
I tried the following code with no success as it gives me this error
Value of type "UITabBarController" has no member 'jsonfile'.
let des = segue.destinationViewController as! TableViewController
How can I get around this?
The segue showListSegue now has your TabBarController as it's destinationViewController. This is why it can not cast it to TableViewController, because it's not the one you're looking for. The TabBarController does contain a reference to the ViewController you want though. You likely have two options:
1: Use .viewControllers and find your view in there, like this:
let tabBarController = segue.destinationViewController as! UITabBarController
let des = tabBarController.viewControllers![0]
In which [0] selects which tab's ViewController you need.
2: Select the right tab and use .selectedViewController
let tabBarController = segue.destinationViewController as! UITabBarController
tabBarController.selectedIndex = 0 // choose which tab is selected
let des = tabBarController.selectedViewController!
Please be aware that you are working with optional values here, so either be absolutely sure you can safely unwrap, or build in checks (casting with as? and if let statements, etc.).
More information: UITabBarController Class Reference
Try This
#IBAction func about(sender: AnyObject) {
performSegueWithIdentifier("about", sender: sender)
}
I am trying to Compile my app again now that Swift 2 is out and the thing is I am having an error with TabBarController instances.
I am declaring the instances in vars in order to use methods from anothers ViewControllers.
Here it's my code:
let barViewControllers = self.tabBarController?.viewControllers
let listViewController = barViewControllers![2].viewControllers![0] as! dbViewController //The [2] is because it's the third TabBar and the [0] it's because It's embebed in a NavigationController.
let calendarViewController = barViewControllers![1] as! CalendarViewController
In the second line Im having the following error:
UIViewController does not have a member named "viewControllers"
Anybody could help me?
Thanks
You are trying to access the property viewControllers of the type UIViewController, which it doesn't have. viewControllers is a property on a UITabBarController, but viewControllers returns an array of UIViewController.
Cast viewControllers to an array of UITabBarController (or only the item you extract) to access it's viewController property.
Like this:
let barViewControllers = self.tabBarController?.viewControllers as! [UITabBarController]
Or this:
let listViewController = (barViewControllers![2] as! UITabBarController).viewControllers![0] as! dbViewController