Bonjour,

J'ai créé une fonction updateItems() qui crée, depuis un tableau, plusieurs UIView's dans un UIScrollView :

Voici le fichier où se trouve la fonction :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
class MainViewController: UIViewController {  
       
        @IBOutlet weak var body: UIScrollView!  
        @IBOutlet weak var edit: UIButton!  
  
        var _title: String = "Title"  
        var _isEditing: Bool = false  
        var firstItems: [UISectionView] = []  
  
        override func viewDidLoad() {  
            super.viewDidLoad()  
            navigationController?.navigationBar.prefersLargeTitles = true  
            navigationController?.navigationBar.topItem?.title = self._title  
            navigationController?.navigationItem.largeTitleDisplayMode = .automatic  
           
            body.contentSize = CGSize(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height + 100)  
           
            self.updateItems(self.firstItems)  
           
            // 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.  
        }  
  
        public func updateItems(_ s: [UISectionView]) {  
            let topMargin = 10  
            let rightMargin = 10  
            let leftMargin = 10  
            let space = 5  
            let heightItem = 60  
            var b = topMargin  
           
            for i in body.subviews {  
                i.removeFromSuperview()  
            }  
           
            for t in s {  
                if t.isHidden == true {  
                    continue  
                }  
                if t.title != nil {  
                    let f = UIFont(name: "Helvetica", size: 20)  
                    let l = UILabel(frame: CGRect(x: rightMargin, y : b, width: Int(UIScreen.main.bounds.width) - (rightMargin + leftMargin), height: Int(f!.lineHeight)))  
                    l.font = f  
                    l.text = t.title  
                    body.addSubview(l)  
                    b = b + Int(f!.lineHeight) + space  
                }  
                for i in t.items{  
                    body.addSubview(i.getView(frame: CGRect(x: rightMargin, y: b, width: Int(UIScreen.main.bounds.width) - (rightMargin + leftMargin), height: heightItem), view: self))  
                    b = b + heightItem + space  
                }  
            }  
        }  
       
    }
TIPS : UISectionView est un objet qui contient un tableau d'UIItemView

Le code de l'objet UIItemView ressemble à :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
    class UIItemView {  
       
        var icon: UIImage = UIImage();  
        var line1: rString = rString("")!;  
        var line2: rString = rString("")!;  
        var leftline: Any = String();  
        var background: String = "white"  
        var onItemTap: (_ sender: UITapGestureRecognizer?) -> () = {sender in }  
        var onItemLongPress: (_ sender: UILongPressGestureRecognizer?) -> () = {sender in }  
       
        var id: String  
       
        init?(_ id: String) {  
            self.id = id  
        }  
       
        public func getView(frame: CGRect, view: UIViewController) -> UIView {  
            let width = Int(frame.width)  
            let height = Int(frame.height)  
            let rightMargin = 20  
            let leftMargin = 10  
            let topMargin = 10  
            let bottomMargin = 10  
            let iconSide = height - (topMargin + bottomMargin)  
            let marginLine = leftMargin + iconSide + 10  
           
            let v = UIView(frame: frame)  
           
            //Background & shape  
            if self.background == "white" {  
                v.backgroundColor = UIColor.white;  
            } else if self.background == "blur" {  
                let bEV = UIVisualEffectView(effect: UIBlurEffect(style: UIBlurEffectStyle.extraLight))  
                bEV.frame = v.bounds  
                bEV.autoresizingMask = [.flexibleWidth, .flexibleHeight]  
                v.addSubview(bEV)  
            }  
            v.layer.cornerRadius = 10.0  
           
            //Icon  
            let i = UIImageView()  
            i.image = self.icon;  
            i.frame = CGRect(x: leftMargin, y: topMargin, width: iconSide, height: iconSide)  
            v.addSubview(i)  
           
            //First Line  
            let l1 = self.line1.getLabel()  
            l1.frame = CGRect(x: marginLine, y: topMargin, width: width - (marginLine + leftMargin), height: Int(self.line1.getFont().lineHeight))  
            v.addSubview(l1)  
           
            //Seconde Line  
            let l2 = self.line2.getLabel()  
            l2.frame = CGRect(x: marginLine, y: height - (bottomMargin + Int(self.line1.getFont().lineHeight)), width: width - (marginLine + leftMargin), height: Int(self.line1.getFont().lineHeight))  
            v.addSubview(l2)  
           
            //Left Line  
            if type(of: self.leftline) == type(of: SpinnerView()) {  
                let sv = (self.leftline as! SpinnerView)  
                sv.frame = CGRect(x: width - (rightMargin + iconSide), y: height/2 - iconSide/2, width: iconSide, height: iconSide)  
                v.addSubview(sv)  
            } else if type(of: self.leftline) == type(of: rString("")) {  
                let rS = (self.leftline as! rString)  
                if rS.text != "" {  
                    rS.fontName = "HelveticaNeue-Bold"  
                    rS.size = 15  
                    rS.color = UIColor(red:0.01, green:0.48, blue:1.00, alpha:1.0)  
                    let l3 = rS.getLabel()  
                    l3.frame = CGRect(x: width - (rightMargin + Int(rS.getFont().lineWidth(rS.text)) + 15), y: height/2 - (Int(rS.getFont().lineHeight) + 10)/2, width: Int(rS.getFont().lineWidth(rS.text)) + 15, height: Int(rS.getFont().lineHeight) + 10)  
                    l3.backgroundColor = UIColor(red:0.94, green:0.94, blue:0.97, alpha:1.0)  
                    l3.layer.masksToBounds = true  
                    l3.layer.borderWidth = 2  
                    l3.layer.borderColor = UIColor(red:0.94, green:0.94, blue:0.97, alpha:1.0).cgColor  
                    l3.layer.cornerRadius = rS.getFont().lineHeight/1.2  
                    l3.textAlignment = .center  
                    v.addSubview(l3)  
                }  
            }  
           
            //GestureRecognizer  
            v.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.oIT(_:))))  
            v.addGestureRecognizer(UILongPressGestureRecognizer(target: self, action: #selector(self.oILP(_:))))  
            v.restorationIdentifier = self.id  
            return v;  
        }  
       
        @objc func oIT(_ sender: UITapGestureRecognizer) {  
            print("Tap")  
            self.onItemTap(sender)  
        }  
        @objc func oILP(_ sender: UILongPressGestureRecognizer) {  
            print("LongPress")  
            self.onItemLongPress(sender)  
        }  
       
        static func ==(lhs: UIItemView, rhs: UIItemView) -> Bool {  
            return lhs === rhs  
        }  
       
    }
TIPS : UIItemView contient le fonction getView() qui retourne un UIView adapté.

Le problème :

Tout fonctionne parfaitement, quand je charge le ViewController (MainViewController) (où se trouve l'UIScrollView) tous les UIView's sont construit comme je le veux, et je peux interagir avec eux via le UITapGestureRecognizer et le UILongPressGestureRecognizer (l'event est bien appelé comme attendu)

MAIS

Quand j'appelle la fonction updateItems() une seconde fois, sans recharger le MainViewController, les UIView's chargent comme attendu mais les event UITapGestureRecognizer et UILongPressGestureRecognizer ne fonctionne plus... Ils ne sont plus appelés

J'espère que vous pouvez m'aider
Si des informations manquent pour votre comprehension, dite-le moi