0

僅供參考我正在編程Objective-C,但任何人都可以提供幫助。過去兩個小時,我一直坐在這裏試圖弄清楚問題所在,並且我已經盡我所能調試這個簡單的小編碼問題。看看下面的代碼,我會解釋。在應用程序中,它始於MainScreenViewController。我的應用程序爲用戶提供了兩個選擇...選擇一個「主」按鈕或「工作人員」按鈕(基本客戶端 - 服務器模型)。根據用戶的選擇,應用程序應該加載另一個視圖(MasterViewController或WorkerViewController)。現在,當我運行應用程序時,如果您已經按照打印命令並假設選擇了「主」按鈕,那麼在我的程序中,它會打印出「masterButtonPressed-Stage 1」和「masterButtonPressed-Stage 2」,但沒有別的。該視圖不會更改,它告訴我問題出在AppDelegate部分的代碼中。將打印命令放入並運行應用程序後,它仍然不會打印出AppDelegate部分中的兩條語句。我在正確的文件中有正確的#import語句,我只是沒有在這裏包含它們,因爲它佔用了無用的空間。我可以在沒有錯誤和沒有警告的情況下編譯代碼。理論上,當我按下「master」按鈕時,我應該在控制檯中看到四行,它們是(按順序) - 「masterButtonPressed - 階段1」,「changeToMaster - 階段1」,「changeToMaster - 階段2」,和「masterButtonPressed - Stage 2」。有人能指出我出錯的地方嗎?就像我說的那樣,按下「主」按鈕後視圖不會改變。謝謝你的幫助!邏輯錯誤或者是我的程序跳過部件?

MainScreenViewController.h

@interface MainScreenViewController : UIViewController { 

} 

-(IBAction)masterButtonPressed; 
-(IBAction)workerButtonPressed; 
@end 

MainScreenViewController.m

@implementation MainScreenViewController 
-(IBAction)masterButtonPressed { 
    NSLog(@"masterButtonPressed - Stage 1"); 
    [ErwinAppDelegate changeToMaster]; 
    NSLog(@"masterButtonPressed - Stage 2"); 
    } 


-(IBAction)workerButtonPressed { 
    NSLog(@"workerButtonPressed - Stage 1"); 
    [ErwinAppDelegate changeToWorker]; 
    NSLog(@"workerButtonPressed - Stage 2"); 
} 
@end 

ErwinAppDelegate.h

@class MainScreenViewController, MasterViewController, WorkerViewController; 
@interface ErwinAppDelegate : NSObject <UIApplicationDelegate> { 
    UIWindow *window; 
    MainScreenViewController *mainScreenViewController; 
    MasterViewController *masterViewController; 
    WorkerViewController *workerViewController; 
} 

@property(nonatomic, retain) IBOutlet UIWindow *window; 
@property(nonatomic, retain) IBOutlet MainScreenViewController *mainScreenViewController; 
@property(nonatomic, retain) IBOutlet MasterViewController *masterViewController; 
@property(nonatomic, retain) IBOutlet WorkerViewController *workerViewController; 

-(void)changeToMaster; 
-(void)changeToWorker; 

@end 

ErwinAppDelegate.m

@implementation ErwinAppDelegate 

@synthesize window, mainScreenViewController, masterViewController, workerViewController; 

- (void)applicationDidFinishLaunching:(UIApplication *)application {  

    // Override point for customization after application launch 
    [window addSubview:mainScreenViewController.view]; 
    [window addSubview:masterViewController.view]; 
    [window addSubview:workerViewController.view]; 
    [window makeKeyAndVisible]; 

    // Bring first view up front 
    [window bringSubviewToFront:mainScreenViewController.view]; 
} 


-(void)changeToMaster { 
    NSLog(@"changeToMaster - Stage 1"); 
    [window bringSubviewToFront:masterViewController.view]; 
    NSLog(@"changeToMaster - Stage 2"); 
} 


-(void)changeToWorker { 
    NSLog(@"changeToWorker - Stage 1"); 
    [window bringSubviewToFront:workerViewController.view]; 
    NSLog(@"changeToWorker - Stage 2"); 
} 
@end 

回答

2

看起來您嘗試調用類方法:+[ErwinAppDelegate changeToMaster]而您已將其聲明爲實例方法。你需要在你的MainViewController中保留一個引用回到應用程序委託對象 - 目前你只處理靜態(類)方法。

執行此操作的「正確」方法是在MainScreenViewController中創建委託協議,並將該視圖添加到ErwinAppDelegate中的窗口後,設置爲mainScreenViewController.delegate = self

下面是創建和使用委託一些非常有用的信息:How do I create delegates in Objective-C?

+0

對不起,你已經說明了一切都很有意義,但我有一個問題......當你說「將視圖添加到窗口後,你指哪一個視圖?」您是否在將mainScreenViewController視圖添加到窗口後將其聲明爲委託? – 2009-07-09 04:44:07

+0

我的意思是:在你運行`[window addSubview:mainScreenViewController.view]`之後`你會做一些像`mainScreenViewController.delegate = self`的東西。然後在你的MainScreenViewController實例中,你可以直接調用[self.delegate changeToWorker]等。 – pix0r 2009-07-10 04:42:09

0

只是一個猜測,但你可能希望你的ErwinAppDelegate的實例發送消息給...

應該有一些ErwinAppDelegate eadeleg; [eadeleg changeToMaster];沒有?