逻辑错误还是我的程序跳过部分?

时间:2009-07-09 03:12:01

标签: objective-c uiviewcontroller delegates uiwindow logical-components

仅供参考我正在使用Objective-C进行编程,但任何人都可以提供帮助。过去两个小时我一直坐在这里试图弄清问题是什么,我已经做了我所知道的一切来调试这个简单的小编码问题。看看下面的代码,我会解释。在应用程序中,它从MainScreenViewController开始。我的应用程序为用户提供了两种选择...选择“主”按钮或“工作”按钮(基本客户端 - 服务器模型)。根据用户的选择,应用程序然后应该加载另一个视图(MasterViewController或WorkerViewController)。现在,当我运行应用程序时,如果你已经按照打印命令并假设选择了“主”按钮,那么在我的程序中,它打印出“masterButtonPressed - Stage 1”和“masterButtonPressed - Stage 2”,但没有别的。视图也没有改变,这告诉我问题在于AppDelegate部分的代码。在我将打印命令放在那里并运行应用程序之后,它仍然不会打印我在AppDelegate部分中的两个语句。我在正确的文件中有正确的#import语句,我只是没有在这里包含它们,因为它占用了无用的空间。我可以编译代码没有错误,没有警告。从理论上讲,当我按下“主”按钮时,我应该在控制台中看到的是四行,它们是(按顺序) - “masterButtonPressed - Stage 1”,“changeToMaster - Stage 1”,“changeToMaster - Stage 2”,和“masterButtonPressed - 第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 个答案:

答案 0 :(得分:2)

在您将其声明为实例方法时,看起来您正在尝试调用类方法:+[ErwinAppDelegate changeToMaster]。您需要将引用保留在MainViewController中的app委托对象中 - 目前您只处理静态(类)方法。

执行此操作的“正确”方法是在MainScreenViewController中创建委托协议,在将该视图添加到ErwinAppDelegate中的窗口后,设置mainScreenViewController.delegate = self

以下是有关创建和使用代理的一些非常有用的信息:How do I create delegates in Objective-C?

答案 1 :(得分:0)

只是一个猜测,但您可能希望ErwinAppDelegate的一个实例将消息发送给...

应该有一些ErwinAppDelegate eadeleg; [eadeleg changeToMaster];没有?