开发者

Passing a float between multiple viewcontrollers

开发者 https://www.devze.com 2023-01-17 10:53 出处:网络
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 variabl

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];
0

精彩评论

暂无评论...
验证码 换一张
取 消