【问题标题】:Objective-C: Calling class 2 instance from class1 [alloc init] way is not workingObjective-C:从 class1 [alloc init] 方式调用类 2 实例不起作用
【发布时间】: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


【解决方案1】:

由于您的cell00 将由NIB 设置,因此如果您只需执行[[GameScreen alloc] init],它将为零。只有在加载了相应的 NIB(并且实际建立了连接)时才会设置它。

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

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

【讨论】:

  • 那我不明白;如果通过特定方法从 AppDelegate 触发所有视图 -(void) fliptoScreenX 并添加视图,则在显示 xib 时不要同时加载 viewController 和类,因为 viewController 与 File's Owner 和 GameScreen 相关风景?
  • "small" 解决方法:如果我在 ViewController 类上有 IBOutlets,在 GameScreen 类上有 IBActions,它会起作用吗?我有 20 多个按钮,这意味着将所有变量加倍,这不是一件好事……可以吗(从“保持清洁和简单”的角度来看)?
  • NIB/XIB 被反序列化。这意味着其中的所有对象都会被解码(通过NSCoder),并且关联/连接是在加载期间建立的。一旦您到达viewDidLoad,就完成了。然后,您创建的所有对象都与 NIB/XIB 没有任何关系。如果您执行[[GameScreen alloc] init],则该实例的IBOutlets 都不会连接。请务必阅读资源编程指南的整个Nib Files部分。
  • 感谢您的提示,非常感谢。将这样做,同时我决定稍微扭曲我的代码:AppDelegate 将用作视图管理器,ViewControllers 将用作输出,而类将用作输入。这样我就可以坚持 MVC 模型并且仍然有干净的代码......再次感谢,确实。
猜你喜欢
  • 1970-01-01
  • 2010-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-13
  • 2016-04-25
  • 2012-01-23
  • 1970-01-01
相关资源
最近更新 更多