Passing a float between multiple viewcontrollers - iphone

Im using tabs to switch between two view controllers.
How do I retrieve a float in the secondviewcontroller, thats been initiated in the firstviewcontroller? should i make some sort of global variable? Where and how do I do this?
Thanks guys :)

Global variables are never desirable, I would strongly recommend using some messaging pattern, s.th. SecondViewController and FirstViewController can synchronize whenever they change anything of interest to the other.
On first glance, I only found this guideline http://www.informit.com/articles/article.aspx?p=1398611 telling about messaging-patterns in cocoa, I guess there will already be sample-implementations for iPhone floating around.

You could make that variable a property of your app delegate, which would be accessible from anywhere within your app. If you don't want that for whatever reason, you could create a "helper" singleton to keep such variables and make them properties again.

Use AppDelegate For This
+(BOOL)SetData:(float)Value
{
GlobalValue=Value;
}
+(float)ReturnData
{
return GlobalValue;
}
and Call like This
[YourAppDelegate ReturnData];

Related

Instances of Objects in Objective-C - iPhone/iPad

I am developing an iOS app that is a calculator to keep track of a score of a certain game. The game has many view controllers. I am going to create a "Player" object and create 4 instances of that player. Now in terms of Objective-C, how would I keep those instances alive between several view controllers? Should I for example, create an array of players and keep passing the array from one VC to another as the view progresses?
Thank you,
using singleton class , you will get the anywhere the same object. you need not to create each and every time. it create once and you use the app whole.
You can create the 4 instances in AppDelegate.
Then these 4 instances will be accessible by all view controller using shared AppDelegate object.
Let me know in case of any difficulty.
Cheers.
Just a quick thought, Passing array of Players can resolve your problem. But quickest way to share objects between view controllers is through using Application Delegate as you can simply access application delegate anywhere.
you can use of ApplicationDelegate to store your progress. when you move to another view at that time update your ApplicationDelegate variable in - (void) viewWillDisappear:(BOOL)animated method and at another view in viewLoad method get the updated value from the delegate variable.

How to set the delegate to another ViewController?

I've recently started developing for the iPhone and so far I'm doing pretty good but there's this basic pattern I really don't seem to get.
Say, I have a TabBar with two views and a custom delegate protocol, thus my structure is the following:
AppDelegate.h/.m
myDelegateProtocol.h
FirstViewController.h/.m
SecondViewController.h/.m
MainView.xib
FirstView.xib
SecondView.xib
Now I want to achieve the following: I placed a button in the FirstView.xib and I'd like the IBAction which it invokes (inside FirstViewController ofc.) to send a message to the SecondViewController ([self.delegate tellSecondViewContrToSayHi]) and invoke another method which simply prints a log into the console saying "hi I'm here."
So far I know what I need to do in theory:
Specify the protocol.
Implement the protocol in the SecondViewController.
Create an id< myDelegateProtocol > delegate inside my FirstViewController,...AND last but not least:
Set the self.delegate = secondViewControllerObject.
Now, nr.4 is where the problem's at. How on earth do I link the delegate to the other viewController? I mean I'm not the one instantiating the views as the tabBar kinda does that for me,... any advise? Or am I just way too tired to notice a really stupid thing I did somewhere?
Theoretically the same question also applies to the target:action: thing,... I mean, how do I define the target?
Thanks a lot,
wasabi
You have the right idea, assuming that you want relatively tight coupling between these controllers via that delegate protocol.
Since neither controller knows about the other until that delegate property is set you need to have some object which has a reference to both of them wire up that relationship. In your case that's probably the application delegate which can create both controllers, set one as the delegate of the other, and pass both along to your tab bar controller.
What you might actually want is to have the app delegate give both controllers a reference to some shared model object. Your FirstViewController can update that model when you tap a button and your SecondViewController can observe changes to the model to update it's display (or just update its view when it appears based on the current model state). That way your controllers don't need to know anything about each other.

What is the best method to use data in different view controllers?

I am new to ios development and I am trying to use some methods in different view controllers in regular intervals of time (using same methods in different view controllers).
Can anyone please tell me what is the best method for this?
Thanks in advance!
I'd suggest using protocol for this. You can never know when you want different method behaviour for different controllers. And you can always implement default behaviour in private category of generic uiviewcontroller.
Maybe you could try to implement this method in a customViewController that would be subclassed by these viewController and than every viewController would have access to these method.
Alternate way would be to implement it on the appDelegate or on a common tabBarController, navigationController (or whatever) and so every viewController would have access to it.
If the time interval has to be the same for every viewControler, you should do only one time interval and iterate on your viewControllers

How can I use the value of a variable from one class in another in Objective-C?

I am fairly new to Objective-C and have been doing some iPhone programming.
Is it possible to look up the value of a variable in a different class?
Basically what I am doing is running a function that exists in my application delegate from a view controller, but the application delegate needs to use a variable stored in the view controller from which the application delegate function was called. Does it make sense?
You can always pass the variable into the function which needs it. Or you can make a getter for it
Fixed the problem by simply passing the variable to the app delegate and doing it that way.
It sounds to me that you need to expose that variable as a property.
You can create a singleton and store your data and have it available all over the place.

tabBar viewControllers in IB: send custom init?

My tabBarController-based app has several tabs. Each has a custom viewController class, the only difference being the way the instance is initialized. Is there a way to make interface builder send the different tabs custom init parameters?
Currently I'm doing the initialisation in viewWillAppear, but for a bunch of reasons it would make sense to do it in IB instead of in the code.
Any suggestions?
thanks,
Kelso
Interface Builder creates an archive of objects that is unarchived when you program executes. You can't really tell IB to call particular methods.
If you need to initialize before viewWillAppear: is called, you can do so in awakeFromNib, which is guaranteed to be called after all objects have been loaded and all outlets have been connected to their targets.
If you want to do initialization even earlier, you can do so by overriding initWithCoder: (see the NSCoding protocol for documentation). I don't know if it is documented anywhere, but that is the designated initialized for objects being decoded from an archive.
In all of the above, you won't be able to receive parameters, but in the code you should be able to access whatever you need with some judicious use of global variables. You can also use [[UIApplication sharedApplication] delegate] to get access to your application delegate object.
I don't think there's any way to change what methods are called by the IB runtime when your nib is loaded. If you described what you were trying to accomplish (i.e. why doing the setup in viewDidAppear doesn't work for you), you might get a suggestion of a better way to handle your initialization.