Data between ContainerView and ViewController - ios

I want to pass data between my container view and my viewcontroller. Then, I click on the button the label text should be change. The button is in the container view and the label text in the viewcontroller. I tried some solutions here on Stack Overflow but nothing seems to working, or I'm doing it wrong maybe.
Can anyone help?
Gamecontroller.swift
#objc func buttonMove(_ sender:UIButton!) {
print("Button tapped" + String(pointsGame))
let buttonWidth = sender.frame.width;
let buttonHeight = sender.frame.height;
let viewWidth = sender.superview!.bounds.width
let viewHeight = sender.superview!.bounds.height
let xwidth = viewWidth - buttonWidth
let yheight = viewHeight - buttonHeight
let xoffset = CGFloat(arc4random_uniform(UInt32(xwidth)))
let yoffset = CGFloat(arc4random_uniform(UInt32(yheight)))
sender.center.x = xoffset + buttonWidth / 2
sender.center.y = yoffset + buttonHeight / 2
pointsGame = pointsGame + 1
}
override func prepare(for segue: UIStoryboardSegue,sender: Any?) {
if segue.identifier == "game" {
let vc = segue.destination as! ViewController
vc.gpoints = pointsGame
}
View controller:
class ViewController: UIViewController {
#IBOutlet weak var points: UILabel!
var gpoints: Int!
override func viewDidLoad() {
super.viewDidLoad()
gpoints = 0
points.text = String(gpoints)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

Gamecontroller.swift
////// declare object first
var vc = ViewController()
#objc func buttonMove(_ sender:UIButton!) {
print("Button tapped" + String(pointsGame))
let buttonWidth = sender.frame.width;
let buttonHeight = sender.frame.height;
let viewWidth = sender.superview!.bounds.width
let viewHeight = sender.superview!.bounds.height
let xwidth = viewWidth - buttonWidth
let yheight = viewHeight - buttonHeight
let xoffset = CGFloat(arc4random_uniform(UInt32(xwidth)))
let yoffset = CGFloat(arc4random_uniform(UInt32(yheight)))
sender.center.x = xoffset + buttonWidth / 2
sender.center.y = yoffset + buttonHeight / 2
pointsGame = pointsGame + 1
vc.refreshView((points : pointsGame)
}
override func prepare(for segue: UIStoryboardSegue,sender: Any?) {
if segue.identifier == "game" {
vc = segue.destination as! ViewController
vc.gpoints = pointsGame
}
View controller:
class ViewController: UIViewController {
#IBOutlet weak var points: UILabel!
var gpoints: Int!
override func viewDidLoad() {
super.viewDidLoad()
gpoints = 0
}
func refreshView(points : Int! )
{
points.text = String(points)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

Its not a good Approach to use viewController globally, Best way is to use delegates or notifications.Here is notification example when you Tap on button just post notification after pointsGame = pointsGame + 1
// Post notification
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "buttonpressed"), object: nil , userInfo: ["points": 20])
And in View controller ViewDidLoad add observer for notification
let name = Notification.Name("buttonpressed")
NotificationCenter.default.addObserver(self, selector: #selector(refreshLabel(object:)), name: name, object: nil)
func refreshLabel(_ notification: Notification) {
if let myDict = notification.object as? [String: Any] {
if let point = myDict["points"] as? Int {
print(point)
points.text = String(point)
}
}

Step 1: Create One Variable of your container view controller.
var templateVC : GPTemplateVC?
Step 2: In Prepare for a segue do this.
if segue.identifier == "embedSegueTemplateVC" {
templateVC = segue.destination as? GPTemplateVC
}
Then you can access every element from the ContainerVC.
You can also use a delegate for the same.
Just confirm your delegate with your main view controller and call delegate method using delegate object in Container view controller. In your case call it on button click.

Related

How to pass array of data between UIViewControllers from #objc function

I have a Child UICollectionViewController where I have an array of images.
When I delete any photo I want to send back that array of updated images to Parent UIViewController.
Also in Child controller I have a programatically view which is called when I click on any image to expand it. When the image is expanded the user can click on a Delete button to delete photos from that array.
My array is updated correctly after delete but I can't manage to send it back to parent for some reasons.
I tried to send it back using Delegates and Protocols.
Here is my code for child controller:
protocol ListImagesDelegate {
func receiveImagesUpdated(data: [String]?)
}
class ListImagesVC: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
// Properties
var receivedImagesPath: [String]? = []
var fullscreenImageView = UIImageView()
var indexOfSelectedImage = 0
var imagesAfterDelete: [String]? = []
var delegate: ListImagesDefectDelegate?
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
print("imagesAfterDelete: \(imagesAfterDelete ?? [])") // I'm getting the right number of images in this array.
delegate?.receiveImagesUpdated(data: imagesAfterDelete)
}
...
...
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("Click on photo: \(indexPath.item + 1)")
if let imagePath = receivedImagesPath?[indexPath.item] {
guard let selectedImage = loadImageFromDiskWith(fileName: imagePath) else {return}
setupFullscreenImageView(image: selectedImage)
indexOfSelectedImage = indexPath.item
}
}
private func setupFullscreenImageView(image: UIImage){
fullscreenImageView = UIImageView(image: image)
fullscreenImageView.frame = UIScreen.main.bounds
fullscreenImageView.backgroundColor = .black
fullscreenImageView.contentMode = .scaleAspectFit
fullscreenImageView.isUserInteractionEnabled = true
self.view.addSubview(fullscreenImageView)
self.navigationController?.isNavigationBarHidden = true
self.tabBarController?.tabBar.isHidden = true
let deleteButton = UIButton(frame: CGRect(x: fullscreenImageView.bounds.maxX - 50, y: fullscreenImageView.bounds.maxY - 75, width: 30, height: 40))
deleteButton.autoresizingMask = [.flexibleLeftMargin, .flexibleBottomMargin]
deleteButton.backgroundColor = .black
deleteButton.setImage(UIImage(named: "trashIcon"), for: .normal)
deleteButton.addTarget(self, action: #selector(deleteButtonTapped), for: .touchUpInside)
fullscreenImageView.addSubview(deleteButton)
}
#objc func deleteButtonTapped(button: UIButton!) {
print("Delete button tapped")
receivedImagesPath?.remove(at: indexOfSelectedImage)
imagesAfterDelete = receivedImagesPath
collectionView.reloadData()
self.navigationController?.isNavigationBarHidden = false
self.tabBarController?.tabBar.isHidden = false
fullscreenImageView.removeFromSuperview()
}
Here is the Parent controller:
var updatedImages: [String]? = []
...
...
extension NewAlbumVC: ListImagesDelegate {
func receiveImagesUpdated(data: [String]?) {
print("New array: \(data ?? [])") // This print is never called.
updatedImages = data
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToImages" {
let listImagesVC = segue.destination as! ListImagesVC
listImagesVC.delegate = self
}
}
}
I want to specify that my child controller have set a Storyboard ID ("ListImagesID") and also a segue identifier from parent to child ("goToImages"). Can cause this any conflict ?
Thanks if you read this.
It appears that the delegate is nil here
delegate?.receiveImagesUpdated(data: imagesAfterDelete)
For this
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
to trigger you must have
self.performSegue(withIdentifier:"goToImages",sender:nil)
Edit: This
let listImagesDefectVC = storyboard?.instantiateViewController(withIdentifier: "ListImagesDefectID") as! ListImagesDefectVC
listImagesDefectVC.receivedImagesPath = imagesPath
navigationController?.pushViewController(listImagesDefectVC, animated: true)
doesn't trigger prepareForSegue , so add
listImagesDefectV.delegate = self
So finally
Solution 1 :
#objc func tapOnImageView() {
let listImagesDefectVC = storyboard?.instantiateViewController(withIdentifier: "ListImagesDefectID") as! ListImagesDefectVC
listImagesDefectVC.receivedImagesPath = imagesPath
listImagesDefectVC.delegate = self
navigationController?.pushViewController(listImagesDefectVC, animated: true)
}
Solution 2 :
#objc func tapOnImageView() {
self.performSegue(withIdentifier:"goToImages",sender:nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToImages" {
let listImagesVC = segue.destination as! ListImagesVC
listImagesVC.receivedImagesPath = imagesPath
listImagesVC.delegate = self
}
}

Swift 4 start with custom ViewController

I have chatting app. I decide to create SlideShow tutorial for it. Now I have problem. How can I run TutorialVC just once when user install the app?
Usually app starts with AuthVC. Now I want to run tutorialVC just once, and then when user close app and run it again, from auth like usually.
My tutorial VC:
class TutorialViewController: UIViewController, UIScrollViewDelegate {
#IBAction func understandButtonAction(_ sender: Any) {
}
#IBOutlet weak var understandButton: UIButton!
#IBOutlet weak var tutorialPageControl: UIPageControl!
#IBOutlet weak var tutorialScrollView: UIScrollView!
var images: [String] = ["1","2","3","4"]
var frame = CGRect(x: 0, y: 0, width: 0, height: 0)
override func viewDidLoad() {
super.viewDidLoad()
setup()
addSlider()
setupButton()
}
//===============================
//EVTAuthorizationViewController
//===============================
override func viewWillAppear(_ animated: Bool) {
UIApplication.shared.keyWindow?.windowLevel = UIWindowLevelStatusBar
}
override func viewWillDisappear(_ animated: Bool) {
UIApplication.shared.keyWindow?.windowLevel = UIWindowLevelNormal
}
//AddButton
func setupButton(){
understandButton.layer.cornerRadius = 20
}
#IBAction func buttonAction(_ sender: Any?) {
print("Successful")
}
//ScrollBars
func setup(){
self.understandButton.isHidden = true
tutorialScrollView.showsHorizontalScrollIndicator = false
tutorialScrollView.showsVerticalScrollIndicator = false
}
//ScrollView method
//=============================
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
var pageNumber = scrollView.contentOffset.x / scrollView.frame.size.width
tutorialPageControl.currentPage = Int(pageNumber)
if tutorialPageControl.currentPage == 3{
self.understandButton.isHidden = false
}else{
self.understandButton.isHidden = true
}
}
//Addslider with photo
func addSlider(){
tutorialPageControl.numberOfPages = images.count
for index in 0..<images.count{
let xPos = self.view.frame.size.width * CGFloat(index)
frame.origin.x = tutorialScrollView.frame.size.width * CGFloat(index)
//frame.size = view.frame.size
let imageView = UIImageView(frame: CGRect(x: xPos, y: 0, width: self.view.frame.width, height: self.view.frame.size.height))
imageView.image = UIImage(named: images[index])
imageView.contentMode = .scaleAspectFill
self.tutorialScrollView.addSubview(imageView)
}
tutorialScrollView.contentSize = CGSize(width: (view.frame.size.width * CGFloat(images.count)), height: view.frame.size.height)
tutorialScrollView.delegate = self
}
}
Use userDefaults. I suppose the understandButton is the button the user hits to skip the tutorial, so when when it's tapped set a true bool value for a key that you are going to use, here I've chosen "tutorial presented":
#IBAction func understandButtonAction(_ sender: Any) {
UserDefaults.standard.set(true, forKey: "tutorial presented")
}
and when the app launches, in the AppDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
let window = (UIApplication.shared.delegate as! AppDelegate).window
let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
if UserDefaults.standard.bool(forKey: "tutorial presented") == true {
let controller = storyboard.instantiateViewController(withIdentifier: "Your Navigation controller name")
window?.rootViewController = tutorialViewController()
} else {
let tutorial = storyboard.instantiateViewController(withIdentifier: "Your tutorial controller name")
window?.rootViewController = tutorial
}
window?.makeKeyAndVisible()
return true
}
You can use a flag and store it via NSUserDefaults.
extension UserDefaults {
private static let didLaunchAppOnceKey = "didLaunchAppOnce"
var didLaunchAppOnce: Bool {
get { return bool(forKey: UserDefaults.didLaunchAppOnceKey) }
set { set(newValue, forKey: UserDefaults.didLaunchAppOnceKey) }
}
}
Then before presenting your view controller, check if the flag is set:
if !UserDefaults.standard.didLaunchAppOnce {
// Set the flag to true, so on next launch, we won't enter in the if again
UserDefaults.standard.didLaunchAppOnce = true
// Present your VC
…
}

Label text not updating after scroll event

I'm trying to update text of a label after a scroll event. I have a print command that prints the correct value but the label is not updating.
Here's my code
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
let x = scrollView.contentOffset.x
let w = scrollView.bounds.size.width
let p = Int(x/w)
print("page \(p)") // this prints correct value
self.signalLabel.text = signalText[Int(x/w)] // this does not update
}
what's the deal?
Here's the complete view controller code. This view is called from a button click on the initial view controller. This view contains a UIScrollView and UIPageControl. The UIScrollView contains two images that can be scrolled back and forth. I want to update the label text based on image that is shown.
import UIKit
class SignalOneViewController: UIViewController, UIScrollViewDelegate {
// MARK: Properties
#IBOutlet weak var signalScrollView: UIScrollView!
#IBOutlet weak var signalPageControl: UIPageControl!
#IBOutlet weak var signalLabel: UILabel!
// MARK: - Button Actions
#IBAction func signalOneButton(_ sender: Any) {
print("signal one button clicked")
performSegue(withIdentifier: "SignalOneSegue", sender: self)
}
#IBAction func onCancelButton(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
let signalImages = ["signal1a.png", "signal1b.png"]
let signalText = ["Ready for play", "Untimed down"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewDidLayoutSubviews() {
self.loadScrollView()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func loadScrollView() {
let pageCount : CGFloat = CGFloat(signalImages.count)
signalLabel.text = signalText[0]
signalScrollView.backgroundColor = UIColor.clear
signalScrollView.delegate = self
signalScrollView.isPagingEnabled = true
signalScrollView.contentSize = CGSize(width: signalScrollView.frame.size.width * pageCount, height: signalScrollView.frame.size.height)
signalScrollView.showsHorizontalScrollIndicator = false
signalScrollView.showsVerticalScrollIndicator = false
signalPageControl.numberOfPages = Int(pageCount)
signalPageControl.pageIndicatorTintColor = UIColor.lightGray
signalPageControl.currentPageIndicatorTintColor = UIColor.blue
signalPageControl.addTarget(self, action: #selector(self.pageChanged), for: .valueChanged)
for i in 0..<Int(pageCount) {
print(self.signalScrollView.frame.size.width)
let image = UIImageView(frame: CGRect(x: self.signalScrollView.frame.size.width * CGFloat(i), y: 0, width: self.signalScrollView.frame.size.width, height: self.signalScrollView.frame.size.height))
image.image = UIImage(named: signalImages[i])!
image.contentMode = UIViewContentMode.scaleAspectFit
self.signalScrollView.addSubview(image)
}
}
//MARK: UIScrollView Delegate
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let viewWidth: CGFloat = scrollView.frame.size.width
// content offset - tells by how much the scroll view has scrolled.
let pageNumber = floor((scrollView.contentOffset.x - viewWidth / 50) / viewWidth) + 1
signalPageControl.currentPage = Int(pageNumber)
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
let x = scrollView.contentOffset.x
let w = scrollView.bounds.size.width
let p = Int(x/w)
print("page \(p)")
self.signalLabel.text = signalText[p]
print(">>> \(signalText[Int(x/w)])")
}
//MARK: page tag action
#objc func pageChanged() {
let pageNumber = signalPageControl.currentPage
var frame = signalScrollView.frame
frame.origin.x = frame.size.width * CGFloat(pageNumber)
frame.origin.y = 0
signalScrollView.scrollRectToVisible(frame, animated: true)
}
}
Make sure signalLabe IBOutlet is attached to your label in storyboard or xib

UIImageViews / Adding Multiple Stickers To A View.

Intro:
Functionality of adding multiple stickers/emoji to a view.
Setup:
There are 2 view controllers - one to which we're adding stickers, and another with a collectionview of stickers.
Stickers themselves are passed in arrays in 'prepareForSegue' func.
There are 2 arrays, one with just sticker images, another - with UIImageViews - stickers that were already panned, pinched and rotated.
Bug:
After adding 2nd sticker, the AddingStickersVC reappears but previous sticker isn't where we left if. It is pinched and zoomed, but not panned. Also new stickers are sticked to the first one (same frame?).
We can pinch and zoom previous stickers separately from new ones - (nope, they have their own frames), but we can't separate them.
End up having a stack of UIImageViews that takes rotating and pinching separately but pans all together.
Also, speed of panning is increasing after each additional sticker (the panning gesture is added multiple time?).
Hierarchy of views
Stickers are added to 'viewForEmoji' view (viewForImgAndEmoji).
AddingStickersVC:
#IBOutlet weak var viewForImgAndEmoji: UIView!
#IBOutlet weak var mainImg: UIImageView!
#IBOutlet weak var viewForSnapshot: UIView!
var imageData: Data!
var imageItself: UIImage!
var currentUserPostRef: FIRDatabaseReference!
var emojiImage: UIImage!
var geoFire: GeoFire!
var arrayOfEmojis = [UIImage]()
var arrayOfEmojiViews = [UIImageView]()
var n:Int = 1
override func viewDidLoad() {
super.viewDidLoad()
if imageData != nil {
let img = UIImage(data: imageData)
let fixedImg = img!.fixOrientation(img: img!)
mainImg.image = fixedImg
} else if imageItself != nil {
mainImg.image = imageItself
}
if arrayOfEmojiViews.count != 0 {
for emojiView1 in arrayOfEmojiViews {
viewForImgAndEmoji.addSubview(emojiView1)
}
}
// get image out of array.
if arrayOfEmojis.count != 0 {
for emoji in arrayOfEmojis {
let emojiView = UIImageView(image: emoji)
emojiView.tag = n
emojiView.frame = CGRect(x: 153, y: 299, width: 70, height: 70)
emojiView.isUserInteractionEnabled = true
let pan = UIPanGestureRecognizer(target: self, action: #selector(self.handlePan(recognizer:)))
pan.delegate = self
viewForImgAndEmoji.addGestureRecognizer(pan)
let pinch = UIPinchGestureRecognizer(target: self, action: #selector(self.handlePinch(recognizer:)))
pinch.delegate = self
viewForImgAndEmoji.addGestureRecognizer(pinch)
let rotate = UIRotationGestureRecognizer(target: self, action: #selector(self.handleRotate(recognizer:)))
rotate.delegate = self
viewForImgAndEmoji.addGestureRecognizer(rotate)
// check so we won't add previous emoji. only new.
if viewForImgAndEmoji.viewWithTag(n) == nil {
viewForImgAndEmoji.addSubview(emojiView)
}
n += 1
}
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if arrayOfEmojis.count != 0 {
for j in 1...n {
if var view1 = self.viewForImgAndEmoji.viewWithTag(j) as? UIImageView {
arrayOfEmojiViews.append(view1)
print("Zhenya: views frame is \(view1.frame)")
}
}
}
if segue.identifier == "EmojiCollectionVC" {
if let emojiCollection = segue.destination as? EmojiCollectionVC{
if let image = sender as? UIImage {
emojiCollection.userImage = image
if arrayOfEmojis.count != 0 {
//arrayToStoreEmojis
emojiCollection.arrayToStoreEmojis = arrayOfEmojis
emojiCollection.arrayToStoreEmojiViews = arrayOfEmojiViews
}
}
}
}
}
#IBAction func handlePan(recognizer: UIPanGestureRecognizer) {
let translation = recognizer.translation(in: self.viewForImgAndEmoji)
if let view = recognizer.view {
view.center = CGPoint(x:view.center.x + translation.x,
y:view.center.y + translation.y)
}
recognizer.setTranslation(CGPoint.zero, in: self.viewForImgAndEmoji)
}
#IBAction func handlePinch(recognizer: UIPinchGestureRecognizer) {
let pinchPoint = recognizer.location(in: viewForImgAndEmoji)
let ourEmojiView = viewForImgAndEmoji.hitTest(pinchPoint, with: nil)
ourEmojiView!.transform = ourEmojiView!.transform.scaledBy(x: recognizer.scale, y: recognizer.scale)
recognizer.scale = 1
}
#IBAction func handleRotate(recognizer: UIRotationGestureRecognizer){
let rotatePoint = recognizer.location(in: viewForImgAndEmoji)
let ourEmojiView = viewForImgAndEmoji.hitTest(rotatePoint, with: nil)
ourEmojiView!.transform = ourEmojiView!.transform.rotated(by: recognizer.rotation)
recognizer.rotation = 0
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
And EmojiCollectionVC:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath) as! EmojiCollectionCell
let chosenEmoji = cell.emojiView.image as UIImage!
arrayToStoreEmojis.append(chosenEmoji!)
performSegue(withIdentifier: "backToEmojiVC", sender: arrayToStoreEmojis)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "backToEmojiVC"{
if let destinationVC = segue.destination as? EmojiVC {
if let array = sender as? [UIImage] {
destinationVC.arrayOfEmojis = arrayToStoreEmojis
destinationVC.arrayOfEmojiViews = arrayToStoreEmojiViews
let data = UIImagePNGRepresentation(userImage)
destinationVC.imageData = data
}
}
}
}
Found solution.
There reason why all new imageViews were stacked - because after panning imageViews didn't change their location in view. The whole f view was moving.
(to find this, spent 8hours tracking changes of frame origins with 'prints' at every step of the program).
And the reason the whole view was moving - because panning gesture was added to the whole view.
So instead of
let pan = UIPanGestureRecognizer(target: self, action: #selector(self.handlePan(recognizer:)))
pan.delegate = self
viewForImgAndEmoji.addGestureRecognizer(pan)
i needed:
let pan = UIPanGestureRecognizer(target: self, action: #selector(self.handlePan(recognizer:)))
pan.delegate = self
emojiView.addGestureRecognizer(pan)
What is interesting, for pinching and rotating - we still add them to the whole view that contains emojiView:
viewForImgAndEmoji.addGestureRecognizer(pinch)
...
viewForImgAndEmoji.addGestureRecognizer(rotate)

Issue when passing data through segue in swift2 [duplicate]

This is a Tip Calculator Project and It must have a settings view where I select the default tip rate. I have some issues with passing data, when I select a default tip percentage it doesn't change in the View Controller, also I want to make the app remember the default rate when I close the app and reopened. I will really appreciate that some one corrects my code and test it. This is for entering a Computer Science Program in college, I don't have previous experience with any programming language before.
100
TipPercentageLabel.text = "(tipDisplay)%"
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func setupContainer() {
tipSlider.minimumValue = 0
tipSlider.maximumValue = 100
tipSlider.value = 20
tipSlider.addTarget(self, action: "sliderTipChanged:", forControlEvents: .ValueChanged)
personsStepper.minimumValue = 1
personsStepper.maximumValue = 30
personsStepper.value = 1
personsStepper.addTarget(self, action: "sliderPersonChanged:", forControlEvents: .ValueChanged)
amountTextField.text = ""
refreshCalculation()
}
#IBAction func OnEditingFieldBill(sender: AnyObject) {
refreshCalculation()
}
func refreshCalculation() {
numberFormatter.numberStyle = NSNumberFormatterStyle.DecimalStyle
if let amount = numberFormatter.numberFromString(amountTextField.text!) as? Double {
let tipAmount = amount * tipPercentage
let totalBill = amount + tipAmount
let billPerPerson = totalBill / Double(numberOfPerson)
numberFormatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
tipAmountLabel.text = numberFormatter.stringFromNumber(tipAmount)
totalBillLabel.text = numberFormatter.stringFromNumber(totalBill)
billPerPersonLabel.text = numberFormatter.stringFromNumber(billPerPerson)
} else {
tipAmountLabel.text = "-"
totalBillLabel.text = "-"
billPerPersonLabel.text = "-"
}
numberFormatter.numberStyle = NSNumberFormatterStyle.PercentStyle
numberFormatter.minimumFractionDigits = 1
numberFormatter.maximumFractionDigits = 1
TipPercentageLabel.text = self.numberFormatter.stringFromNumber(tipPercentage)
numberOfPersonLabel.text = "\(numberOfPerson)"
}
#IBAction func sliderTipChanged(sender: AnyObject) {
tipPercentage = Double(round(tipSlider.value)) / 100
refreshCalculation()
}
#IBAction func StepperPersonChanged(sender: AnyObject) {
numberOfPerson = Int(round(personsStepper.value))
refreshCalculation()
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let id = segue.identifier {
if id == "show settings" {
if let SettingsViewController = segue.destinationViewController as? SettingsViewController {
}
}
}
}
}
SETTINGS VIEW CONTROLLER
import UIKit
class SettingsViewController: UIViewController {
#IBOutlet weak var tipControl: UISegmentedControl!
var tipRates:Double?
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 DefaultRate(sender: AnyObject) {
va
if let tip = tipRates {
ViewController.tipPercentage = tip/100
Replace you DefaultRate IBAction with this:
#IBAction func changevalue(sender: UISegmentedControl) {
var tipRate = [5, 10, 15, 20, 25, 30]
tipRates = Double(tipRate[tipControl.selectedSegmentIndex])
delegate?.tipPercentageChanged(tipRates!) print("(tipRates)")
NSUserDefaults.standardUserDefaults().setDouble(tipRates!, forKey: "DefaultTipRate")
NSUserDefaults.standardUserDefaults().synchronize()
}
And set the event as "Value Changed" as shown in the image below
I strongly recommend you go through a few introductory iOS & Swift tutorials online. SO is not the place to get beginner level understanding of programming topics. Some recommended tutorials:
Swift 2 Tutorial
Apple swift tutorials

Resources