How to use localized UIImage with specific language? - objective-c

I have settings in my App and want to give the user the possibility to switch the language of the app. The language can be different to the device setting.
For the NSStrings I found a solution but how can I do it with UIImages?
I've localized all my images but now they are chosen depending on the device language. What do I have to do to get this working? My only option that I have right now is to put the names in the Localizable.strings and load them from there.
Is there a better way to do this?
Thx ;-)

Something like:
- [NSBundle URLForResource:withExtension:subdirectory:localization:]
- [NSBundle pathForResource:ofType:inDirectory:forLocalization:]
should do it.
Then just use something like:
-[UIImage initWithContentsOfFile:]
-[UIImage initWithData:]
for creating a UIImage.

Related

Is there something similar to JOptionPane in Objective-C?

I'm trying to create a simple game in Objective-C. In Java I could use JOptionPane.showMessageDialog to create a simple window with a message or JOPtionPane.showInputDialog to create a simple input window... Is there's something similar in Objective-C? (Or Cocoa, or whatever I could use...)
I'm not familiar with JOptionPane, but you can use UIAlertView to do both of those things. By default, it displays a message with either one or two buttons. As of iOS 5, it also has style properties to give it a UITextField, which would make a simple input prompt.

How do I share NSNumberFormatter over multiple NIB files?

I'm working on an NSPersistentDocument based app that has a per-document currency setting.
Preferably, I would like to use a single NSNumberFormatter currencyFormatter for all my NIB files, but there doesn't seem to be any easy way to bind, say, NSTextField formatter to document.currencyFormatter or something similar.
I'm pretty sure it is possible to do this using code, adding bindings manually, but it is quite a bit of work since I have many fields using the currency formatter.
What is the best option to share the currency formatter across multiple NIB files?
Here I suggest two ways:
Subclass the NSNumberFormatter and make it be a singleton (the init method return the same object every time is called).Then drag an object in every xib file and set it's class to the name of the NSNumberFormatter subclass;
Just use the bindings inspector to bind each formatter to document.currencyFormatter.
PS: I'm not sure if I completely understand what you're asking, tell me if one of these would be a valid solution to you.

Setting Path of UIImage

I would like to know how to set the path (into folders) of a UIImage when I'm doing something like this:
[UIImage imageNamed:#"Sprites/Macbeth/Climbing/1.png"];
I want to to have it use the image entitled "1.png" in the folder Sprite, in that folder in Macbeth and in that folder Climbing. I want to be able to do that with one line of code since I have a lot of UIImages like that for sprite animations (all in arrays). Does anyone know how to do this? Thanks in advance!
iOS does not maintain the folder system you have. So this isn't possible. You would pull your image simply using this line of code.
[UIImage imageNamed:#"1.png"];
Instead of trying to nest folders try putting that into the file name like macbeth-climbing-1.png if you need to classify multiple 1.png's
Write a method or function that does the following:
Call [NSBundle mainBundle] to get your application bundle
Call -[NSBundle pathForResource:ofType:inDirectory:] to get the path to the image
Call +[UIImage imageWithContentsOfFile:] with that path

Using Interface Builder's System Media files in code

I have a question regarding the several images and sound files in the Media tab of IB's library: how can they be used in code?
I want change the image of a NSImageVew to NSRevealFreestandingTemplate, but how can I access it? I'm pretty sure there must be a better way than creating a hidden image view and pulling it from there. Any ideas?
Thank you!
Use NSImage's imageNamed: method to get system images. You can use any of the values found in NSImage's Constants section as the name. To get the image you specified, you would use:
[NSImage imageNamed: NSImageNameRevealFreestandingTemplate];
[imageView setImage:[NSImage imageNamed:#"NSRevealFreestandingTemplate"]];

Creating UIWebview programmatically and using it to do a couple of things then destroy it?

Is it possible to create a hidden UIWebView programmatically and then release it(destroy it completely deallocating all of it's memory) when I have used it? If so, could you give some tips. Thanks!
It seems odd that you want to use a hidden UIWebView object for some task when the main purpose of an UIWebView object is to present the html content. The answer to your question is yes. You can create them and remove them programmatically just like any other view as #Nil has mentioned but it seems your main purpose behind this is to get the content. If that's so, you have many other ways you can get that content without having to create a UIWebView object. You can use NSString's initWithContentsOfURL:encoding:error: method to get the contents of the url into a string which you can parse and use. NSData has a similar method. However to use these you will have to perform them in the background otherwise they will block the main thread. The most obvious and better way would be to use NSURLConnection or its popular counterpart ASIHTTPRequest.
Yes, you can create and destroy a uiwebview as any other uiview.