Переглянути джерело

Using delegates to pass info between the views and the controller.

Iñigo Valentin 9 роки тому
батько
коміт
8ec0f0dd6e
6 змінених файлів з 67 додано та 4 видалено
  1. 2 0
      app/AppDelegate.swift
  2. 25 0
      app/BlogView.swift
  3. 1 0
      app/HomeView.swift
  4. 12 0
      app/RowBlog.swift
  5. 16 0
      app/UIView.swift
  6. 11 4
      app/ViewController.swift

+ 2 - 0
app/AppDelegate.swift

@@ -25,6 +25,8 @@ import CoreData
 class AppDelegate: UIResponder, UIApplicationDelegate {
 
 	var window: UIWindow?
+	
+	var controller: ViewController?
 
 	
 

+ 25 - 0
app/BlogView.swift

@@ -26,6 +26,8 @@ Class to handle the home view.
 */
 class BlogView: UIView {
 	
+	//var window:UIWindow?
+	
 	//The main scroll view.
 	@IBOutlet weak var scrollView: UIScrollView!
 	
@@ -35,6 +37,8 @@ class BlogView: UIView {
 	//Each of the sections of the view.
 	@IBOutlet weak var section: Section!
 	
+	var controller : ViewController? = nil
+	
 	
 	override init(frame: CGRect){
 		super.init(frame: frame)
@@ -116,6 +120,12 @@ class BlogView: UIView {
 				
 				print("BLOG:DEBUG: Row height: \(row.frame.height)")
 				
+				
+				// Set tap recognizer
+				let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector (self.openPost (_:)))
+				row.isUserInteractionEnabled = true
+				row.addGestureRecognizer(tapRecognizer)
+				
 				parent.addArrangedSubview(row)
 				
 			}
@@ -142,6 +152,16 @@ class BlogView: UIView {
 		//viewController.showPost(id: 4)
 		
 		
+		
+		
+		
+	}
+	
+	func openPost(_ sender:UITapGestureRecognizer? = nil){
+		print("BLOG:DEBUG: getting delegate and showing post.")
+		let delegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
+		delegate.controller?.showPost(id: 4)
+		print("BLOG:DEBUG: Post should be shown.")
 	}
 	
 	func getLanguage() -> String{
@@ -153,4 +173,9 @@ class BlogView: UIView {
 			return "es"
 		}
 	}
+	
+	func setController(controller: ViewController){
+		
+		//self.controller = controller
+	}
 }

+ 1 - 0
app/HomeView.swift

@@ -84,6 +84,7 @@ class HomeView: UIView {
 		
 		pastActivitiesSection.expandSection()
 		
+		
 		//Always at the end: update scrollview
 		var h: Int = 0
 		for view in scrollView.subviews {

+ 12 - 0
app/RowBlog.swift

@@ -74,6 +74,12 @@ Extension of UIView to be formatted as sections.
 		
 		container.frame = v.bounds
 		v.addSubview(container)
+		
+		// Set tap recognizer
+		print("ROW_BLOG:DEBUG: set tap recognizer")
+		let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector (self.openPost (_:)))
+		tapRecognizer.delegate = (UIApplication.shared.delegate as! AppDelegate).controller
+		addGestureRecognizer(tapRecognizer)
 	}
 	
 	/**
@@ -83,6 +89,8 @@ Extension of UIView to be formatted as sections.
 		super.init(frame: frame)
 	}
 	
+
+	
 	/**
 	Changes the title of the post. Decodes HTML.
 	:param: text The new title.
@@ -115,4 +123,8 @@ Extension of UIView to be formatted as sections.
 			image.setImage(localPath: path, remotePath: "https://margolariak.com/\(path)")
 		}
 	}
+	
+	func openPost(_ sender:UITapGestureRecognizer? = nil){
+		print("ROW_BLOG:DEBUG: openpost")
+	}
 }

+ 16 - 0
app/UIView.swift

@@ -59,4 +59,20 @@ import UIKit
 			return layer.cornerRadius
 		}
 	}
+	
+	var parentViewController: ViewController? {
+		var parentResponder: UIResponder? = self
+		var i = 0
+		while parentResponder != nil {
+			print("UIVIEW:DEBUG: parentViewController level \(i)")
+			parentResponder = parentResponder!.next
+			if let viewController = parentResponder as? ViewController {
+				print("UIVIEW:DEBUG: parentViewController found  controller.")
+				return viewController
+			}
+		}
+		print("UIVIEW:DEBUG: parentViewController no controller found.")
+		return nil
+	}
+	
 }

+ 11 - 4
app/ViewController.swift

@@ -24,14 +24,16 @@ import CoreData
 /**
   The view controller of the app.
  */
-class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
+class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UIGestureRecognizerDelegate {
+	
+	var delegate: AppDelegate?
 	
 	//The menu collection.
 	var sectionCollection: UICollectionView!
 
 	//Each of the sections of the app.
 	@IBOutlet weak var containerViewGallery: UIView!
-	@IBOutlet weak var containerViewBlog: UIView!
+	@IBOutlet weak var containerViewBlog: BlogView!
 	@IBOutlet weak var containerViewActivities: UIView!
 	@IBOutlet weak var containerViewLablanca: UIView!
 	@IBOutlet weak var containerViewLocation: UIView!
@@ -45,7 +47,7 @@ class ViewController: UIViewController, UICollectionViewDataSource, UICollection
 	}
 	
 	func showPost(id: Int){
-		print("VIEW CONTROLLER showPost")
+		print("CONTROLLER:DEBUG: showPost")
 	}
 	
 	/**
@@ -62,12 +64,17 @@ class ViewController: UIViewController, UICollectionViewDataSource, UICollection
 		
 		//self.containerViewBlog.isHidden = true
 				
-		print("ViewController:viewDidLoad()")
+		print("CONTROLLER:LOG: viewDidLoad()")
 		print("CONTROLLER:DEBUG: Skyp sync")
 		//Sync()
 		
 		super.viewDidLoad()
 		// Do any additional setup after loading the view, typically from a nib.
+		
+		//self.containerViewBlog.setController(controller: self as ViewController)
+		
+		delegate = UIApplication.shared.delegate as! AppDelegate
+		delegate?.controller = self
 	}
 
 	/**