Mac OS - Change Status Item Button Image - swift

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"))

Related

Swift UIKit: Unable to update UIButton X when pressing UIButton Y [duplicate]

I was trying to make a simple tic-tac-toe app in Swift, so I set up 9 buttons with tags from 1 to 9 and call setImage to set noughts or crosses. This is working as intended.
The problem comes when trying to reset the board, where I call this piece of code:
for i in 1..<10 {
if let button = view.viewWithTag(i) as? UIButton {
button.setImage(nil, for: .normal)
}
}
This should remove the image from the button, but it does nothing. The button is set to Custom in the storyboard, and the tags are indeed assigned as they should. I also tried getting an outlet for one of the buttons and calling setImage(nil, for: .normal) in that one and it didn't work either.
I even created a new project with just a button where I call setImage, and it is indeed working for non-nil images but not with nil as value.
Has Apple changed the way of removing images? Another question(When button pressed, button.setImage(nil) doesn't work) seems to have the same problem, but if I work with isHidden I can no longer click on the buttons, and it should not be the workaround, looks very hacky to me.
Is this a bug on iOS? Did the implementation change or something?
Thank you.
This seems like it may be a bug in iOS 15, but it still works if use the new UIButton Configuration API you set its configuration's image instead:
button.configuration?.image = nil
Be sure to also configure your button's image through its configuration property when you want it to have an image on iOS 15, too

Core Data detail view Error

I have been working on an app that has a tableview where users can add information on a seperate ViewController via UITextField. I am now trying to set it up so the user can tap on any given Cell to be able to view the data back on that ViewController or edit it again. Everything is being saved using CoreData.
Best way is you can put the check like this. So that it will never crash or throw the error:-
if let sender = sender as? String {
guest.player = sender
}

Dynamically Updating A Tab Bar Item In the More ViewController Table

OK. I haven't actually seen how to do this anywhere. It may be a question of "You can't get there from here." or "Holy ##$! That is such a disgusting hack it should be taken behind the woodshed and shot!".
I have a tabbed Swift 3 iOS app that will dynamically update the Tab Bar images of selected pages as the page state changes.
I do that sort of like this:
if let navController = self.navigationController as? MyNavController {
navController.tabBarItem.image = navController.tabBarImage
navController.tabBarItem.selectedImage = navController.tabBarImage
}
The tabBarImage is actually a calculated property. This snippet is called within a UI callback that updates when the state changes.
This works great.
When in the MoreViewController, though, not so great. Those images remain stubbornly static, no matter what I do.
I have done some exploration of the MoreViewController. I can get at the tableView and the cells, but that smells like the kind of hack that will get my app taken behind the woodshed by the Blue Meanies at App Review.
Is there a proper way to do this?
You can use Notification and pass the images within a Dictionary as a Notification object. Then you can get different tabBarImage with different key value at once.
OK. I figured out how to do it.
I was crawling back along the navigation controller path; which worked fine for items not in the more space.
I fixed it by crawling forward from the tab bar controller, instead:
self.tabBarController?.viewControllers?[MySelectionIndex].tabBarItem.image = self.tabBarImage
self.tabBarController?.viewControllers?[MySelectionIndex].tabBarItem.selectedImage = self.tabBarImage

Unexpectedly found nil when opening an UIImage

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.

Adding User Location Alert on MapView Loading - Xcode

I am working on Map View project but when my app loads up i get this alert on the very first screen
'"Project Name Would Like to Use Your Current Location"(Alert Message) "Don't Allow"(button) "OK"(button)'(Location Alert Box)
before the Map View it shows me alert on the first view i want when i switch to Map View then only the alert should come up so that at that time user can click "OK" and App will be able to use the location of user, i have searched alot but did not found some good ways to do it, i know it can be done because i have seen one or two app doing that thing but i am not able to do this feature in my app ... plz help me out in this...
I just want that the alert of location search should only show up when i reach on map view screen of my application not before that .. any suggestions ?? coding will be much appreciated.
You can simply not instantiate your CLLocationManager until you reach the screen where you want the alert to appear. It is the instantiation of the location manager that is prompting the OS to display the alert.
As GeraldWilliam already explained, its the CLLocationManager that forces the popup, which you cannot alter.
However, what you could do is show the mapview and ask the user for its current location when the view is loaded, e.g. using the – viewDidAppear: method.