Couldn't segue to another view controller from textField - ios

I want to have the pretty simple transition from the home view where the user enters the text in the textfield, then have its segue go to the next view controller. I did implement the function textFieldShouldReturn(textField: UITextField) -> Bool which tries to perform the segue, but didn't get the desired result.
Here's the part of the code
class LogInVC: UIViewController {
#IBOutlet weak var logInLabel: UILabel!
#IBOutlet weak var logInField: UITextField!
override func viewDidLoad() {
NSLog("LogIn view loaded, setting delegate")
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewDidAppear(_ animated: Bool) {
NSLog("viewWillAppear: Performing possible queued updates")
textFieldShouldReturn(textField: logInField)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let autoCheckInVC = segue.destination as?
AutocheckInVC else {
preconditionFailure("Wrong destination type: \(segue.destination)")
}
guard logInField.text != nil else {
preconditionFailure("Text property should not be nil")
}
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
self.performSegue(withIdentifier: "AutoCheckInVC", sender: self)
return true
}
}
Any quick help for debugging this would be so greatly appreciated. I have tried quite much time fixing this, but I haven't found the way out!!!

You have a few errors in your code:
To be able to call textFieldShouldReturn you need to inherit from UITextFieldDelegate and set the delegate of your textField, logInField.delegate = self
Don´t call textFieldShouldReturn from viewDidAppear
Use the latest syntax for the method textFieldShouldReturn
You got the warning result of call to this method is unused because it was never called since you never had the delegates
Try this code instead of yours:
class LogInVC: UIViewController, UITextFieldDelegate {
#IBOutlet weak var logInLabel: UILabel!
#IBOutlet weak var logInField: UITextField!
override func viewDidLoad() {
NSLog("LogIn view loaded, setting delegate")
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
logInField.delegate = self
}
override func viewDidAppear(_ animated: Bool) {
NSLog("viewWillAppear: Performing possible queued updates")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let autoCheckInVC = segue.destination as?
ViewController else {
preconditionFailure("Wrong destination type: \(segue.destination)")
}
guard logInField.text != nil else {
preconditionFailure("Text property should not be nil")
}
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
self.performSegue(withIdentifier: "AutoCheckInVC", sender: self)
return true
}
}

Related

Swift3 shared a class instances between viewControllers

I am new to iOS programming and I am trying to build a simple calculator app, but I am stuck at connecting actions between two viewControllers. I am trying to change the value of a segmentControl of the firstView by manipulating the same control in my second view. Any suggestions how to do it? Thank you. Below are the code and screenshots of it.
code first viewController
class ViewController: UIViewController {
#IBOutlet weak var billField: UITextField!
#IBOutlet weak var totalLabel: UILabel!
#IBOutlet weak var tipLabel: UILabel!
#IBOutlet weak var tipControll: UISegmentedControl!
#IBAction func onTap(_ sender: Any) {
//dismiss the keyboard when we tap anywhere from the screen
view.endEditing(true)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func calculateTip(_ sender: AnyObject) {
//calculate
let percentage = [0.18, 0.2, 0.25]
let bill = Double(billField.text!) ?? 0
let tip = bill * percentage[tipControll.selectedSegmentIndex]
let total = bill + tip
//set the labels
tipLabel.text = String(format: "$%.2f", tip)
totalLabel.text = String(format: "$%.2f", total)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
print("view will appear")
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
print("view did appear")
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
print("View will disappear")
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
print("view did disappeared")
}
code second viewController
import UIKit
class SettingViewController: UIViewController {
#IBOutlet weak var segmentControlLabel: UISegmentedControl!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
#IBAction func onTipChanged(_ sender: Any) {
}
}
Screen
I think you should use UserDefaults to connect data because it will save your tip default for next time open app.
In second viewcontroller
override func viewDidLoad() {
super.viewDidLoad()
// Get value saved in last time here.
//NSUserDefaults.standardUserDefaults().integerForKey("DefaultTip")
}
#IBAction func onTipChanged(_ sender: Any) {
// Set value when tip changed
//NSUserDefaults.standardUserDefaults().setInteger(value, forKey: "DefaultTip")
}
In first viewcontroller
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
print("view will appear")
// Get value saved in last time here.
//NSUserDefaults.standardUserDefaults().integerForKey("DefaultTip")
}
And remember register default value
NSUserDefaults.standardUserDefaults().registerDefaults([
"DefaultTip": <your default value>
])
at AppDelegate.swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
NSUserDefaults.standardUserDefaults().registerDefaults([
"DefaultTip": <your default value>
])
return true
}

Issues passing data from text field with unwind segue

I am following the accepted answer to this question, but I am having issues passing the data from ViewControllerB (second View Controller) back to ViewController (initial View Controller). Based on the output of a few print statements I added, I think the unwind to ViewController is happening before textFieldDidEndEditing.
dataPassed from ViewControllerB: Default passed data
newValue: Default passed data
textFieldDidEndEditing: Pass this back
I was able successfully pass the data back to ViewController by creating an outlet to the button on ViewControllerB and setting dataPassed = textField.text. Another way I was able to get this to work was by changing dataReceived = sourceViewController.dataPassed to dataReceived = sourceViewController.textField.text in the unwindToThisController function. But it does not look like the poster of the accepted answer mentioned above used either or these workarounds, and I am unsure if either of these workarounds are an acceptable way to do this.
I am using Xcode 8.2.1 and Swift 3.
ViewController code (initial View Controller)
/* ViewController.swift */
import UIKit
class ViewController: UIViewController {
var dataReceived: String? {
willSet {
print("newValue: " + newValue!)
labelOne.text = newValue
}
}
#IBOutlet weak var labelOne: UILabel!
#IBAction func buttonOne(_ sender: UIButton) {
performSegue(withIdentifier: "viewNext", sender: self)
}
override func viewDidLoad() {
super.viewDidLoad()
labelOne.text = "Default passed data"
}
// Segue ViewController -> ViewControllerB
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "viewNext" {
let viewControllerB = segue.destination as! ViewControllerB
viewControllerB.dataPassed = labelOne.text
}
}
// Segue ViewController <- ViewControllerB
#IBAction func unwindToThisControllerWithSegue(sender: UIStoryboardSegue) {
if let sourceViewController = sender.source as? ViewControllerB {
print("dataPassed from ViewControllerB: " + sourceViewController.dataPassed!)
dataReceived = sourceViewController.dataPassed
//dataReceived = sourceViewController.textField.text
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
ViewControllerB code (second View Controller)
/* ViewControllerB.swift */
import UIKit
class ViewControllerB: UIViewController, UITextFieldDelegate {
var dataPassed: String?
#IBOutlet weak var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
textField.text = dataPassed
textField.delegate = self
}
// Text field delegate methods
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
func textFieldDidEndEditing(_ textField: UITextField) {
print("textFieldDidEndEditing: " + textField.text!)
dataPassed = textField.text
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
I am the asker of that question and fortunately I have a backup of that sample project and here is what I found there. This is what I did in the first view controller
var displayValue: Double{
get{
return NSNumberFormatter().numberFromString(display.text!)!.doubleValue
}
set{
display.text?="\(newValue)"
typing=false
}
}
#IBAction func unwind(segue: UIStoryboardSegue){
let foo = segue.sourceViewController as! viewControllerB
let a = foo.dataSent
displayValue = NSNumberFormatter().numberFromString(a!)!.doubleValue
}
And it's working fine. Maybe if you can share the sample project you're working on I can see why it's not working? :)

#IBAction to performSegue

I have the following:
class Settings: UIViewController {
#IBAction func CitySend(sender: AnyObject) {
self.performSegue(withIdentifier: "senddata", sender: self)
}
#IBOutlet weak var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "senddata" {
let Mainview = segue.destination as! ViewController
Mainview.label = textField.text!
}
}
}
My mainview looks like this:
class ViewController: UIViewController {
#IBOutlet var city: UILabel!
var label: String = ""
override func viewDidLoad() {
super.viewDidLoad()
city.text = label
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
im not sure, if this is how i should even do this, but to me the logic is that i only want to allow this segue to pass a string to my label in my mainview if the button is pressed (Like a save button)
However as to what i have made now, nothing happens, and it gives me the 1: signal SIGABRT error, whenever i press the button.
Use the following code:
ViewController.swift
import UIKit
class ViewController: UIViewController, SecondViewControllerProtocol{
#IBOutlet weak var dataLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func navigateToSecondViewControllerOnButtonClick(_ sender: AnyObject) {
let secondVC: ViewController2 = storyboard?.instantiateViewController(withIdentifier: "viewcont2") as! ViewController2
secondVC.delegate = self
self.navigationController?.pushViewController(secondVC, animated: true)
}
func saveData(data: String){
dataLabel.text = data
}
}
ViewController2.swift
import UIKit
protocol SecondViewControllerProtocol {
func saveData(data: String) // this function the first controllers
}
class ViewController2: UIViewController {
var delegate: SecondViewControllerProtocol?
#IBOutlet weak var dataTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func saveButtonClicked(_ sender: AnyObject) {
delegate?.saveData(data: dataTextField.text!)
}
}
Storyboard screenshot:
Please check my GitHub link below to test sample:
https://github.com/k-sathireddy/PassingDataThroughSegue
#IBAction func CitySend(sender: AnyObject) {
self.performSegue(withIdentifier: "senddata", sender: textField.text)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
switch (segue.identifier, segue.destination, sender) {
case (.Some("senddata"), let controller as ViewController, let text as String):
controller.label = text
default:
break
}
}

View Controller delegate returns nil

My code is below. When I press the 'Done' or 'Cancel' buttons, it doesn't work as I want. I did some debugging, and delegate is nil even though I set it. Please help - thanks.
class ViewController: UIViewController,EditViewControllerDelegate {
#IBOutlet weak var label: UILabel!
//页面跳转时
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "EditView" {
//通过seque的标识获得跳转目标
let controller = segue.destinationViewController as! EditViewController
//设置代理
controller.delegate = self
//将值传递给新页面
controller.oldInfo = label.text
}
}
func editInfo(controller:EditViewController, newInfo:String){
label.text = newInfo
//关闭编辑页面
controller.presentingViewController!.dismissViewControllerAnimated(true, completion: nil)
}
func editInfoDidCancer(controller:EditViewController){
//关闭编辑页面
controller.presentingViewController!.dismissViewControllerAnimated(true, completion: nil)
}
}
import UIKit
protocol EditViewControllerDelegate {
func editInfo(controller:EditViewController, newInfo:String)
func editInfoDidCancer(controller:EditViewController)
}
class EditViewController: UIViewController {
#IBOutlet weak var textField: UITextField!
var delegate:EditViewControllerDelegate?
var oldInfo:String?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
if oldInfo != nil{
textField.text = oldInfo
}
}
#IBAction func done(sender: AnyObject) {
delegate?.editInfo(self, newInfo: textField.text!)
}
#IBAction func cancel(sender: AnyObject) {
delegate?.editInfoDidCancer(self)
}
}
Try this,
class ViewController: UIViewController,EditViewControllerDelegate {
var controller: EditViewController?
#IBOutlet weak var label: UILabel!
//页面跳转时
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "EditView" {
//通过seque的标识获得跳转目标
controller = segue.destinationViewController as! EditViewController
//设置代理
controller.delegate = self
//将值传递给新页面
controller.oldInfo = label.text
}
}
func editInfo(controller:EditViewController, newInfo:String){
label.text = newInfo
//关闭编辑页面
controller.presentingViewController!.dismissViewControllerAnimated(true, completion: nil)
}
func editInfoDidCancer(controller:EditViewController){
//关闭编辑页面
controller.presentingViewController!.dismissViewControllerAnimated(true, completion: nil)
}
}
import UIKit
protocol EditViewControllerDelegate {
func editInfo(controller:EditViewController, newInfo:String)
func editInfoDidCancer(controller:EditViewController)
}
class EditViewController: UIViewController {
#IBOutlet weak var textField: UITextField!
var delegate:EditViewControllerDelegate?
var oldInfo:String?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
if oldInfo != nil{
textField.text = oldInfo
}
}
#IBAction func done(sender: AnyObject) {
delegate?.editInfo(self, newInfo: textField.text!)
}
#IBAction func cancel(sender: AnyObject) {
delegate?.editInfoDidCancer(self)
}
}
I’m not able to tell from your code how you are handling the opening of EditViewController from your ViewController.
I’m guessing that your prepareForSegue:sender: is not getting called resulting in the delegate not getting set. To fix this, you will need to add a performSegueWithIdentifier:sender: call at the point where the segue needs to happen as in
self.performSegueWithIdentifier("EditView", sender: self)
You should replace whatever code is performing the opening of EditViewController with that call.
I have an app the uses showViewController:sender: to open up a second view controller from the first one even though there is a Show segue defined in my storyboard. The segue in my storyboard doesn’t get used in that case and prepareForSegue:sender: is never called as a result.
If I replace my
showViewController(myVC, sender: self)
with
performSegueWithIdentifier("mySegue", sender: self)
then prepareForSegue:sender: will get called. If I am setting a delegate, like you are, in prepareForSegue:sender:, the delegate will get set before the segue happens.

Swift: Segue into a UITableView Search

I want to segue into a UITableView Search. I already have the uitableview and search bar implemented and working. Now I want to add a button, that when clicked, opens up the search view with the keyboard out. How can I implement this?
Challenge: How can I put this button on separate view controller?
I have looked up ways to segue into a search bar, but all I have found are ways to segue out of a search.
My Code:
/*Search*/
var searched: Bool = false
#IBOutlet weak var searchBarReal: UISearchBar!
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
getSortedSectionList()
tableView.reloadData()
if searched {
searchBarReal.becomeFirstResponder()
searched = false
}
}
override func viewDidLoad() {
super.viewDidLoad()
let nav = self.navigationController?.navigationBar
nav?.backgroundColor = UIColor.whiteColor()
nav?.tintColor = UIColor.whiteColor()
setUpIcons()
searchBarReal.delegate = self
}
You can tell your searchBar to becomeFirstResponder.
code example:
Initial View Controller
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func bttnTouched(sender: AnyObject) {
performSegueWithIdentifier("next", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "next" {
let nextVc = segue.destinationViewController as! NextViewController
nextVc.shouldSearchBarRespond = true
}
}
}
Next View Controller
import UIKit
class NextViewController: UIViewController, UISearchBarDelegate {
#IBOutlet weak var searchBar: UISearchBar!
var shouldSearchBarRespond: Bool?
override func viewDidLoad() {
super.viewDidLoad()
searchBar.delegate = self
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(animated: Bool) {
if shouldSearchBarRespond == true {
searchBar.becomeFirstResponder()
}
}
}

Resources