Cancel button only clears the recipient number and does not return back to app? - swift

The cancel button only clears the recipient phone number but does not go back to the app? Any help? Here is the code I'm currently using.....
import UIKit
import MessageUI
class ViewController: UIViewController, MFMessageComposeViewControllerDelegate {
#IBOutlet weak var txtMsg: UITextField!
#IBOutlet weak var txtPhone: UITextField!
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 sndSMS(sender: AnyObject) {
txtMsg.resignFirstResponder()
txtPhone.resignFirstResponder()
let msgVC = MFMessageComposeViewController()
msgVC.body = txtMsg.text!
msgVC.recipients = ["1-206-724-8288"]
msgVC.messageComposeDelegate = self;
self.presentViewController(msgVC, animated: true, completion: nil)

You're assigning the delegate but you are not implementing the method listed here:
https://developer.apple.com/documentation/messageui/mfmessagecomposeviewcontrollerdelegate/1614061-messagecomposeviewcontroller
func messageComposeViewController(_ controller: MFMessageComposeViewController,
didFinishWithResult result: MessageComposeResult)

Related

'Storyboard doesn't contain a view controller with identifier 'TimeController'' error in Swift

I am trying to send an Int from one view controller to another in swift, but I am getting this error. There is another post on this same topic, but the advice given is to 'clean' the project which does not work for me. Here is my code:
First view controller:
import UIKit
class UserInput: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
var myInt = Int()
override func viewDidLoad() {
super.viewDidLoad()
myInt = 5
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func submit(_ sender: Any) {
let myVC = storyboard?.instantiateViewController(withIdentifier: "TimeController") as! TimeController
myVC.intPassed = myInt
navigationController?.pushViewController(myVC, animated: true)
}
}
Second view controller:
import Foundation
import UIKit
class TimeController: UIViewController {
var intPassed = Int()
override func viewDidLoad() {
super.viewDidLoad()
print(intPassed)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Ensure you have inserted TimeController as the Storyboard ID in your storyboard.

textField to LabelView live by swift 2 [duplicate]

This question already has answers here:
UITextField text change event
(22 answers)
Closed 6 years ago.
how can i what ever i type in textField immediately show it in labelView without button that do it ?
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var labelView: UILabel!
#IBOutlet weak var textViewout: UITextField!
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.
}
}
It's best to use UITextField's delegate:
class ViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var labelView: UILabel!
#IBOutlet weak var textViewout: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
textViewout.delegate = self
}
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
labelView.text = textViewout.text
return true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
EDIT:
Sorry, it actually is not exactly the best solution. It will be delayed. This should work more efficiently for you:
class ViewController: UIViewController {
#IBOutlet weak var labelView: UILabel!
#IBOutlet weak var textViewout: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
textViewout.addTarget(self, action: "updateLabel", forControlEvents: .EditingChanged)
}
func updateLabel() {
labelView.text = textViewout.text
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

How to send my UITextfield to a variable via an UIButton?

I want to know how can I type something in my UITextfield and then send it to a variable via an UIButton?
When I try it with this code the app automatically crashes and show me this error:
Thread:1 signal SIGABRT
Here's the code:
import UIKit
import Foundation
class ViewController: UIViewController {
#IBOutlet weak var LabelHeure: UILabel!
#IBOutlet weak var Heure: UITextField!
#IBAction func Send(sender: AnyObject) {
var one = String(Heure.text)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
LabelHeure.text = NSDateFormatter.localizedStringFromDate(NSDate(),dateStyle:NSDateFormatterStyle.MediumStyle,timeStyle: NSDateFormatterStyle.NoStyle)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
You don't need to cast Heure.text as a string because it is already a string.
Here is the code I used and it worked. After pressing the button, the variable is updated with the text from the textfield. I used a println to verify.
import UIKit
import Foundation
class ViewController: UIViewController {
#IBOutlet weak var LabelHeure: UILabel!
#IBOutlet weak var Heure: UITextField!
#IBAction func Send(sender: AnyObject) {
var myVariable = Heure.text
println(myVariable)
}
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.
}
}

Swift: Updating a label from a second view controller

I have two view controllers. I also have a universal variable called number. The first view controller has a label on it called mainLabel. My second view controller has a button on it. When the button is pressed it should subtract 200 from the variable number then update the mainLabel label. I can not figure out how to make mainLabel a label that works on the second view controller too.
First View Controller
import UIKit
var number:Int = 0
class ViewController: UIViewController {
#IBOutlet weak var mainLabel: UILabel!
#IBAction func backgroundButton(sender: AnyObject) {
number = number + 1
mainLabel.text = "\(number)"
NSUserDefaults.standardUserDefaults().setObject(number, forKey: "number")
}
override func viewDidLoad() {
super.viewDidLoad()
if NSUserDefaults.standardUserDefaults().objectForKey("number") != nil {
number = NSUserDefaults.standardUserDefaults().objectForKey("number") as! Int
}
mainLabel.text = "\(number)"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Second View Controller
import UIKit
class SecondViewController: UIViewController {
#IBOutlet weak var buy1Label: UILabel!
#IBAction func buy1(sender: AnyObject) {
number = number - 200
buy1Label.text = "Bought!"
mainLabel.text = "\(number)"
}
}
class ViewController: UIViewController {
#IBOutlet weak var mainLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
NSNotificationCenter.defaultCenter().addObserverForName("NSNotificationName", object: nil, queue: nil) { (note) -> Void in
// Number changed, update your UILabel.
var number: Int = NSUserDefaults.standardUserDefaults().integerForKey("keyOfNumber")
self.mainLabel.text = "\(number)"
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func subtractNumberBy200() {
var number: Int = NSUserDefaults.standardUserDefaults().integerForKey("keyOfNumber")
number -= 200
NSUserDefaults.standardUserDefaults().setInteger(number, forKey: "keyOfNumber")
}
}
Hope this will works for you.
Wow, this question cuts to the core of what I have to deal with every day. Here is my first take on how to do this with notifications.
First, setup some common global things.
let NumberDidChangeNote = "NumberDidChangeNote" // A name for the notification.
// A function to get the value of number the same way every time.
func number() -> Int {
return NSUserDefaults.standardUserDefaults().integerForKey("number");
}
// A function to set the value of number the same way every time.
func setNumber(number: Int) {
NSUserDefaults.standardUserDefaults().setInteger(number, forKey: "number")
NSUserDefaults.standardUserDefaults().synchronize()
// Post a number did change notification
NSNotificationCenter.defaultCenter().postNotificationName(NumberDidChangeNote, object: nil)
}
The first view controller splits the roles of setting number and setting the label.
class ViewController: UIViewController {
#IBOutlet weak var mainLabel: UILabel!
#IBAction func backgroundButton(sender: AnyObject) {
setNumber(number() + 1)
}
dynamic func numberDidChange(note: NSNotification) {
mainLabel.text = "\(number())"
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
// Add an observer which will call numberDidChange() anytime the number changes.
let center = NSNotificationCenter.defaultCenter()
center.addObserver(self, selector: "numberDidChange", name: NumberDidChangeNote, object: nil)
// Fake an initial notification to numberDidChange() to set the initial value of the label.
numberDidChange(NSNotification(name: NumberDidChangeNote, object: nil))
}
override func viewWillDisappear(animated: Bool) {
// Remove the observer when not on the screen.
let center = NSNotificationCenter.defaultCenter()
center.removeObserver(self, name: NumberDidChangeNote, object: nil)
super.viewWillDisappear(animated)
}
}
Now the second view controller doesn't have to worry about the first view controller at all.
class SecondViewController: UIViewController {
#IBOutlet weak var buy1Label: UILabel!
#IBAction func buy1(sender: AnyObject) {
setNumber(number() - 200)
buy1Label.text = "Bought!"
}
}

Linking View Controllers through button

I'm a beginner at all of this...Having said that I've come across a point in my app where I've stalled and don't know what to do or fix next. So any answers would be appreciated!
So in my Home View Controller, I have four buttons with four different categories.
Each of these categories has its own question list, but they have a common "General Question" list. The general question list has its own view controller.
When you click on any of the four buttons, it brings you to the General Question view. At the bottom of this view, I have a "Next" button.
Goal: Configure the Next button to continue to one of the category's question list based on what is initially pressed in the Home View Controller.
I've connected the buttons via outlet and action in the View Controller.
However, the Next button will not connect when I control + drag into the View Controller. I'm not sure where I need to put the code for this...
I was thinking that the code for the Next button might need to have some kind of conditional statement, but since it won't connect I can't even get that far.
Help!
(This is what I have) Sample Code:
import UIKit
import AddressBookUI
import AddressBook
import Foundation
import CoreData
import CoreGraphics
import EventKit
import EventKitUI
import CoreFoundation
class ViewController: UIViewController {
#IBOutlet var ColorButton: UIButton!
#IBOutlet var StyleButton: UIButton!
#IBOutlet var CutButton: UIButton!
#IBOutlet var MakeupButton: UIButton!
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.
}
var eventstore: EKEventStore!
var event: EKEvent!
weak var editViewDelegate: EKEventEditViewDelegate!
#IBAction func ColorButtonPressed(sender: UIButton) {
}
#IBAction func StyleButtonPressed(sender: UIButton) {
}
#IBAction func HaircutButtonPressed(sender: UIButton) {
}
#IBAction func MakeupButtonPressed(sender: UIButton) {
}
}
Here is a suggested approach as shown in the code below for 2 controllers (instead of 4) for brevity. Use appropriate named segues to each of the "next processing" controllers from the common processing controller and set up a chain. Here is a link to the project file: Project file
import UIKit
class ViewController: UIViewController {
var nextVcId = 0 // defines the button that is pressed
#IBAction func unwindFromOtherControllers(segue: UIStoryboardSegue) {
// In case you want to get back to the main VC
}
#IBAction func btn2Action(sender: UIButton) {
nextVcId = 0
self.performSegueWithIdentifier("commonSegue", sender: sender)
}
#IBAction func btn1Action(sender: UIButton) {
nextVcId = 1
self.performSegueWithIdentifier("commonSegue", sender: sender)
}
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.
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let vc = segue.destinationViewController as! CommonViewController
vc.nextControllerId = nextVcId
}
}
import UIKit
class CommonViewController: UIViewController {
var nextControllerId = 0
#IBOutlet weak var StatusLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
self.StatusLabel.text = "Common"
commonProcessing()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func commonProcessing() {
// do your common processing
if nextControllerId == 0 {
performSegueWithIdentifier("next1Segue", sender: self)
} else {
performSegueWithIdentifier("next2Segue", sender: self)
}
}
}
import UIKit
class Next1ViewController: UIViewController {
#IBOutlet weak var statusLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
self.statusLabel.text = "Next1"
next1Processing()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func next1Processing() {
println("Next 1 Processing")
}
}
import UIKit
class Next2ViewController: UIViewController {
#IBOutlet weak var statusLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
statusLabel.text = "Next 2"
next2Processing()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func next2Processing() {
println("Next 2 Processing")
}
}
processing