I am trying to dynamically set a UIImageView using Kingfisher. The context is there is a TableView on the previous screen of different selectable rows and then when the row is selected a details UIView shows which has more information, including an image. I have gone to the Kingfisher documentation and found that the declaration belongs in the viewDidLoad as i have it but i still cannot get the image to load. I have also verified with debugging that the URL being passed to the Kingfisher is a link to a picture. I am thinking this may have to do with the image being set on a background thread but i am unsure how to fix that if that is the case. I have tried moving the implementation of the Kingfisher to the viewWillAppear and viewDidAppear functions and they still do not display the image even after an extended amount of wait time
Here is my code with some unrelated functionality removed.
import UIKit
import Kingfisher
class DetailsViewController: UIViewController {
#IBOutlet weak var thumbnail: UIImageView!
var thumbnailURL: URL?
override func viewDidLoad() {
super.viewDidLoad()
self.thumbnail.kf.setImage(with: thumbnailURL)
...................................................
// self.thumbnail.image = UIImage(named: "image1")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
kd02 - I have found the answer to my question. When you asked if it was http or https i remembered that you have to enable http in xcode 9 now because of the security enhancement. I did that and now it works.
Try something like this
import UIKit
import Kingfisher
class DetailsViewController: UIViewController {
#IBOutlet weak var thumbnail: UIImageView!
var thumbnailURL: URL?
override func viewDidLoad() {
super.viewDidLoad()
if let url = self.thumbnailURL {
self.thumbnail.setImage(with: url)
}
}
}
I have feeling the optional URL is causing the problem.
Related
I understand it’s rather basic, but I’m only trying to get a grasp on basic functions.
I have produced some code by partially my own knowledge and partial bits from different guides.
I am not getting any errors, but the label is not displaying itself as “Text”. I believe it’s to do with the order/place my code is put.
Please help explain how I can fix this!
Please note as well:
I have just a single label called myLabel (named under the document section of my the identity inspector
It is has the text “Loaded” put into it already when I put it in.
I have no other code anywhere, only the default new project code.
I renamed the ViewController to ViewManager to avoid a class error.
First image: This is the image just so you know the location and other bits. I’ll attach the code too:
Second image: What I get, with no errors:
Third image: My main storyboard file:
And now it in code:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var myLabel: UILabel!
#IBAction func labelSet() {
myLabel.text = "Text"
}
}
Make sure that the IBAction is connected to Touch Up Inside in Interface Builder.
Change the signature of the IBAction to
#IBAction func labelSet(_ sender: UIButton) {
Your function func labelSet() isn't called anywhere. Neither in the Storyboard nor elsewhere.
You can call it in viewDidLoad() like this:
override func viewDidLoad() {
super.viewDidLoad()
labelSet()
}
Alternatively call it after the label has loaded.
#IBOutlet weak var myLabel: UILabel! {
didSet {
labelSet()
}
}
maybe I am missing something really fundamental here, but after staring at the code for an hour or so, my brain is going trough cycles and I would appreciate a fresh glance at this problem.
I have the following UIView:
import UIKit
protocol DetailViewWillShowUpDelegate {
func sendDetailOpened(_ openedBool: Bool)
}
class locationXIBController: UIView {
#IBOutlet weak var loationLabel: UILabel!
#IBOutlet weak var vsedniOteviraciDobaLabel: UILabel!
#IBOutlet weak var prijmajiKartyLabel: UILabel!
#IBOutlet weak var detailViewButtonOutlet: UIButton!
#IBOutlet weak var backgroundViewButton: UIButton!
let openedBool = true
var detailViewWillShowUpDelegate: DetailViewWillShowUpDelegate?
override func awakeFromNib() {
super.awakeFromNib()
}
#IBAction func vecerkaDetailButtonPressed(_ sender: UIButton) {
detailViewWillShowUpDelegate?.sendDetailOpened(openedBool)
print("pressed")
}
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
if let result = detailViewButtonOutlet.hitTest(convert(point, to: detailViewButtonOutlet), with: event) {
return result
}
return backgroundViewButton.hitTest(convert(point, to: backgroundViewButton), with: event)
}
}
Now the problem is, that when I call/press the vecerkaDetailButtonPressed function I get "pressed" output in the console but the protocol for some reason doesn't go trough.
The other side looks like this (stripped for simplicity):
class MapViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
let locationXIB = locationXIBController()
let isVecerkaDetailOpened = false
override func viewDidLoad() {
locationXIB.detailViewWillShowUpDelegate = self
}
extension MapViewController: DetailViewWillShowUpDelegate {
func sendDetailOpened(_ openedBool: Bool) {
isVecerkaDetailOpened = openedBool
print("success")
}
}
I know the protocol value at the moment of execution is nil. As I said, any help is appreciated, thanks!
First, a couple of naming convention issues:
The name locationXIBController is a bad choice for a UIView object. It is a view object, not a controller object.
Second, class names in Swift should start with an upper-case letter. So LocationXIBView would be a much better name for that view class.
Next, your code
let locationXIB = locationXIBController()
...is wrong. That creates a brand-new instance of your locationXIBController class that you never install in your view hierarchy. You should make that line an IBOutlet:
#IBOutlet weak var locationXIB: locationXIBController!
And then you should control-drag from the locationXIBController in your StoryBoard onto the outlet in your view controller. That will cause Interface Builder to connect the outlet.
Now when you run your program the variable locationXIB will be connected to the locationXIBController view from your storyboard/XIB when it's loaded.
In addition to the answer of #Duncan C, you might check whether you need super.viewDidLoad() at the top of the viewDidLoad() method in the MapViewController class? Not doing that can lead to quirky things in your app.
I asked:
So does detailViewWillShowUpDelegate actually point at anything, or is it nil?
And you replied:
I just tried debugging and it is actually nil
So that's the problem... you need to set detailViewWillShowUpDelegate to point to a valid delegate object. This is often done in the .xib file or storyboard, and sometimes people forget to make that connection, so check there if it makes sense. Else you'll just need to get a reference to the delegate at some point before the code in question can run and set it up.
Answer to the credit of #Paulw11
I finally managed to get it working by communicating like so:
step 1) 1:1 communication via protocol between MKAnnotation and MKAnnotationView
step 2) 1:1 communication via protocol between MKAnnotationView and MapViewController passing the same data
Finally works like a charm, thanks!
Using swift3 with xcode8
Below is my viewconroller.swift
class ViewController: UIViewController {
#IBOutlet weak var YahooWebview: UIWebView!
#IBOutlet weak var activity: UIActivityIndicatorView!
override func viewDidLoad() {
super.viewDidLoad()
let YURL = URL(string: "http://www.yahoo.com")
let YURLRequest = URLRequest(url: YURL!)
YahooWebview.loadRequest(YURLRequest)
}
}
func webViewDidStartLoad(YahooWebview: UIWebView) {
activity.startAnimating()
}
func webViewDidFinishLoad(YahooWebview: UIWebView) {
print("show indicator")
activity.stopAnimating()
}
Why my indicator is not showing when webview is loading?
I can not even see string "show indicator" from my log in Xcode.
You need to set your class as the delegate of UIWebView as
YahooWebview.delegate = self
Check my answer to get details regarding delegates and delegation pattern.
Note from apple developer: In apps that run in iOS 8 and later, use the
WKWebView class instead of using UIWebView. Additionally, consider setting the WKPreferences property javaScriptEnabled to false if you render files that are not supposed to run JavaScript.
There can be two problems as mentioned in comments.
Both can be solved in your Stroyboard/xib file.
Your UIActivityIndicatorView is may be hidden behind UIWebView. Just change the position of the view so that it comes above in the view heirarchy.
You may not have set delegate property of UIWebview class as your ViewController. Right click on webview and check. This needs to be set as you are animating the activity indicator view inside delegate methods.
I have the following code for a view which is one of the options attached to a tab on a Tab Bar Controller (swift 3):
import UIKit
class SecondViewController: UIViewController {
#IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
let str8REDURL = URL(string: "https://str8red.com/leaderboard/")
let str8REDURLRequest = URLRequest(url: str8REDURL!)
webView.loadRequest(str8REDURLRequest)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
This is working perfectly. However, the user can currently have a browse of the site and then select another tab. When they select this tab I would like the webView to reload the URL as above, so basically resetting that view as if it had just loaded. I am assuming there is some "reload" option but can't seem to work it out and would appreciate any guidance.
UPDATE
After a few suggestions I even tried the following with no joy:
func viewWillAppear() {
let str8REDURL = URL(string: "https://str8red.com/leaderboard/")
let str8REDURLRequest = URLRequest(url: str8REDURL!)
webView.loadRequest(str8REDURLRequest)
}
Many thanks, Alan.
Just put the load request in viewWillAppear(), it will be called everytime you change the tab back to that screen
webview.loadRequest() will load the webview content. webview.reload() reloads the last request which has finished loading. Therefore for your case, you can use webview.reload()
So I am trying to load a picture programmatically using Swift and I am having some trouble. I can change the UIImage of a view using the Attributes Inspector, so I know the picture I am using is added properly and I am using the right name.
Here is the code I am using:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var myPicture: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
myPicture.image = UIImage(named:"myNewPic")
}
}
I think there might be some setting I am missing in Attributes inspector or something since the code is pretty simple and I know the image is added properly.
Any help would be appreciated.
Make sure that your image name is correct. You have image properly in xcode and your outlet is properly connected
add image with extension if you are not using assets for images. like myNewPic.png or myNewPic.jpg whatever extension it is.
So, your code should be like,
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var myPicture: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
myPicture.image = UIImage(named:"myNewPic.png") //or .jpg or else
}
}
another choice
if you load image from assets usemyNewPic if you load from bundle use myNewPic.png (with extensions).
if let img = UIImage(named: "myNewPic") {
myPicture.image = img
} else {
myPicture.image = UIImage(named:"myNewPic.Imageextensions")
}