2016-04-14 21 views
0

我想調用AppDelegate.m中另一個類的方法。我已經做了以下情況:來自AppDelegate的調用方法無法識別的選擇器發送到實例錯誤

  1. 在MyClass.h,我有-void testMethod
  2. 在AppDelegate.m,我有
    #import "MyClass.h"
    MyClass * myClassInstance = [[MyClass alloc] init];
    [myClassInstance testMethod]
  3. 在AppDelegate.h,我有也進口
    #import "MyClass.h"

我想我已經做了一切這個問題的答案建議:calling a method inside someClass from AppDelegate Objective-C IOS

但我仍然收到「無法識別的選擇器發送到實例」錯誤?

什麼是缺失?

在此先感謝。

+1

用完整準確的錯誤信息更新您的問題。您已經省略了郵件的導入部分。用導致錯誤的確切代碼行更新您的問題。 – rmaddy

+0

你忘了在void的旁邊添加括號嗎?例如, - (void)testMethod –

+0

@Ke Cheng您需要在m文件中實現此方法。查看本教程http://roadfiresoftware.com/2014/06/how-to-implement-methods-in-objective-c/ – bazyl87

回答

-1

嘗試把方法調用中像一些的appDelegate生命週期方法:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  

    // Override point for customization after application launch.  
    Test* testObject = [[Test alloc] init]; 
    [testObject testMethod]; 
    return YES; 
} 

對於要使用實例化的東西像類:

Test.h文件

#import <Foundation/Foundation.h> 

@interface Test : NSObject 

-(void)testMethod; 

@end 

和Test.m文件

#import "Test.h" 

@implementation Test 


-(void)testMethod{ 
    NSLog(@"test"); 
} 

@end 

在這種情況下,應用程序初始化日誌消息出現在控制檯上。

相關問題