从class1 [alloc init]的方式调用类2实例是不工作的



我在gamesscreen上使用了以下方法。在gamesgreen .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调用这个方法。

//GameScreenViewController.m
#import "GameScreenViewController.h"
#import "GameScreen.h"
... 
- (void) viewDidLoad
{
   GameScreen *aGameScreen = [[GameScreen alloc] init];
   [aGameScreen drawNumbers];
   [aGameScreen release];
   [super viewDidLoad];
}

这将改变GameScreen中按钮的标题。GameScreenViewController. xib文件m是viewController和gamesscreen类是事件处理程序,我得到所有的按钮点击,计时器运行等。我试图从[viewDidLoad]调用[drawNumbers],因为我想改变标题时,屏幕被带到前面(屏幕管理是通过AppDelegate文件完成)。

问题是,如果我在同一个类中通过 调用drawNumbers实例
//GameScreen.m
#import GameScreen.h
-(void) onButtonClick:(id)sender
{
    //some other code
    [self drawNumbers];
}

它可以工作(也就是说,代码实现或图形界面没有问题)。

我已经浏览了苹果指南和互联网上的大量页面,但我似乎找不到任何关于这个的线索。任何进一步的帮助(包括关于在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]];
}

因为你的cell00是由NIB设置的,如果你只是做[[GameScreen alloc] init],它将是nil。只有在加载了相应的NIB(并且实际建立了连接)时才会设置它。

如果单元格可以在您的viewDidLoad中访问,则在GameScreen上创建一个属性并通过属性(或专用的initWithCell:或其他东西)传递它。

如果您在GameScreenViewController上有类似IBOutlet GameScreen *aGameScreen;的东西(并且还在同一NIB中建立了与cell00的连接),您应该访问它。

最新更新