2011-03-27 53 views
0

我试图创建一个包含在应用程序代理中创建的类的应用程序。 我初始化:我的应用程序代理出现问题

Mobile *tmp = [[Mobile alloc] init]; 
    mobile = tmp; 
    [tmp release]; 

,然后我尝试在我的应用程序与此用它在其他类:

projectAppDelegate *delegate = (projectAppDelegate *)[[UIApplication sharedApplication] delegate]; 
    mobile = delegate.mobile; 

但是当我做这样的事情:

[mobile enter:x :y]; 

它崩溃...

有没有我做错了,或有任何解决方案制作一个类,应用程序中的所有其他类可以使用的类?

谢谢。

+0

“enter”方法的签名是什么?你似乎缺少一个参数名称。 – 2011-03-27 22:51:22

+0

他没有进入输入法,因为手机是零。有什么我做错了? – MTA 2011-03-27 23:37:02

回答

0

如果你想使用你必须将它们存储为应用程序委托的性质你对象的实例。

//appdelegate.h 
// 
//... 
// 
@interface AppDelegate : NSObject <UIApplicationDelegate> { 
    Mobile *tmp; 
} 
//... 

//appdelegate.m 
// 
//... 
// 
- (BOOL)application:(UIApplication *)application 
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 

    mobile = [[Mobile alloc]init];  
} 
//... 
- (void)dealloc { 
    [mobile release]; 
    [super dealloc]; 
//... 
} 

比你要得到一个指向您的应用程序委托的共享实例,并打电话给你mobile财产。

//... Somewhere 
AppDelegate* ref = (AppDelegate*) [[UIApplication sharedApplication] delegate]; 
NSLog(@"%@", [ref mobile]); 
//... 
+0

谢谢,我试图调用,我已在从与该另一个类的移动类的方法: 'projectAppDelegate *代表=(projectAppDelegate *)[[UIApplication的sharedApplication]委托]; \t Mobile * mobile = delegate.mobile; \t [手机输入:X:Y]' 他并没有跳到这个方法,你也许知道为什么吗? – MTA 2011-03-27 23:07:46

+0

请确保*移动是不是零 – asdf 2011-03-27 23:15:04

+0

你也许对这个问题的任何解决方案? – MTA 2011-03-27 23:25:06

0

在您的第一个代码片段中,您正在有效地创建并立即销毁该对象。如果对象是应该坚持这个方法执行完毕后,你应该只使用

mobile = [[Mobile alloc] init]; 
+0

谢谢you.i尝试从另一个类中调用我在另一个类中使用的方法:'projectAppDelegate * delegate =(projectAppDelegate *)[[UIApplication sharedApplication] delegate]; \t Mobile * mobile = delegate.mobile; \t [手机输入:x:y];' 他没有跳到这个方法,你可能知道为什么? – MTA 2011-03-27 23:09:30

相关问题