How can I call a new UIViewController within in UITabcontroller - swift

I have a UITabContrroller(here WelcomeViewController) and when I click a button on one of it's tab item page, it shows a popover(PopViewController) with another button for displaying a new page (SecondPageViewController), but should show same tabs in the parent UITabContrroller.
First I tried the option Sequee from the button of PopViewController and uncomment the prepareForSegue, but it did not work.
Later I tried to connect WelcomeViewController(where Tabbar resides) with seguee to SecondPage.
button code is as follows:
#IBAction func goToCarerUpdateProfile(sender: UIButton) {
let careerPageView = self.storyboard?.instantiateViewControllerWithIdentifier("WelcomeViewControllerIdentifier") as! WelcomeViewController
careerPageView.performSegueWithIdentifier("pushToCarerUpdateProfileSegue", sender: self)
}
This code calls the prepareForSegue function of WelcomeViewControllerIdentifier, but there is no Second page visible.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "pushToCarerUpdateProfileSegue" {
print("This portion working")
let svc = segue.destinationViewController as? CarerUpdateProfileViewController
}
How can I call a new UIViewController within in UITabcontroller ? The conditionn is the new View should have the same tabs as previous.

Related

Moving from one NSViewController to another NSViewController

I am totally new to the Mac apps development. I am facing an issue since last two days but could not succeed.
My problem is the same as question Swiching between 2 diferent NSViewControllers with data.
Could you please help me to understand the process and syntax of how to move from one NSViewController to another.
I have a View controller for login where I have two fields i.e. UserId and password.
On the click of the login button a web API is called to authenticate the user and upon receiving "SUCCESS" as the response, control should be transferred from LoginViewController to ProfileViewController.
I have tried to resole this issue as per the answer of the question (link given) but I am getting an error that. "fromviewcontroller.view.superview cannot be nil."
Create a segue from LoginViewController to ProfileViewController and give an identifier to it like "showProfile".
#IBAction func loginButton(_ sender: Any) {
// Code for validating User.
if response == "SUCCESS" {
performSegue(withIdentifier: "showProfile", sender: sender)
}
}
override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
super.prepare(for: segue, sender: sender)
if segue.identifier == "showProfile" {
let profileController = segue.destinationController as! ProfileViewController
profileController.data = responseData
}
}
Not using storyboards?
#IBAction func loginButton(_ sender: Any) {
// Code for validating User.
if response == "SUCCESS" {
let profileController = ProfileViewController(nibName: "ProfileViewController", bundle: Bundle.main)
profileController.data = responseData
self.view.window!.contentViewController = profileController
}
}
You have to create a Segue in your Storyboard (Ctrl + Left click the yellow circle button above your LoginViewController and drag it to your ProfileViewController) and then name it something like "showProfile".
When you received your "SUCCESS" you want to call:
//here you enter the name of the segue you want to call
//& in sender the data you want to pass to the new VC.
performSegue(withIdentifier: "showProfile", sender: nil)
this will call
prepare(for segue: UIStoryboardSegue, sender: Any?)
in your current ViewController, so if you want to pass data to your new ViewController, you need to override it.
Example for passing data between ViewControllers :
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
//asking which segue is called
if segue.identifier == "showProfile" {
//when you called "showProfile", you can be sure that your
//destination is a ProfileViewController
let vc = segue.destination as! ProfileViewController
//passing the data to your new VC
vc.data = sender
}
}

How to programmatically seque to multiple view controllers from a button

I have a view controller with a lot (36) of buttons, and I want each of these buttons to segue to a particular view controller based on a variable that was set earlier in the program. In other words, any button could potentially go to 15 different view controllers based on a variable that was sent to the viewcontroller containing the buttons...
I think I can make this work if I click and drag each button to every viewcontroller... but it seems silly and messy.
I tried doing something like this:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if variable == "Whatever" {
let send = segue.destination as! AViewController
send.variablesent = (sender as! UIButton).title(for: .normal)!}
}
But this only works if I click and drag the button in the storyboard to the "AViewController".
Any help is appreciated, thanks!!
For that you can make segue from SourceViewController to DestinationViewController instead of from Button to Controller, After that when you call perfromSegue in your button action then pass button reference as sender in perfromSegue call.
#IBAction func buttonAction(_ sender: UIButton) {
self.performSegue(withIdentifier: "segueIdentifier", sender: sender)
}
Now in prepareForSegue cast sender to UIButton and set title according to it.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if variable == "Whatever" {
let send = segue.destination as! AViewController
if let button = sender as? UIButton {
send.variablesent = button.titleLabel?.text ?? "Default value"
}
}
}

How to show activity indicator while passing between view controller

Lets say I have 2 ViewControllers, in my MainViewController I have a button which performs a segue to SecondViewController. When button tapped, I'm saving some initial data to coreData, so it takes some time.
Here is the thing that I want to do;
While passing between ViewControllers, I want to show ActivityIndicator, but its starts after SecondViewController is opened. Could you help me? I'm new to Swift.
Here is the code I used in my MainVC:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "SecondViewController") {
SwiftSpinner.show("Loading") // Act. indicator found on github
willRunOnce() // Here Im saving data to CoreData
SwiftSpinner.hide()
}
}
Instead of adding code of ActivityIndicator in prepare(for:sender:) method you need to call it in the Button action and after that call performSegue(withIdentifier:sender:) method.
#IBAction func onBtnSkip(_ sender: UIButton) {
SwiftSpinner.show("Loading") // Act. indicator found on github
willRunOnce() // Here Im saving data to CoreData
SwiftSpinner.hide()
//Now performSegue
self.performSegue(withIdentifier: "identifier", sender: nil)
}

How to identify if a scene has been opened by a certain segue in Swift?

I have a scene which I open with this segue.
//pass all retrived info from signup field over to the login scene to facilitate user experience
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "showLoginController") {
let svc = segue.destinationViewController as! LoginController;
print("pass data over to next viewcontroller")
}
}
This segue goes back to the Login screen where I want to set some values. However when the Login screen has not been loaded by segue "showLoginController", some fields should be empty.
How can I identify if LoginScene has been opened by a certain segue and set values accordingly?
In your LoginController view controller add this:
var fromSegue = false
And in prepareForSegue:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "showLoginController") {
let svc = segue.destinationViewController as! LoginController;
svc.fromSegue = true
print("pass data over to next viewcontroller")
}
}
And then in your LoginController you can check fromSegue.
You can know the segue from the previous scene, in the sender scene, implement prepareForSegue() method to detect the sender

Why two successive page opens with segue

when I am selected button twice will open a new page.
//////
Main viewController
var Country = []
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
#IBAction func taxiAction(sender: AnyObject) {
let opt = ["1","2","3","4","5"]
Country = opt
performSegueWithIdentifier("viewPlaceSegu", sender: sender)
}
...
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// get a reference to the second view controller
if segue.identifier == "viewPlaceSegu" {
if let secondViewController = segue.destinationViewController as? TableViewPlace {
// set a variable in the second view controller with the String to pass
secondViewController.tnt = Country as! [String]
}
}
}
////
http://i.stack.imgur.com/LKrN7.jpg
I tried so but didn't realize problem .
Does anyone know about this? :)
A likely cause is that you have connected the segue from the button to the new ViewController in the storyboard. When the button is pressed it will load the segue created in storyboard as well as the one created programatically.
If this is the cause then you would just need to delete the storyboard segue and create a new one from the ViewController rather than from the button.