【发布时间】:2011-10-28 09:08:51
【问题描述】:
我在 GameScreen.m 文件上有以下方法,在 GameScreen.h 文件上有自己的声明 - (void) drawNumbers:
//GameScreen.h
#import <UIKit/UIKit.h>
@interface GameScreen : UIView
{
IBOutlet UIButton *cell00;
}
- (void) drawNumbers;
- (IBAction) onCellClick:(id)sender;
@property (nonatomic, retain) IBOutlet UIButton *cell00;
@end
//GameScreen.m
#import "GameScreen.h"
- (void) drawNumbers
{
//testing if this works, so far it doesn't
[cell00 setTitle:@"Whatever" forState:UIControlStateNormal];
[cell00 setTitle:@"Whatever" forState:UIControlStateHighlighted];
}
我正在尝试从我的 GameScreenViewController.m 文件中调用此方法,这样:
//GameScreenViewController.m
#import "GameScreenViewController.h"
#import "GameScreen.h"
...
- (void) viewDidLoad
{
GameScreen *aGameScreen = [[GameScreen alloc] init];
[aGameScreen drawNumbers];
[aGameScreen release];
[super viewDidLoad];
}
这应该更改 GameScreen.xib 文件中按钮的标题,其中 GameScreenViewController.m 是 viewController,GameScreen 类是事件处理程序,我在其中获得所有按钮点击、计时器运行等。我正在尝试从 [viewDidLoad] 调用 [drawNumbers] 因为我希望在屏幕显示在前面时更改标题(屏幕管理是通过 AppDelegate 文件完成的)。
问题是,如果我通过
从同一类内部调用 drawNumbers 实例//GameScreen.m
#import GameScreen.h
-(void) onButtonClick:(id)sender
{
//some other code
[self drawNumbers];
}
它可以工作(也就是说,代码实现或图形界面没有错)。
我浏览过 Apple 指南和互联网上的大量页面,但我似乎找不到任何线索。任何进一步的帮助(包括关于在 ADG 中确切找到答案的答案)将不胜感激。
(已编辑:此处是 AppDelegate 代码以翻转到特定视图,以防万一):
//myAppAppDelegate.h
#import <UIKit/UIKit.h>
@class myAppViewController, GameScreenViewController;
@interface myAppDelegate : NSObject <UIApplicationDelegate>
{
UIWindow *window;
myAppViewController *viewController;
GameScreenViewController *gameScreenViewController;
}
- (void) flipToGameScreen;
@property (nonatomic, retain) UIWindow *window;
@property (nonatomic, retain) GameScreenViewController *gameScreenViewController;
@end
//myAppAppDelegate.m
-(void) flipToGameScreen
{
GameScreenViewController *aGameScreenView = [[GameScreenViewController alloc] initWithNibName: @"GameScreen" bundle:nil];
[self setGameScreenViewController:aGameScreenView];
[aGameScreenView release];
[gameScreenViewController.view.frame = [[UIScreen mainScreen] applicationFrame];
[viewController.view removeFromSuperview];
[self.window addSubview:[gameScreenViewController view]];
}
【问题讨论】:
-
你能把GameScreen.h的内容贴出来吗?
-
GameScreen中的cell00实例变量是如何设置的?此处未显示,在您调用-drawNumbers方法时,它可能是nil。 -
按要求编辑。你也可以看到单元格实例变量。
-
在您的 GamesViewController.m viewDidLoad 方法中更改此行 [GameScreen release];到 [aGameScreen 发布];
-
这里的错字,我的错。解释编辑。您还需要 AppDelegate 文件吗?
标签: objective-c class init void alloc