Segue and Button programmatically swift - swift

I am using iCarousel and I have to create my own button. I want to pass data from the button made programmatically to another view, but I don't have a segue identifier because I created the button programmatically. I don't know if it is possible to create the identifier of the segue programmatically.
button.addTarget(self, action: #selector(buttonAction3), for: .touchUpInside)
button.setTitle("\(titulos[index])", for: .normal)
tempView.addSubview(button)
let myImage = UIImage(named: "modo4.png") as UIImage?
button.setImage(myImage, for: .normal)
let viewController:UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "modo") as! Modo1ViewController
self.present(viewController, animated: false, completion: nil)
if segue.identifier == "" {
if let destination = segue.destination as? Modo1ViewController {
destination.nomb = nombres
}
}

Create seuge
Assign identifier
and your button target
#IBAction func button_clicked(_ sender: UIButton) {
self.performSegue(withIdentifier: "segueToNext", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segueToNext" {
if let destination = segue.destination as? Modo1ViewController {
destination.nomb = nombres // you can pass value to destination view controller
// destination.nomb = arrayNombers[(sender as! UIButton).tag] // Using button Tag
}
}
}

in your case, if you use self.present and you want to send data between views. Try this:
let viewController:UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "modo") as! Modo1ViewController
viewController.nomb = nombres
self.present(viewController, animated: false, completion: nil)
I don't know how to set segue's identifier but I think code above can help
If you want do to easier work, you can create segue in IB (Interface Builder) and set it's identifier, then use
performSegue:withIdentifier:sender

Related

Can you change view controllers without segue?

Basically i have a button that changes title, and if the button title is something i want the button to take the user to somewhere different to if the button title was something else
let act1 = act1ViewController()
let act2 = act2ViewController()
override func viewDidLoad() {
super.viewDidLoad()
self.buttonName.setTitle(self.text, for: .normal)
// Do any additional setup after loading the view.
}
#IBAction func buttonPressed(_ sender: UIButton) {
if text == "cheer up" {
self.present(act1, animated: true, completion: nil)
}
else if text == "yay" {
self.present(act2, animated: true, completion: nil)
}
}
Instantiate the ViewController depending on the if check, and present it:
let storyBoard: UIStoryboard = UIStoryboard(name: "MyStoryboard", bundle: nil)
let destinationVC = storyBoard.instantiateViewController(withIdentifier: "ViewControllerIdentifier") as! NewViewController
present(destinationVC, animated: true, completion: nil)
Just remember to set the Identificator in the Storyboard
If the 2 controllers that you want to show are,
class Act1ViewController: UIViewController {
//your code...
}
class Act2ViewController: UIViewController {
//your code...
}
You simply need to put a switch-case on buttonName's title and present the relevant controller act1 or act2. Create act1 and act2 using the storyboard.
#IBAction func buttonPressed(_ sender: UIButton) {
switch sender.titleLabel?.text {
case "cheer up":
if let act1 = self.storyboard?.instantiateViewController(withIdentifier: "Act1ViewController") as? Act1ViewController {
self.present(act1, animated: true, completion: nil)
}
case "yay":
if let act2 = self.storyboard?.instantiateViewController(withIdentifier: "Act2ViewController") as? Act2ViewController {
self.present(act2, animated: true, completion: nil)
}
default:
break
}
}
}
Don't forget to set Act1ViewController and Act2ViewController as storyboardID of the respective controllers in the storyboard.

Swift: Pass data from a view controller to a navigation controller

I want to pass data from a view controller to another but I've a Navigation controller between them, so there's a "present" function instead a "performSegue".
private func IniciarMainAdmin(){
let mainAdmin = UIStoryboard(name: "Main", bundle: Bundle.main)
guard let mainAdminNVC = mainAdmin.instantiateViewController(withIdentifier: "NCMainAdmin") as? NCMainAdmin else{
return
}
present(mainAdminNVC, animated: true, completion: nil)
}
I've tried with this code but it didn't work for me:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destViewController = segue.destination as! NCMainAdmin
let secondViewcontroller = destViewController.viewControllers.first as! NCMenuAdmin
secondViewcontroller.adminUserData = "This is a test"
}
Notes:
NCMainAdmin is the Navigation Controller
NCMenuAdmin is the first View Controller of the Navigation Controller
Thanks!
For your code prepare(for to trigger , you need to create a segue from the sender vc to the navigation , so either
private func IniciarMainAdmin(){
let mainAdmin = UIStoryboard(name: "Main", bundle: Bundle.main)
guard let mainAdminNVC = mainAdmin.instantiateViewController(withIdentifier: "NCMainAdmin") as? NCMainAdmin else{
return
}
let first = mainAdminNVC.viewControllers.first as! NCMenuAdmin
first.adminUserData = "This is a test"
present(mainAdminNVC, animated: true, completion: nil)
}
OR
self.performSegue(withIdentifier:"id",sender:nil)
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destViewController = segue.destination as! NCMainAdmin
let secondViewcontroller = destViewController.viewControllers.first as! NCMenuAdmin
secondViewcontroller.adminUserData = "This is a test"
}

How to programmatically add uibutton action?

I have created a button and I was wondering how do I programmatically code an action for the UIButton to take me to another view controller?
This is all I have so far:
let getStartedButton: UIButton = {
let getStartedButton = UIButton()
getStartedButton.backgroundColor = UIColor(red:0.24, green:0.51, blue:0.59, alpha:1.0)
getStartedButton.setTitle("Get Started", for: .normal)
getStartedButton.titleLabel?.font = UIFont(name: "Helvetica Bold", size: 18)
getStartedButton.translatesAutoresizingMaskIntoConstraints = false
getStartedButton.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
return getStartedButton
}()
#objc func buttonAction(sender: UIButton!) {
print("...")
}
If you want to make the transition to another ViewController after pressing the button, you can do this in 2 ways:
1) present(_:animated:completion:)
#objc func buttonAction(sender: UIButton!) {
let vc = self.storyboard?.instantiateViewController(withIdentifier: "Main") as! SecondViewController
self.present(vc, animated: true, completion: nil)
}
2) pushViewController(_:animated:)
#objc func buttonAction(sender: UIButton!) {
let vc = self.storyboard?.instantiateViewController(withIdentifier: "Main") as! SecondViewController
self.navigationController?.pushViewController(vc, animated: true)
}
There are 3 ways you can show a new View Controller:
Presenting View Controller:
#objc func buttonAction(sender: UIButton!) {
let destinationVC = self.storyboard?.instantiateViewController(withIdentifier: "DestinationViewController") as! DestinationViewController
self.present(destinationVC, animated: true, completion: nil)
}
Performing a Segue from Storyboard:
If you already have the View Controller you want to present in your Storyboard, and it has a segue from your origin VC to your destination VC, then you can add an identifier to the segue and do this...
#objc func buttonAction(sender: UIButton!) {
self.performSegue(withIdentifier: "MySegueIdentifier", sender: self)
}
Pushing View Controller onto Stack (this only works if your original VC is embedded in a Navigation Controller):
#objc func buttonAction(sender: UIButton!) {
let destinationVC = self.storyboard?.instantiateViewController(withIdentifier: "DestinationViewController") as! DestinationViewController
self.navigationController?.pushViewController(destinationVC, animated: true)
}

Swift: Can't push to view controller programatically

I'm trying to push to different viewcontroller programatically using this code:
let loginVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LoginViewController") as! MenuController
self.navigationController?.pushViewController(loginVC, animated: true)
Here is my setup: I have swift file called: MenuController which contains this code I set the identifier name to: LoginViewController I set the class of both ViewControllers set to MenuController. But whenever I press the button to trigger the event it doesn't do anything.
Here is the rest of my code as requested:
import UIKit
class MenuController: UIViewController {
var thingy = 0
#IBAction func ThingyBttn(_ sender: UIButton) {
if(thingy == 1) {
print("ok")
}else{
print("good")
let loginVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LoginViewController") as! MenuController
self.navigationController?.pushViewController(loginVC, animated: true)
}
}
#IBOutlet var myButton: UIButton!
#IBAction func Button(_ sender: UIButton) {
if let ButtonImage = myButton.image(for: .normal),
let Image = UIImage(named: "ButtonAppuyer.png"),
UIImagePNGRepresentation(ButtonImage) == UIImagePNGRepresentation(Image) {
thingy = 1
print("1")
} else {
thingy = 2
print("2")
}
}
}
I know that everything else with the code works fine because it's printing the message.
You need to insert the vc you do the push from inside a navigationController as this
self.navigationController?
is nil , or do it programmatically
Edit:
Replace this
let loginVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LoginViewController") as! MenuController
self.navigationController?.pushViewController(loginVC, animated: true)
with
let loginVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LoginViewController") as! MenuController
let nav = UINavigationController(rootViewController:self)
(UIApplication.shared.delegate as! AppDelegate).window!.rootViewController = nav
nav.pushViewController(loginVC, animated: true)
I think your view controllers are not in Navigation Stack thats why its not pushing to next view controller
let nextVC = self.storyboard?.instantiateViewController(withIdentifier: “nextvc”) as! nextvc
let navigation = UINavigationController(rootViewController:self)
UIAppliaction.window!.rootViewController = navigation
navigation.pushViewController(nextVC, animated: true)

SWReveal TableViewController - how to launch to different ViewControllers?

I'm using the SWReveal slide out menuing system.
The menu is generated from an array, and all works fine. E.g.
arrayOfCellData = [cellData(cell : 1, text : "Angles", image : #imageLiteral(resourceName: "Angles.png")),
cellData(cell : 2, text : "Area", image : #imageLiteral(resourceName: "Area.png")),
When I tap on a particular menu option I want to segue to a different ViewController. Here is the code that I have been using which doesn't work.
The idea that Row 1 invokes segue A and 2 segue B...
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if(indexPath.row == 1 ){
let vc: AnyObject! = self.storyboard?.instantiateViewController(withIdentifier: "A")
self.performSegue(withIdentifier: "A", sender: self)
self.show(vc as! A, sender: vc)
NSLog("A")
}
if(indexPath.row == 2 ){
let vc: AnyObject! = self.storyboard?.instantiateViewController(withIdentifier: "B")
self.performSegue(withIdentifier: "B", sender: self)
self.show(vc as! B, sender: vc)
NSLog("B")
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let backItem = UIBarButtonItem()
backItem.title = " "
navigationItem.backBarButtonItem = backItem
if segue.identifier == "A", let nextScene = segue.destination as? A {
nextScene.categoryCounter = 1
}
if segue.identifier == "B", let nextScene = segue.destination as? B {
nextScene.categoryCounter = 2
}
}
With SWRevealViewController it is best to not use segues in storyboard and instead contract your reveal view controller programmatically. Try this, in your didSelectRowAtIndexPath method add the following to your code.
// Here I get the view controllers I am interested in
let frontStoryboard = UIStoryboard(name: "YourStoryboard", bundle: .main)
let frontVC = frontStoryboard.instantiateInitialViewController()
let rearStoryboard = UIStoryboard(name: "YourOtherStoryboard", bundle: .main)
let rearVC = rearStoryboard.instantiateInitialViewController()
// Construct the SWRevealViewController with your view controllers
if let revealVC = SWRevealViewController(rearViewController: rearVC, frontViewController: frontVC) {
revealVC.modalTransitionStyle = .crossDissolve // Set segue transition
present(revealVC, animated: true, completion: nil) // Segue to view controller
}