在iphone dev中传递浮动进出app appate

时间:2009-08-25 21:38:31

标签: iphone

我正在开发具有多个视图控制器的iphone应用程序。我现在能够传递一个int值到app delegate和从app delegate传递如下(我想使用float而不是!但是不确定如何):

app delegate.h

@interface { 
    NSInteger accumulatedTime;
}

@property (nonatomic) NSInteger accumulatedTime;

@end

app delegate.m

@implementation

@synthesize accumulatedTime;

然后在我的视图controller.h中

@interface {
    int secs2;
}

查看controller.m

BetaTeam1AppDelegate *mainDelegate = (BetaTeam1AppDelegate *)[[UIApplication sharedApplication] delegate];

secs2 = mainDelegate.accumulatedTime ;  

我想使用一个浮点数,很容易在视图控制器.h和.m中更改int以浮动但是如何在应用程序委托中编码?

3 个答案:

答案 0 :(得分:1)

只需将accumulatedTime的类型更改为float即可。 floatint在这里没有什么不同;两者都只是C类型,属性可以返回C类型。

那就是说,你应该让accumulatedTime属于NSTimeInterval类型,因为这将与Cocoa时间例程更加一致。 NSTimeInterval通常以double实现。

答案 1 :(得分:0)

NSNumber可以封装CGFloat。

NSNumber *accumulatedTime;
CGFloat secs2 = [mainDelegate.accumulatedTime floatValue];

答案 2 :(得分:0)

考虑将此全局信息放在不同的位置,例如MVC主题中的Model对象。即使它是Singleton,它也比设置这样的App Delegate更好。