Half of my window is a UITableView and other is a PageViewController.
In my PageViewController, I have two properties:
#IBOutlet weak var displayImageView: UIImageView!
var displayImage : UIImage?
PageViewController viewDidLoad I assign:
override func viewDidLoad() {
super.viewDidLoad()
self.displayImageView.image = self.displayImage
}
displayImageView is an outlet connected to my scene in Storyboard. As the User taps different cells, I would like the image in displayImageView to change.
In my parentViewController that displays the tableView and the PageViewController, I call the following method in my didSelectRowAtIndexPath
func getDisplayImageForSelectedRow() {
var row = self.tableView.indexPathForSelectedRow()?.row
//DisplayImageVC is a contentViewController that is loaded by pageViewController
var displayImageVC = self._pageContent[1] as DisplayImageVC
displayImageVC.displayImage = self._displayImage[row!] as UIImage
println(displayImageVC.displayImage)
if (self.isPageViewVisible){
displayImageVC.displayImageView.reloadInputViews()
displayImageVC.displayImageView.setNeedsDisplay()
}
}
Here the log from println():
You selected cell #0
Optional(<UIImage: 0x7c21c2e0>)
You selected cell #1
Optional(<UIImage: 0x7c21c5a0>)
You selected cell #2
Optional(<UIImage: 0x7c21cd00>)
You selected cell #3
Optional(<UIImage: 0x7c21cff0>)
You selected cell #4
Optional(<UIImage: 0x7c21d2d0>)
Based on the log, I presume a new image is being assigned; however, the UIImageView outlet continues to show the same image that was loaded the first time pageViewController was loaded.
As you may have noticed in the code, I have tried the following lines to code to get the UIImageView to refresh, but no avail:
displayImageVC.displayImageView.reloadInputViews()
displayImageVC.displayImageView.setNeedsDisplay()
How can I get the UIImageView to show the new image that was loaded?
In getDisplayImageForSelectedRow you are only updating the UIImage, you should also assign the image to the view.:
func getDisplayImageForSelectedRow() {
..
displayImageVC.displayImage = self._displayImage[row!] as UIImage
displayImageVC.displayImageView.image = displayImageVC.displayImage <--THIS
..
}
Related
I am trying to display a new image based on a user tapping the image view. I have added a UITapGestureRecognizer on top of the UIImageView which has been defined a "displayPhoto" and connected it as an outlet to the view controller.
#IBOutlet weak var displayPhoto: UIImageView!
#IBAction func changeImage(_ sender: UITapGestureRecognizer) {
displayPhoto.image = UIImage(named: "myimage")
}
The sent action is listed as follows:
sent actions
When I run the app and click on the image, nothing happens. I've even tried to make the first line of the IBAction function a fatal error but nothing happens. What am I missing?
Thanks in advance for any help.
The full view and connection
Tap Gesture Recognizer Window
Firstly you try; clean your project
command+option+shift+K
The first case cannot be empty
You should add a picture.
override func viewDidLoad() {
super.viewDidLoad()
displayPhoto.image = UIImage(named: "firsCase")
}
I want to scroll and display the whole table view in my view controller.
Like this, I want the collection view moves up when scrolling, and the content of the table view will display in the whole screen.
But what I got now is the table view scrolled just inside the table view section, and the collection view didn't move up.
This is my view controller's hierarchy, I have the Collection View & Table View inside the Scroll View
I have checked this post and this post but not worked.
Instead of adding collectionView separately inside the controller's view, you can add the collectionView in tableView's headerView.
1. Create a custom UIView - HeaderView, that will contain a UICollectionView
class HeaderView: UIView {
#IBOutlet weak var collectionView: UICollectionView!
//Add collectionView dataSource and delegate methods here
}
2. Add the above created HeaderView as tableView's headerView in your controller.
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
//Here.....
if let headerView = Bundle.main.loadNibNamed("HeaderView", owner: self, options: nil)?.first as? HeaderView {
self.tableView.tableHeaderView = headerView
}
}
//add UITableViewDataSource and UITableViewDelegate methods here...
}
In storyboard - add the tableView in the controller and pin its - top, bottom, left and right constraints to the controller's view.
Output:
You might try placing the collection view inside a cell and place the cell as the first row in your table view.
How do I make a label in my ViewController have a diffrent string of text each time the view crontroller is shown? Thanks! I'm using Swift 3
Assuming you know how to add UILabel to your ViewController, here is quick sample how to pick random text on start:
class ViewController: UIViewController {
let allTexts = ["Hey", "Hi", "Hello"]
#IBOutlet weak var label: UILabel! //get UILabel from storyboard
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.label.text = self.allTexts[Int(arc4random_uniform(UInt32(self.allTexts.count)))]
}
}
Adding this code to viewWillAppear will change your text anytime ViewController is about to appear - which means if you cover it with another ViewController (let's say popup) and then hide popup - it will change text.
If your prefer to just do it one time - when UIViewController is created put the same code inside viewDidLoad method.
I have a UIButton inside my cell together with an image and a text label. I manage to change the image and label programatically, but the UIButton does not seem to respond to anything except isHidden.
This is my code, the button that is not changing is followButton:
import UIKit
class ProfileTableCell: UITableViewCell {
#IBOutlet weak var name: UILabel!
#IBOutlet weak var profileImage: UIImageView!
#IBOutlet weak var followButton: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
self.profileImage.layer.borderWidth = 0.0;
self.profileImage.layer.cornerRadius = self.profileImage.frame.size.width/2;
self.profileImage.clipsToBounds = true
self.profileImage.image = UIImage(named: "belt")
self.name.text = "Bar Refaeli"
self.followButton.layer.borderColor = UIColor.black.cgColor
self.followButton.layer.borderWidth = 3.0;
self.followButton.layer.cornerRadius = self.frame.size.width/4
self.followButton.backgroundColor = UIColor.black
}
func setCell(image: UIImage, name: String){
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
The profileImage and name outlets change the appearance fine, like mentioned above.
I also tried to remove the button and bring it back in, clean xcode project, remove the outlet reference and connecting it again. Pretty frustrated by now.
I also tried to change the background color of the button through the storyboard, just for testing, and it does not change it! what does change is the titleLabel and the text color.
awakeFromNib()- Prepares the receiver for service after it has been loaded from an Interface Builder archive, or nib file.
Given that, move your code to a view initiating method like viewDidLoad or viewDidAppear(_:)
Child objects that are attributes like textLabels act differently than child view objects.
Eventually I actually solved this by tossing the table view to the garbage and implementing the same needs using a collection view. there was no problem there..
I am writing a master-detail application. When table row item is clicked, I load the image in the DetailViewController, which has a UIImageView. I assume the UIImageView is instantiated by app and I only need to set the UIImage.
imageView.image = UIImage(name:"camera")
However, the imageView is nil. I sure that the imageView is linked with UIImageView on storyboard.
#IBOutlet weak var imageView: UIImageView!
func configureView() {
if !imageView {
println("nilnilnil") // nilnilnil is printed...
}
}
You need to make sure that the view has already been loaded. You should at least wait until after viewDidLoad. Usually you would do this directly in viewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
self.imageView.image = UIImage(name: "camera")
}
This is because the subviews are not created and the connections are not setup until the view is loaded. The view is not loaded until the view property of the view controller is accessed – usually, this is when the view controller is presented for the first time.
Minor Note: A variable is "instantiated" or "initialized" not "instanced"