xcode navigationbar is not more visible after popover with button - ios

I have created a popover in iphone view with a button inside to segue to another ViewController.
But all Viewcontroller after popOver have the navigationbar not anymore.
My project structure:
NavigationController -> ViewController1 -> ViewController2(popOver) ->ViewController3
All connections are: "show",except from ViewController1 to ViewController2: "present as popover"
If i connect(show) directly from ViewController1->ViewController3, everything is fine...
Where can be the problem?
I used this tutorial:
http://richardallen.me/2014/11/28/popovers.html
ViewController1:
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
return UIModalPresentationStyle.None
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "PopOverIdentifier" {
let popoverViewController = segue.destinationViewController as!
popoverViewController.modalPresentationStyle = UIModalPresentationStyle.ViewController2
popoverViewController.popoverPresentationController!.delegate = self
}
}
ViewController2
includes Pickerview and:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "toVC3" {
var DestViewController : ViewController3 = segue.destinationViewController as!ViewController3
DestViewController.passedUserID_target = selected_user
}
}
Problem:

I solved this problem (temporarily) with adding a new NavigationController to the ViewController3 with (Editor->EmbedIn->NavigationController) and add a back button to buttom of the layout.
The problem to segue data from VC1->VC3, i solved with defining a global Variable in VC3 and add a action Button (in VC2) with assigning the data to these created global variables.

Related

Passing a labels text from a ViewController to a MapViewController

I've got this app in which I need to pass an address displayed on a textLabel ( I am adding an invisible button over the textLabel.
So by tapping on the button, I pretend to pass the text from that View to another view which is a MapViewController to display that text and get directions using the Maps App from the iPhone.
I am trying to do this through performing a segue like this:
This inside my MainViewController :
#IBAction func goToMap(_ sender: Any? ) {
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.destination is MapViewController
{
let vc = segue.destination as? MapViewController
vc?.pinAddress = addressLabel.text!
}
}
}``
// pinAddress is declared inside the MapViewController and I need the text in the addressLabel that is actually in MainViewController.This is not working though...
You need to get prepare(for segue out of the IBAaction
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let vc = segue.destination as! MapViewController
vc.pinAddress = addressLabel.text!
}
#IBAction func goToMap(_ sender: Any? ) {
self.performSegue(withIdentifier:"segue",sender:nil)
}

Seque order for selection lists

I am implementing the following. I have a view controller and I have buttons that open a table view controller with selection items. when I then select an item in a cell and go back to the view controller, previous values are blank (looks like it opens a new instance). The code I am using to go back is as follows:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "playerselected" {
let cell = sender as! UITableViewCell
let indexPath = tableView.indexPath(for: cell)
let itemController : GameViewController = segue.destination as! GameViewController
let item : Player = frc.object(at: indexPath!) as! Player
itemController.item2 = item
}
Yes, you're right, if you have segue from VC1 (GameViewController) to VC2 (SomeTableViewController) and then from VC2 to VC1, it's not the same instance of VC1.
If you don't use UINavigationController, you can simply "go" from VC2 back to VC1 by dismissing your VC2 using
dismiss(animated: true)
But how can you now pass data back to your previous controller? Start with this: in VC2 create closure variable which takes parameter of type Player. Then before you dismiss VC2, call this closure
class SomeTableViewController: UITableViewController {
var callback: ( (Player) -> Void )?
func playerSelected(_ player: Player) {
callback?(player)
dismiss(animated: true)
}
}
But will happen after callback is called? Nothing, you have to assign what will happen before you present VC2. So you can override prepare(for:sender:) inside VC1 and assign destination's callback closure variable (when callback will be called, you need to change VC1's item2)
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segueToTableViewController" {
let tableViewController = segue.destination as! SomeTableViewController
tableViewController.callback = { player in
self.item2 = player
}
}
}

Segue To View Controller Inside Of A UITableView

How do you segue from a ViewController to a ViewController located in a UITableView (which is in a NavigationController)?
Image of Storyboard with description
I can Control-Drag from my "DetailButton" to the "DetailViewController", but it is not displayed in the navigation controller if you do that. The NavigationBar should still allow a user to go back to the UITableView, even if they segued here from the "DetailButton".
Here's the solution I came up with. If anyone else has a better way, please post it here.
Storyboard Steps:
Control-Drag from the "DetailButton" to the Navigation Controller to create a segue. Give this segue the identifier "detailSegue".
Control-Drag from the UITableViewCell you would like to link to DetailViewController to the DetailViewController. Give this segue the name "showDetail".
UITableViewController Steps:
var shouldSegueToDetail = false
override func viewDidLoad() {
...
// Check if we should segue to DetailViewController
if shouldSegueToDetail {
// Reset flag
shouldSegueToDetail = false
// Go to DetailViewController
performSegue(withIdentifier: "showDetail", sender: self)
}
}
OriginalViewController Steps:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "detailSegue" {
let navigationVC = segue.destination as! UINavigationController
let tableVC = navigationVC.viewControllers[0] as! UITableViewController
tableVC.shouldSegueToDetail = true
}
}
Option:
Instead of making a segue from the UITableViewCell to the DetailViewController, you could probably call UITableView's selectRow method with the IndexPath of the item you want selected.

How to segue multiple view controllers programmatically using swift 3.0

I'm getting an error:
has no segue with identifier 'dest1''
and what I did was:
route(ctrl+drag) in storyboard viewcontroller -> dest1, viewcontroller -> dest2
have an action button in viewController.swift
set storyboard ids to "dest1" and "dest2"
#IBAction func Parameters(_ sender: AnyObject) {
self.performSegue(withIdentifier: "dest1", sender: nil)
}
Prepare for segue
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
print("0")
if segue.identifier == "dest1" {
if let svc = segue.destination as? secondViewController {
print("1")
}
} else if segue.identifier == "dest2" {
if let svc = segue.destination as? reverbViewController {
print("2")
}
}
What am I missing?
From your description, you have set storyboard ids to "dest1" and "dest2", whereas you need to set segue identifiers. Click on the segue itself in IB, and set it's property.
Storyboard identifiers are used when creating the view controller using code...
instantiateViewControllerWithIdentifier("myViewController")

Pass data from UIViewController to UISpiltView

Right now I am facing some problem to pass data from UIViewController to UISpiltViewController.
My app start with UIViewController, and after user login it, there is a segue from UIViewController to UISpiltViewController, UISpiltViewController is root view of a NavigationViewController, and this NavigationViewController is a root view of a TableViewController. I have written a class for this TableViewController, called MasterView, and right now I want to pass some information from loginView to this MasterView.
self.performSegueWithIdentifier("login", sender: self)
But I want to know how to write the prepareforsegue function.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "login"
{
let controller = (segue.destinationViewController as UINavigationController).topViewController as MasterViewController
controller.authorityNum = self.authorityArray
}
}
I want to pass an array, but I will get class cast error. Please help me with this.
Try this i am not sure but i think your segue is Model segue so
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "login"
{
var splitView:UISplitViewController = segue.destinationViewController as! UISplitViewController
var controller = splitView.viewControllers.first?.topViewController as! MasterViewController
controller.authorityNum = self.authorityArray
}
}
Try to NSLog every line it may help
Like this,
let destinationViewController = segue.destinationViewController as! MasterViewController
Swift 3 of the accepted answer.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "login" {
let splitView:UISplitViewController = segue.destination as! UISplitViewController
let navController = splitView.viewControllers.first as! UINavigationController
let controller = navController?.topViewController as! MasterViewController
controller.authorityNum = self.authorityArray
}
}

Resources