Explorar el Código

Trying to call the RootViewController from UIViews.

Iñigo Valentin hace 9 años
padre
commit
2d3b52d6a5
Se han modificado 6 ficheros con 107 adiciones y 31 borrados
  1. 4 0
      app.xcodeproj/project.pbxproj
  2. 10 0
      app/BlogView.swift
  3. 28 26
      app/PostView.swift
  4. 18 5
      app/PostView.xib
  5. 42 0
      app/UIApplcation.swift
  6. 5 0
      app/ViewController.swift

+ 4 - 0
app.xcodeproj/project.pbxproj

@@ -20,6 +20,7 @@
 		935892D01E493C2300E1CD5F /* UIImageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 935892CF1E493C2300E1CD5F /* UIImageView.swift */; };
 		937EA0291E5A267C00D0893A /* PostView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 937EA0281E5A267C00D0893A /* PostView.swift */; };
 		937EA02B1E5A271F00D0893A /* PostView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 937EA02A1E5A271F00D0893A /* PostView.xib */; };
+		937EA02D1E5B8B5300D0893A /* UIApplcation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 937EA02C1E5B8B5300D0893A /* UIApplcation.swift */; };
 		93B408031D9ED44C000FBC99 /* MenuCollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 93B408021D9ED44C000FBC99 /* MenuCollectionViewCell.swift */; };
 		93B84B9A1D9C31AB006C0F67 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 93B84B991D9C31AB006C0F67 /* AppDelegate.swift */; };
 		93B84B9C1D9C31AB006C0F67 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 93B84B9B1D9C31AB006C0F67 /* ViewController.swift */; };
@@ -56,6 +57,7 @@
 		935892CF1E493C2300E1CD5F /* UIImageView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIImageView.swift; sourceTree = "<group>"; };
 		937EA0281E5A267C00D0893A /* PostView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PostView.swift; sourceTree = "<group>"; };
 		937EA02A1E5A271F00D0893A /* PostView.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = PostView.xib; sourceTree = "<group>"; };
+		937EA02C1E5B8B5300D0893A /* UIApplcation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIApplcation.swift; sourceTree = "<group>"; };
 		93B408021D9ED44C000FBC99 /* MenuCollectionViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = MenuCollectionViewCell.swift; path = ../MenuCollectionViewCell.swift; sourceTree = "<group>"; };
 		93B84B961D9C31AB006C0F67 /* app.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = app.app; sourceTree = BUILT_PRODUCTS_DIR; };
 		93B84B991D9C31AB006C0F67 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
@@ -145,6 +147,7 @@
 				93BAD2F71E586741009DF853 /* RowHomeSocial.swift */,
 				937EA0281E5A267C00D0893A /* PostView.swift */,
 				937EA02A1E5A271F00D0893A /* PostView.xib */,
+				937EA02C1E5B8B5300D0893A /* UIApplcation.swift */,
 			);
 			path = app;
 			sourceTree = "<group>";
@@ -242,6 +245,7 @@
 			buildActionMask = 2147483647;
 			files = (
 				93294FF81E336FA000FDAF50 /* Entry.swift in Sources */,
+				937EA02D1E5B8B5300D0893A /* UIApplcation.swift in Sources */,
 				93D9EA641DE273FD00089002 /* Section.swift in Sources */,
 				93D9EA6A1DE3491100089002 /* HomeView.swift in Sources */,
 				93BAD2F81E586741009DF853 /* RowHomeSocial.swift in Sources */,

+ 10 - 0
app/BlogView.swift

@@ -131,6 +131,16 @@ class BlogView: UIView {
 		}
 		// TODO: Calculate at the end
 		self.scrollView.contentSize.height = 2500//CGFloat(h);
+		
+		// The view controller
+		//var viewController: ViewController  = self.window?.rootViewController as! ViewController
+		
+		
+		let viewController = UIApplication.topViewController() as! ViewController
+		
+		viewController.showPost(id: 4)
+		
+		
 	}
 	
 	func getLanguage() -> String{

+ 28 - 26
app/PostView.swift

@@ -28,11 +28,23 @@ class PostView: UIView {
 	//The main scroll view.
 	@IBOutlet weak var scrollView: UIScrollView!
 	
-	//The container of the view.
-	@IBOutlet var container: UIView!
+	@IBOutlet weak var title: UILabel!
 	
-	//Each of the sections of the view.
-	@IBOutlet weak var section: Section!
+	@IBOutlet weak var image: UIImageView!
+	
+	@IBOutlet weak var text: UILabel!
+	
+	@IBOutlet weak var imageExtra: UIStackView!
+	
+	@IBOutlet weak var date: UILabel!
+	
+	@IBOutlet weak var commentCount: UILabel!
+	
+	@IBOutlet weak var commentList: UIStackView!
+	
+	@IBOutlet weak var commentUser: UITextField!
+	
+	@IBOutlet weak var commentContent: UITextView!
 	
 	
 	override init(frame: CGRect){
@@ -48,8 +60,8 @@ class PostView: UIView {
 		
 		//Load the contents of the HomeView.xib file.
 		Bundle.main.loadNibNamed("BlogView", owner: self, options: nil)
-		self.addSubview(container)
-		container.frame = self.bounds
+		//self.addSubview(container)
+		//container.frame = self.bounds
 		
 		
 	}
@@ -62,9 +74,7 @@ class PostView: UIView {
 		let lang : String = getLanguage()
 		context.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator
 		let fetchRequest: NSFetchRequest<Post> = Post.fetchRequest()
-		let sortDescriptor = NSSortDescriptor(key: "dtime", ascending: false)
-		let sortDescriptors = [sortDescriptor]
-		fetchRequest.sortDescriptors = sortDescriptors
+		fetchRequest.predicate = NSPredicate(format: "post == %i", id)
 		
 		do {
 			//go get the results
@@ -73,11 +83,9 @@ class PostView: UIView {
 			//I like to check the size of the returned results!
 			print ("Post: \(searchResults.count)")
 			
-			var row : RowBlog
 			var count = 0
-			var id: Int
-			var title: String
-			var text: String
+			var sTitle: String
+			var sText: String
 			var image: String
 			
 			//You need to convert to NSManagedObject to use 'for' loops
@@ -87,14 +95,10 @@ class PostView: UIView {
 				print("Perm: \(r.value(forKey: "permalink"))")
 				
 				
-				//Create a new row
-				row = RowBlog.init(s: "rowBlog\(count)", i: count)
-				id = r.value(forKey: "id")! as! Int
-				title = r.value(forKey: "title_\(lang)")! as! String
-				text = r.value(forKey: "text_\(lang)")! as! String
-				print(title)
-				row.setTitle(text: title)
-				row.setText(text: text)
+				sTitle = r.value(forKey: "title_\(lang)")! as! String
+				sText = r.value(forKey: "text_\(lang)")! as! String
+				title.text = sTitle
+				text.text = sText
 				
 				// Get main image
 				image = ""
@@ -104,21 +108,19 @@ class PostView: UIView {
 				imgFetchRequest.sortDescriptors = imgSortDescriptors
 				imgFetchRequest.predicate = NSPredicate(format: "post == %i", id)
 				imgFetchRequest.fetchLimit = 1
+				//TODO get more images
 				do{
 					let imgSearchResults = try context.fetch(imgFetchRequest)
 					for imgR in imgSearchResults as [NSManagedObject]{
 						image = imgR.value(forKey: "image")! as! String
 						print ("IMAGE: \(image)")
-						row.setImage(filename: image)
+						//TODO set image
+						//row.setImage(filename: image)
 					}
 				} catch {
 					print("Error getting image for post \(id): \(error)")
 				}
 				
-				print("Row height: \(row.frame.height)")
-				
-				parent.addArrangedSubview(row)
-				
 			}
 		} catch {
 			print("Error with request: \(error)")

+ 18 - 5
app/PostView.xib

@@ -9,9 +9,22 @@
         <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
     </dependencies>
     <objects>
-        <placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
+        <placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="PostView" customModule="app" customModuleProvider="target">
+            <connections>
+                <outlet property="commentContent" destination="kba-hh-hSu" id="awu-5F-mxL"/>
+                <outlet property="commentCount" destination="w3V-dl-ijf" id="5Vn-ki-Mzq"/>
+                <outlet property="commentList" destination="For-Ru-u5Y" id="DBl-M2-7iO"/>
+                <outlet property="commentUser" destination="4Rd-hH-5iP" id="0Mk-Q5-mqo"/>
+                <outlet property="date" destination="lBQ-aX-qhL" id="KDe-Bo-Utv"/>
+                <outlet property="image" destination="vwR-EU-h1T" id="6hL-jI-oGf"/>
+                <outlet property="imageExtra" destination="L5f-5R-97B" id="iCs-yP-Kzd"/>
+                <outlet property="scrollView" destination="8gP-MM-Wzw" id="ABL-y6-ZHh"/>
+                <outlet property="text" destination="NoQ-Kc-oS7" id="LNM-QN-1DR"/>
+                <outlet property="title" destination="4ub-YM-i5l" id="yWW-H6-DCF"/>
+            </connections>
+        </placeholder>
         <placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
-        <view contentMode="scaleToFill" id="Zrz-uS-eQa">
+        <view contentMode="scaleToFill" id="Zrz-uS-eQa" customClass="PostView" customModule="app" customModuleProvider="target">
             <rect key="frame" x="0.0" y="0.0" width="320" height="480"/>
             <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
             <subviews>
@@ -114,19 +127,19 @@
                                                             <nil key="textColor"/>
                                                             <nil key="highlightedColor"/>
                                                         </label>
-                                                        <stackView opaque="NO" contentMode="scaleToFill" axis="vertical" translatesAutoresizingMaskIntoConstraints="NO" id="For-Ru-u5Y">
+                                                        <stackView opaque="NO" contentMode="scaleToFill" axis="vertical" spacing="10" translatesAutoresizingMaskIntoConstraints="NO" id="For-Ru-u5Y" userLabel="Comment List">
                                                             <rect key="frame" x="0.0" y="20.5" width="304" height="200"/>
                                                             <constraints>
                                                                 <constraint firstAttribute="height" constant="200" id="5tA-ix-yCI"/>
                                                             </constraints>
                                                         </stackView>
-                                                        <textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="4Rd-hH-5iP">
+                                                        <textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="4Rd-hH-5iP" userLabel="Comment user">
                                                             <rect key="frame" x="0.0" y="220.5" width="304" height="30.5"/>
                                                             <nil key="textColor"/>
                                                             <fontDescription key="fontDescription" type="system" pointSize="14"/>
                                                             <textInputTraits key="textInputTraits"/>
                                                         </textField>
-                                                        <textView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" textAlignment="natural" translatesAutoresizingMaskIntoConstraints="NO" id="kba-hh-hSu">
+                                                        <textView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" textAlignment="natural" translatesAutoresizingMaskIntoConstraints="NO" id="kba-hh-hSu" userLabel="Comment content">
                                                             <rect key="frame" x="0.0" y="251" width="304" height="128"/>
                                                             <color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
                                                             <string key="text">Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.</string>

+ 42 - 0
app/UIApplcation.swift

@@ -0,0 +1,42 @@
+// Copyright (C) 2016 Inigo Valentin
+//
+// This file is part of the Gasteizko Margolariak IOS app.
+//
+// The Gasteizko Margolariak IOS app is free software: you can
+// redistribute it and/or modify it under the terms of the
+// GNU General Public License as published by the Free Software
+// Foundation, either version 3 of the License, or (at your
+// option) any later version.
+//
+// The Gasteizko Margolariak IOS app is distributed in the
+// hope that it will be useful, but WITHOUT ANY WARRANTY;
+// without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
+// Public License for more details.
+//
+// You should have received a copy of the GNU General Public
+// License along with the Gasteizko Margolariak IOS app.
+// If not, see <http://www.gnu.org/licenses/>.
+
+import Foundation
+import UIKit
+
+/**
+Extension of UIApplication to get the root view controller.
+*/
+extension UIApplication {
+	class func topViewController(controller: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
+		if let navigationController = controller as? UINavigationController {
+			return topViewController(controller: navigationController.visibleViewController)
+		}
+		if let tabController = controller as? UITabBarController {
+			if let selected = tabController.selectedViewController {
+				return topViewController(controller: selected)
+			}
+		}
+		if let presented = controller?.presentedViewController {
+			return topViewController(controller: presented)
+		}
+		return controller
+	}
+}

+ 5 - 0
app/ViewController.swift

@@ -37,12 +37,17 @@ class ViewController: UIViewController, UICollectionViewDataSource, UICollection
 	@IBOutlet weak var containerViewLocation: UIView!
 	@IBOutlet weak var containerViewHome: HomeView!
 	
+	
 	func getContext () -> NSManagedObjectContext {
 		//let appDelegate = UIApplication.shared.delegate as! AppDelegate
 		//return appDelegate.persistentContainer.viewContext
 		return NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
 	}
 	
+	func showPost(id: Int){
+		print("VIEW CONTROLLER showPost")
+	}
+	
 	/**
 	 Run when the app loads.
 	*/