Unexpectedly found nil when opening an UIImage - swift

I am developing a Walkthrough screen where I have added 3 instruction images which help the users to work with the application. The problem occurs, when I first launch the app I see only the labels with the "instruction tekst" , but below this label the user should also see the images. Maybe I am doing something wrong, attached I uploaded a picture of the error:
And here I upload the picture of the code I have used to transfer these picture to the TutorialPageContentHolderViewController:
I would be very thankful if you see my mistake, because I have been struggling with that the whole day.

I guess the imageFileName variable in your first image is nil when you use it in
myImageView.image = UIImage(named: imageFileName)
When you assign it a value in the second screenshot it could be too late, as viewdidLoad is loaded before that. You could check if this is true by manually assigning a valid value to imageFileName before using it and see if it works.
One simple way to fix it would be to add an initialiser to the view controller class, pass the filename as a parameter and store the filename in a local variable until you use it in viewDidLoad.
A second way would be to add a method to the page view controller as below (and remove the corresponding line from viewDidLoad):
func prepare(with filename:String) {
myImageView.image = UIImage(named: filename)
}
and call it from the pageTutorialAtIndex method in your main view controller.

Related

Swift 4: Image inside UIView only display after second UIViewController call

I inserted a UIView with a image inside a Cell View like tha image bellow.
What i tried:
DispatchQueue.main.async {
self.hideProfileDescriptionView.addSubview(self.hidedescriptionLogo)
self.hideProfileDescriptionView.bringSubview(toFront:
self.hidedescriptionLogo)
self.cellDescriptionView.addSubview(self.hideProfileDescriptionView)
}
In the First time I call the UIVIewController, the image does not display, but in the second time I call UIViewController the image appears.
How can I solve that.
Thanks
In the first case Image didnt load fully and second time download is completed. If you dont to wait this installations You can use KingFisher repo https://github.com/onevcat/Kingfisher .
This repo is highly recommended. Also with this repo you can put laoder in UIImageView.
I realized that all cell content was not being displayed, so i discovered a flag that i did put on the code and i did disable that. Solved.

Mac OS - Change Status Item Button Image

I want to update the image when the user clicks something in the application but I can't get it to work.
The status item with the menu is defined in the AppDelegate. I am trying to update the image in the ViewController with this piece of code which I think should work:
AppDelegate().statusItem.button?.image = NSImage(named:NSImage.Name("icon-orange"))
No errors are showing up, but turns out it still doesn't work, so is it possible to change the image or am I doing something wrong?
AppDelegate() creates a brand new instance which is not the delegate class in Interface Builder.
You need the real reference:
(NSApp.delegate as! AppDelegate).statusItem.button?.image = NSImage(named:NSImage.Name("icon-orange"))

UICollectionView with pictures

I am trying to appear pictures into the cells that related to UICollectionViewCell. but for some reasons the pictures do not appear on the cells and also when I run the app is shows some errors
here's the error first.
libc++abi.dylib: terminating with uncaught exception of type NSException
and this is my codes
enter image description here
thanks
The most obvious issue would be that you have no view with tag 2 set. Since you are forcing the value, instead of checking if you actually get a value there, you might get an exception at that point if the call to viewWithTag returns nil.
Try changing this line:
var imageView = cell.viewWithTag(2) as! UIImageView
To something like this:
if let imageView = cell.viewWithTag(2) as? UIImageView {
// Set the image here
}
If you don't get the crash after that, then the issue is in that bit of code. If you still get a crash, you might want to try uploading your project somewhere and then posting a link here so that anybody interested can take a look to see what is going on.

Need to use a NSTextView (or NSTextField) for a clickable URL

I'm given an arbitrary NSAttributedString (parsed from markdown, not that it matters here) which may contain URLs that I want to be clickable in a text field within an NSTableView cell. The requirements state that if the user clicks the URL, they be taken to it with the default browser. IF they click anywhere else in the cell, we have default behavior (displaying an additional info popup).
I'm attempting to use a NSTextView to display the content. However, clicking outside the URL but within the view selects the text and eats the mouse click. Making the view not selectable won't allow clicking the URL either. I also don't really want the text to be selectable but that's a minor side problem.
So... I decided to make my view controller an NSTextViewDelegate so I could use some of those callbacks. But my app crashes if I set the NSTextView's delegate property to 'self'. This happens even if I don't implement any of the functions, even though they are all optional.
I'm using Swift 3 and wonder if there's some bug or other issue there? The call stack appears to be sending a textView:willChangeSelectionFromCharacterRanges:toCharacterRanges: message even though it's not implemented. And incidentally, implementing that method isn't helping either.
Any help, or sample code in Swift 3 using the delegate protocol, would be greatly appreciated.
Here's the crash I get by simply setting the delegate property on NSTextView:
By request, here's the code that set's the delegate. Currently I just set it whenever the message changes. This can obviously be optimized but for now I just want to see it work.
var notification: SSNotification! {
didSet {
guard let notificationCellView = self.view as? SSNotificationCellView else { return }
notificationCellView.subjectLabel.stringValue = notification.subject
if let description = notification.message , description != "" {
let attrString = TSMarkdownParser.standard().attributedString(fromMarkdown: description)
notificationCellView.messageLabel.textStorage?.setAttributedString(attrString)
notificationCellView.messageLabel.isHidden = false
notificationCellView.messageLabel.delegate = self
} else {
notificationCellView.messageLabel.isHidden = true
}
}
}
I never did figure out why I was crashing but I was able to come up with a workaround. I was originally trying to make the view controller for the table cell which contained NSTextView be the delegate. I changed it so that the cell's view subclass itself was the delegate and all is well.
I don't get it but it works, so that's what matters.

how to change default image in UIActivityIndicator by SWIFT

In my app activityIndicator is necessary. But, i don't want that default symbol. If i want to replace any other image by using SWIFT means, how can i? Kindly help me.
You can't customize the spinning thing in a UIActivityIndicator. What I suggest is that you make your own indication of activity; for example, use an animated UIImage:
let im = UIImage.animatedImageNamed("myImages", duration:1)
... and show it (in an image view, probably) as a way of suggesting that activity is occurring.