Categories
Mastering Development

modalPresentationStyle not updating the data

The below code can show the ratings screen using modal or i can push it. if i use modal i can see the animation effect and not in pushed controller method, the animation are defined in a view class separately, but in case of pushed i can see the ratings gets updated instantly but not in modal presentation.

what can be the reason? the code i use to get back after updating the ratings is also below this

How can i update the ratings as well as show the animations effects using either modal or pushed?

why are they behaving differently and not doing both tasks of showing animations and updating the views, but only doing one each?

Is there any limitation as to what modal can do and what push can do, why pushed controller refuses to animates the view? the animations are being defined in separate view class, while the modal animates them perfectly well, but it then refuse to update the new ratings when i show the detail view again.

Code to push controller

@objc func gotoRatings() {
      let rateDetailView = RatingsViewController()
     rateDetailView.restaurant = restaurant
      rateDetailView.modalPresentationStyle = .fullScreen
      present(rateDetailView, animated: true, completion: nil)
       //     rateDetailView.restaurant = restaurant
      
      //  navigationController?.pushViewController(rateDetailView, animated: true)
    }

Code to add ratings

@objc func addRatings(_ sender: UIButton) {
    
    let detailReturn = DetailViewController()
        if let rating = sender.titleLabel?.text {
         self.restaurant.rating = rating
          if let appDelegat = (UIApplication.shared.delegate as? AppDelegate) {
            appDelegat.saveContext()
          }
        }
        
        
       detailReturn.restaurant = restaurant
     
   //   self.navigationController?.pushViewController(detailReturn, animated: false)
    dismiss(animated: true, completion: nil)
  }

UPDATES

The modal view is basically an extension of current view hence it has no back button.

The push controller can not have custom actions, but is used for the case when a controller is pushed on top of stack with all views , updated.

In my case i want a combination of both, so may be i will need to push the view and then make animations on the control page rather then view page as the view’s animations will not get pushed

UPDATED CODE- the class where protocol is defined

import UIKit
import CoreData

protocol RatingsPresentation: class {
    func defineTheratings(_ ratings: String)
}

class RatingsViewController: UIViewController, RatingsPresentation {
    func defineTheratings(_ ratings: String) {
         print("ratings seleced\(ratings)")
     
               self.restaurant.rating = ratings
               if let appDelegate = (UIApplication.shared.delegate as? AppDelegate) {
                   appDelegate.saveContext()
               }
     
               
               if  self.presentedViewController  != nil {
                   self.dismiss(animated: true, completion: nil)
               }
               else {
                   self.navigationController?.popViewController(animated: true)
               }
    }

    var restaurant: Restaurant!
    var rates = RatingsView()
    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(rates.view)
        //Delegates
      
        // set the rating delegate
        rates.ratingDelegate = self
        //
        if let restaurantImage = restaurant.image {
            
            rates.bkgImageView.image = UIImage(data: restaurantImage as Data)
        }
        
        rates.crossBtn.addTarget(self, action: #selector(closeRatings), for: .touchUpInside)
  
        
        let animateCross = CGAffineTransform(translationX: 1000, y: 0)
        rates.crossBtn.transform = animateCross
  
    }
    
    @objc func closeRatings() {
    
        navigationController?.popViewController(animated: true)
     
    }

}

The View for ratings

import UIKit

class RatingsView: UIViewController {
    
    var bkgImageView = UIImageView()
    var crossBtn = UIButton()
    var btnCollection: [UIButton] = []
    weak var ratingDelegate: RatingsPresentation?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let ratings: [String] = ["cool", "happy","love", "sad", "angry"]
        
        let animationBlur = UIBlurEffect(style: .dark)
        let visualBlurView = UIVisualEffectView(effect: animationBlur)
        
        
        
        
        // add elements to self
        view.addSubview(bkgImageView)
        view.addSubview(visualBlurView)
        
        bkgImageView.translatesAutoresizingMaskIntoConstraints = false
        visualBlurView.translatesAutoresizingMaskIntoConstraints = false
        bkgImageView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        bkgImageView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        bkgImageView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        bkgImageView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
        
        // constrain visualBlurView to all 4 sides
        visualBlurView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        visualBlurView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        visualBlurView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        visualBlurView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
        
        bkgImageView.contentMode = .scaleAspectFill
        // Do any additional setup after loading the view.
        //Add stackView
        
        let stackEmoji = UIStackView()
        stackEmoji.translatesAutoresizingMaskIntoConstraints = false
        
        stackEmoji.axis = .vertical
        stackEmoji.spacing = 5
        stackEmoji.distribution = .fill
        stackEmoji.alignment = .top
        
        let font = UIFont(name: "Rubik-Medium", size: 28)
        let fontM = UIFontMetrics(forTextStyle: .body)
        
        ratings.forEach { (btn) in
            let b = UIButton()
            b.setTitle(btn, for: .normal)
            b.titleLabel?.font = fontM.scaledFont(for: font!)
            b.setImage(UIImage(named: btn), for: .normal)
            stackEmoji.addArrangedSubview(b)
            btnCollection.append(b)
            
            //btn animation
            let sizeAnimation = CGAffineTransform(scaleX: 5, y: 5)
            let positionAnimation = CGAffineTransform(translationX: 1000, y: 0)
            let combinedAninaton = sizeAnimation.concatenating(positionAnimation)
            b.transform = combinedAninaton
            b.addTarget(self, action: #selector(ratingButtonTapped(_:)), for: .touchUpInside)
        }
        
        view.addSubview(stackEmoji)
        
        stackEmoji.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        stackEmoji.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
        
//          if let img = UIImage(named: "cross") {
//            crossBtn.setImage(img, for: [])
//              }
        
        crossBtn.setTitle("X", for: [])
        crossBtn.setTitleColor(.white, for: .normal)
        crossBtn.setTitleColor(.gray, for: .highlighted)
        crossBtn.titleLabel?.font = UIFont.systemFont(ofSize: 44, weight: .bold)
        view.addSubview(crossBtn)
        
        
        crossBtn.translatesAutoresizingMaskIntoConstraints = false
        crossBtn.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -30).isActive = true
        crossBtn.topAnchor.constraint(equalTo: view.topAnchor, constant: 150).isActive = true
        
       

    }
    
  
    
    @objc func ratingButtonTapped(_ sender: UIButton) {
      //  print(sender.titleLabel?.text)
        guard let t = sender.titleLabel?.text else {
            return
        }
        
        ratingDelegate?.defineTheratings(t)
                 
    }
   

}

Leave a Reply

Your email address will not be published. Required fields are marked *