How do I share NSNumberFormatter over multiple NIB files? - objective-c

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.

Related

How to create a reusable subview with controls, without using xib file

I'm coding an app, and have several views that are reused (f. ex. containing a textfield, a label and a button)
Now i would like to just create it in a "globalviews.m" file once and add it as a subview to the mainview. (I really don't like the interface builder)
I could easily create a function that returns a UIView, with the components in them, but i would like to access the controls of course.
I was hoping something like (making "searchview" global in the viewcontroller in use)
// making searchview a global thingy
UIView *seachview ;
// rest of code here and then in viewdidload:
UIView *seachview = [[UIView alloc] init] ;
searchview = [[globaviews alloc] thesearchviews_name] ;
[self addsubview:searchview] ;
But how could I make controls inside easily accessible. Like:
NSString *something = searchview.textviewname.text ;
Or would this be a terrible idea to begin with?
Its just the way I would prefer to code...
You can create a custom class that is a subclass of UIView. You could then add properties for each control (the same way you would add NSString, NSNumber etc). Or you could create public methods to modify / get data.
e.g.
- (void)setTextFiledColour:(UIColor *)color;
- (NSString *)getTextFieldText;
My personal opinion (from a lot of experience) is to learn interface builder and deal with it. It is perfectly possible to do what you want and many people agree with you and choose to do it that way. Personally I've never seen it done "right". Meaning that its all done custom to create their own patterns and methodologies, avoiding years of experience and testing that has gone into the patterns provided by interface builder.
I find that storyboards in particular force a very specific pattern and style of coding. I think moving away form that is a huge mistake, as if used correctly it has great potential to abstract away UI / Code, prevents taking shortcuts that come back later on and most importantly when someone else needs to change it, there is no ambiguity, you can't make a mistake with class names or variable names etc.
I've used storyboards a lot and have yet to have an issue with them, on the flip side i've worked with developers who insist on doing it all by hand and have never had so many issues, and shocked at how long it takes to develop applications in this manner.
In the end its up to you.
Note
You mentioned wanting to create a view and reuse it. Its also possible to do this in a .xib file and reuse it that way. Losing some of the storyboard abilities but might give you a little of both worlds.

What is the best way to delegate UIButtons,UITextFields etc?

In all my apps I am delegating all my UIButtons,UITextField,pickers etc.I am able to delegate these by 3 ways.
1-I am simply control+dragging the buttons,text field etc from the story board to the .h file which creates the delegation directly.
2-In .h file I am creating the buttons,text fields etc and then making the connections.
3-Programmatically doing the delegations
I want to know which is the best way to do it.
Best way is to use storyboard for referencing outlets and delegates. If you are not able to use storyboards then you do by codding. Storyboards will save your time of codding there is no major difference then this.
That is depending on you,you can do any way based on your
requirement,but Final result is same for all three methods.
if you want to create dynamic labels and buttons at that time you must
use programatical method because you don't have any other solution
,like that you can do it by your choice based on your requirement.
They are equivalent. It's the same question as "is it better to write my UI programmatically or design it with Interface Builder?". The end result is the same; you should think about Interface Builder as simply a convenience tool and mechanism for creating interfaces and logic quickly. And if you need, you can mix both methods without problems.
When you drag your control from the IB sheet to your controller's code, you're creating a target-action association that is stored in the .xib file. When the .xib file is unpacked at runtime, your controls are created and their connections are restored, the same as if you did that programmatically.

Objective-C: Creating a file purely for holding variables

I would like to add a file to my project, who's sole purpose would be to hold an array. I would then #import this file wherever I need to add/get from the array.
My problem is that when I create a new file (I'm using Xcode 4), I'm not sure what type of template to choose, and then what subclass to choose.
My reason for doing all of this is because I have a SplitView-Based app, and one of the views has a textfield, where I am trying to output data. My problem is that whenever I switch to a different view and then switch back, only the most recent entry is there. I am not 100% why that is but I suspect it is because when I switch to a different view, the current view is forgotten about, along with the variables in it.
This is not a good way to do it. There are many ways to do what you want: prepareForSegue: if you are using storyboards, delegation, instantiating your viewcontroller in code and setting a property in the header-file..those are just a few ways.
The way you are proposing is a slippery slope to bad Objective-C code and is only going to cause you more headaches in the future. Take the time to learn to do it right.
Check out this to get you thinking in the right direction.
How you save your data doesn't appear to be your problem. Take a look at the MVC design pattern and how view controllers implement it. They often rely on a dataSource protocol, which links the data from a "Model" to your "View" in a logical way to achieve your intended purpose.
The view controller should then be able to assign a delegate (usually itself (self) to keep the view populated with the correct data, whether the view gets unloaded or not.
If your view controller doesn't refer to a data source or a corresponding protocol, it would still be worth your time to see how you might take advantage of that design pattern. It will pay off in the long run to know this.
Instead of saving variables to a text file, you should consider using NSUserdefaults instead.
But I don't think that's the real solution to your problem, just wanted you know that there are other ways than saving stuff to a text file.

Can I convert a non-xib app to use xibs?

I have a working app with a navigation controller, two view controllers, two views, a model object, and no xib file. The views are created programatically. Now I'd like to build the same app using IB and xib files. Does anyone know of a writeup that explains how to add xib files and delete the program statements they replace? If not, then something that discusses the equivalences between xib library elements and program statements?
Why do I want to do this? Because I'm trying to understand what xib files do and how they interact with program statements and I thought this would be a good learning exercise. I made one try at it but couldn't get it to compile. In fact, every time I use xib files, except following textbook examples, I get hopelessly tangled up in code that won't compile or doesn't work if it does compile. So I must have some basic misconception. I need to figure out what xib files are doing behind the scenes. They seem potentially very powerful but I won't use them if I don't understand what they're doing.
I read elsewhere in stack overflow that they are a serialized version of a compiled view (or something like that). I understand that conceptually. I'm thoroughly familiar with Python's pickle files. But that doesn't explain much in practice.
A nib/xib contains the information needed to create and connect objects together. Basically, it contains instance variable and class information for the objects it contains. To load one, you use the UINib class (NSNib in Mac OS X), or the NSBundle loadNib… methods. NSBundle's methods are easier to use, and I would suggest using them unless you will load a nib several times. For iOS, you would typically use [[NSBundle mainBundle] loadNibNamed:#"NibNameWithoutExtension" owner:self options:nil];. The owner does not have to be self. It is the object represented by the "File's Owner" object in IB and will receive any connections made to that object. The options parameter can be used to get the top level objects in the nib, but you usually use outlets instead. When a nib is loaded, the objects inside it are allocated and instantiated (using initWithCoder:), and connections are created using setter methods, or by setting the instance variables directly.

Passing variables to different view controllers

I've searched and searched but nothing has really worked.
I'm trying to set a textvalue from a text box, into a string or whatever, so that I can call it up later in a different view controller. I can't seem to get it to work!
I'd also like numbers to be carried over, such like currency's.
Any ideas on them?
Cheers.
You could make an instance variable on the other view controller retain or copy the value before you push/pop the view. For example:
OpenNextViewController *varNextPageController = [[OpenNextViewController alloc] initWithNibName:#"OpenNextViewController" bundle:nil];
varNextPageController .textString= self.textString;
[[self navigationController] pushViewController:varNextPageController animated:YES];
[varNextPageController release];
In "OpenNextViewController" in this example have an instance variable "textString" that retains or copies (depending on your needs) your text.
Spend some time trying to grok the Model View Controller pattern.
In your case you may be looking to share data between different views sharing a common Model. The Model is the store of your data, in your case the textvalue.
Your question is a bit vague. Could you give any more specifics.
It sounds like you want to know:
How to get values from controls. In the case of a text field there should be a text property you can get the value from.
How to share values between controllers. Not sure exactly what you mean. The controller usually orchestrates the sharing of values between different views by using a model as the authoritative version of the data.
Again, if you can be any more specific we may be able to help more
If you want it in several controllers, then I would think you need to run it through the model?
Do you guys think that using the AppDelegate as the holder for one's model is fundamentally wrong? I mean AppDelegate is easily visible to all controllers so it's easy to bind to and get/set it's properties.
pom