Unable to perform segue from UIView connected to XIB - swift

This is UIView file's owner of XIB
import UIKit
class sideBarContent: UIView {
#IBOutlet weak var userName: UILabel!
#IBOutlet var sideBarContentView: UIView!
let mapviewcontroller = MapViewController()
#IBAction func userProfile(_ sender: UIButton) {
mapviewcontroller.performSegueFromView(stringFromView: "userprofile")
}
Whereas inside my UIviewcontroller
class MapViewController: UIViewController
{
func performSegueFromView(stringFromView:String){
performSegue(withIdentifier: "\(stringFromView)", sender: nil)
}
}
It says that my segue identifier does not exist, please help, i'm quite lost. And i couldn't perform segue in UIView as well, and i tried to use storyboard ID method too, but the present(vc,animated:true,completion:nil) not showing up.

First things first, I think you should not access the view controller from your view. It is better to model the controller logic in the view controller rather than in the view.
That said, you can use a delegate pattern to implement your functionality.
import UIKit
class SideBarContent: UIView {
#IBOutlet weak var userName: UILabel!
#IBOutlet var sideBarContentView: UIView!
let delegate: SideBarContentDelegate? = nil
#IBAction func userProfile(_ sender: UIButton) {
self.delegate?.performSegueFromView(withIdentifier: "userprofile")
}
}
protocol SideBarContentDelegate {
func performSegueFromView(withIdentifier identifier: String)
}
class MapViewController: UIViewController, SideBarContentDelegate {
#IBOutlet private weak var sidebarView: SideBarContent!
override func viewDidLoad() {
super.viewDidLoad()
self.sidebarView.delegate = self
}
func performSegueFromView(withIdentifier identifier: String) {
self.performSegue(withIdentifier: identifier, sender: nil)
}
}
More info : https://stackoverflow.com/a/1340470/2603900

Related

Swift transit from vc to another vc

ViewController has a label and a button for go to secondVC. And secondVC has a text field , label , button to write user-entered text in the text field on the label. When user press the back button of navigation bar, I want transit secondVC's label's text to ViewController's label's text. How can I do this ?
ViewController's code:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var nameLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func goVC2(_ sender: Any) {
performSegue(withIdentifier: "toVC2", sender: nil)
}
}
secondVC's code:
import UIKit
class secondVC: UIViewController {
#IBOutlet weak var nameField: UITextField!
#IBOutlet weak var resultLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func saveClicked(_ sender: Any) {
resultLabel.text = nameField.text
}
}
I tried prepare for segue in ViewController and but it was error. And I searched on google for this but I couldn't find solution.
Use protocol for pass data from second VC to first VC. you need to create your own delegate method and call on dismiss with pass data, check below code :----
ViewController's code:
import UIKit
class ViewController: UIViewController, Delegate {
#IBOutlet weak var nameLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
func save(str : String) {
self.nameLabel.text = str
}
#IBAction func goVC2(_ sender: Any) {
if let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "secondVC") as? secondVC {
vc.delegate = self
self.present(vc, animated: true, completion: nil)
}
}
}
secondVC's code:
import UIKit
protocol Delegate : AnyObject {
func save(str : String)
}
class secondVC: UIViewController {
#IBOutlet weak var nameField: UITextField!
#IBOutlet weak var resultLabel: UILabel!
weak var delegate : Delegate?
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func saveClicked(_ sender: Any) {
if let del = self.delegate {
let txt = nameField.text
del.save(str: txt)
self.dismiss(animated: true, completion: nil)
}
}
}

Global View visible in all .xib ViewControllers

I'm trying to create a "Global View" maybe it goes into AppDelegate, but I'm not sure. I would like my View to always be visible, no matter what ViewController .xib you are on in the app.
For now I have inserted the View in the first ViewController .xib but when I navigate in the other ViewControllers the View disappears.
This is the code:
import UIKit
class MonitoringViewController: UIViewController {
#IBOutlet var contentView: UIView!
#IBOutlet weak var viewForTab: UIView!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func buttonTab1(_ sender: UIButton) {
}
#IBAction func buttonTab2(_ sender: UIButton) {
delegate?.navigateToAlertViewController()
let alert = AlertViewController(nibName: "AlertViewController", bundle: nil)
contentView.addSubview(alert.view)
alert.didMove(toParent: self)
}
#IBAction func buttonTab3(_ sender: UIButton) {
}
}

How to make a protocol for UIView which performs a segue

I have added a protocol that makes a segue from a UIView button to a NavigationController but when i compile and press the button after the UIView opens it just does nothing which seems pretty weird since it all makes sense to me at least, Please if I am doing anything wrong lead me to fix my code.
I got this code from this Answer: https://stackoverflow.com/a/52789290/18277261
Code:
class popUpView: UIView, UITextFieldDelegate {
#IBOutlet weak var popUpViewInterface: UIView!
#IBOutlet weak var payNowPopUp: UIButton!
let delegate: popUpViewDelegate? = nil
#IBAction func payNowToWebView(_ sender: Any) {
let myColor = UIColor.red
priceTextFieldPopUp.layer.borderColor = myColor.cgColor
priceTextFieldPopUp.layer.borderWidth = 1.0
self.delegate?.performSegueFromView(withIdentifier: "paymentWebView")
}
}
protocol popUpViewDelegate {
func performSegueFromView(withIdentifier identifier: String)
}
class dashboardViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource, ChartViewDelegate, AxisValueFormatter, popUpViewDelegate {
func performSegueFromView(withIdentifier identifier: String) {
self.performSegue(withIdentifier: identifier, sender: nil)
}
In dashboardViewController where you initialise popUpView make sure that you set the delegate like below.
let popView = popUpView()
popView.delegate = self

Using Delegation to Pass Data from the Initial View Controller to a Second View Controller - Swift

I am trying to understand delegation in Swift better, and I have seen how delegation can be used to pass data from a second view controller back to an initial view controller. But I am confused about how to pass data from the initial view to a second view using delegation. I have initialized an instance of the delegate in my initial view and implement the delegate protocol in the second view, but I am still unsuccessful in passing the data from initial view to second view using delegation. Any advice on what I might be doing wrong would be appreciated! Here is the storyboard view of what my delegation attempt looks like. 1
Initial View Class:
import UIKit
class ViewController: UIViewController {
var data: DataDelegate?
#IBOutlet weak var text: UITextField!
#IBOutlet weak var send: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func send(_ sender: Any) {
let msg = text.text!
data?.sendData(data: msg)
print("SENDING MESSAGE: \(msg)")
}
}
protocol DataDelegate {
func sendData(data: String)
}
Second View Class:
import UIKit
class RVC: UIViewController, DataDelegate {
#IBOutlet weak var delegationLabel: UILabel!
#IBOutlet weak var message: UILabel!
#IBOutlet weak var back: UIButton!
let vc = ViewController()
func sendData(data: String) {
print("DATA SENT")
let msg = data
print(msg)
message.text = data
}
override func viewDidLoad() {
super.viewDidLoad()
print("LOADED")
vc.data = self
// Do any additional setup after loading the view.
}
}

How to update UITableView when a CustomCell label value changes?

I have a custom cell class with two buttons and one label defined in its own class. Am using protocol-delegates to update the value of the label when the button is pressed. But am not able to figure out how to update the UITableView.
protocol customCellDelegate: class {
func didTapDecrementBtn(_ sender: AllCountersTableViewCell)
func didTapIncrementBtn(_ sender: AllCountersTableViewCell)
func updateTableViewCell(_ sender: AllCountersTableViewCell)
}
class AllCountersTableViewCell: UITableViewCell {
#IBOutlet weak var counterValueLbl: UILabel!
#IBOutlet weak var counterNameLbl: UILabel!
#IBOutlet weak var counterResetDateLbl: UILabel!
#IBOutlet weak var counterDecrementBtn: UIButton!
#IBOutlet weak var counterIncrementBtn: UIButton!
weak var delegate: customCellDelegate?
#IBAction func decrementBtnPressed(_ sender: UIButton) {
delegate?.didTapDecrementBtn(self)
delegate?.updateTableViewCell(self)
}
#IBAction func incrementBtnPressed(_ sender: UIButton) {
delegate?.didTapIncrementBtn(self)
delegate?.updateTableViewCell(self)
}
In my ViewController, I have provided the delegate. But reloadData() is not working, so although sender.counterLbl.value is changing its not reflecting on the view.
extension AllCountersVC: customCellDelegate {
func didTapIncrementBtn(_ sender: AllCountersTableViewCell) {
guard let tappedCellIndexPath = tableView.indexPath(for: sender) else {return}
let rowNoOfCustomCell = tappedCellIndexPath[1]
let newValue = String(allCounter[rowNoOfCustomCell].incrementCounterValue(by: allCounter[rowNoOfCustomCell].counterIncrementValue))
sender.counterValueLbl.text = newValue
}
func updateTableViewCell(_ sender: AllCountersTableViewCell) {
allCountersTableView.reloadData()
}
In cellForRow you must set cell.delegate = self for this logic to start working. Its not enough just to make you controller confroms to your custom cell delegate protocol. From your post I assume delegate is always nil in the cell, that is why it does not work.