Xcode - viewDidLoad方法不起作用

时间:2012-01-25 20:59:48

标签: xcode viewdidload

我是Objective-c和Xcode的新手,但我相信我对此有足够的了解,认识到这里出了问题。我有一个使用故事板的应用程序,并且应用程序中的一个视图没有运行其viewDidLoad方法。我不确定这是否相关,但我最近不小心删除了我原来的故事板,并且不得不再制作另一个故事板。原来没有问题,但是当我使用新的时,它不起作用。有问题的视图也是标签栏控制器的第一个视图,它可能是问题的一部分。

以下是负责视图(.h)的类的代码:

#import <UIKit/UIKit.h>
@interface TabSynth : UIViewController
@property (nonatomic, retain) IBOutlet UIImageView *tomeWater;
@property (nonatomic, retain) IBOutlet UIImageView *tomeFire;
@property (nonatomic, retain) IBOutlet UIImageView *tomeAir;
@property (nonatomic, retain) IBOutlet UIImageView *tomeEarth;
@property (nonatomic, retain) IBOutlet UIImageView *tomeDark;
@property (nonatomic, retain) IBOutlet UIImageView *tomeLight;
@property (nonatomic, retain) IBOutlet UIButton *airSynthButton;
@property (nonatomic, retain) IBOutlet UIButton *darkSynthButton;
@property (nonatomic, retain) IBOutlet UIButton *earthSynthButton;
@property (nonatomic, retain) IBOutlet UIButton *fireSynthButton;
@property (nonatomic, retain) IBOutlet UIButton *lightSynthButton;
@property (nonatomic, retain) IBOutlet UIButton *waterSynthButton;
@property (nonatomic, retain) IBOutlet UIButton *synthButtonLarv;
@property (nonatomic, retain) IBOutlet UIButton *synthButtonAmoeb;
@property (nonatomic, retain) IBOutlet UIButton *synthButtonLarv1;
@property (nonatomic, retain) IBOutlet UIButton *synthButtonAmoeb1;
@property (nonatomic, retain) IBOutlet UIImageView *tomeWater1;
@property (nonatomic, retain) IBOutlet UIImageView *tomeFire1;
@property (nonatomic, retain) IBOutlet UIImageView *tomeAir1;
@property (nonatomic, retain) IBOutlet UIImageView *tomeEarth1;
@property (nonatomic, retain) IBOutlet UIImageView *tomeDark1;
@property (nonatomic, retain) IBOutlet UIImageView *tomeLight1;

以下是(.m)的代码:

#import "TabSynth.h"

@implementation TabSynth

@synthesize tomeWater;
@synthesize tomeFire;
@synthesize tomeAir;
@synthesize tomeEarth;
@synthesize tomeDark;
@synthesize tomeLight;
@synthesize tomeWater1;
@synthesize tomeFire1;
@synthesize tomeAir1;
@synthesize tomeEarth1;
@synthesize tomeDark1;
@synthesize tomeLight1;
@synthesize airSynthButton;
@synthesize darkSynthButton;
@synthesize earthSynthButton;
@synthesize fireSynthButton;
@synthesize lightSynthButton;
@synthesize waterSynthButton;
@synthesize synthButtonLarv;
@synthesize synthButtonAmoeb;
@synthesize synthButtonLarv1;
@synthesize synthButtonAmoeb1;

-(void)viewDidLoad {
    extern int gTomeAir;
    extern int gTomeDark;
    extern int gTomeEarth;
    extern int gTomeFire;
    extern int gTomeLight;
    extern int gTomeWater;
    extern int gAmoebaeNum;
    extern int gLarvaeNum;
    synthButtonAmoeb.hidden=YES;
    synthButtonAmoeb1.hidden=NO;
    synthButtonLarv.hidden=YES;
    synthButtonLarv1.hidden=NO;
    if (gTomeAir>0) {
        tomeAir.hidden=NO;
        tomeAir1.hidden=YES;
        airSynthButton.hidden=NO;
    }
    if (gTomeDark>0) {
        tomeDark.hidden=NO;
        tomeDark1.hidden=YES;
        darkSynthButton.hidden=NO;
    }
    if (gTomeEarth>0) {
        tomeEarth.hidden=NO;
        tomeEarth1.hidden=YES;
        earthSynthButton.hidden=NO;
    }
    if (gTomeFire>0) {
        tomeFire.hidden=NO;
        tomeFire1.hidden=YES;
        fireSynthButton.hidden=NO;
    }
    if (gTomeLight>0) {
        tomeLight.hidden=NO;
        tomeLight1.hidden=YES;
        lightSynthButton.hidden=NO;
    }
    if (gTomeWater>0) {
        tomeWater.hidden=NO;
        tomeWater1.hidden=YES;
        waterSynthButton.hidden=NO;
    }
    if (gAmoebaeNum > 0) {
        synthButtonAmoeb.hidden=NO;
        synthButtonAmoeb1.hidden=YES;
    }
    else {
        synthButtonAmoeb.hidden=YES;
        synthButtonAmoeb1.hidden=NO;
    }
    if (gLarvaeNum >= 1) {
        synthButtonLarv.hidden=NO;
        synthButtonLarv1.hidden=YES;
    }
    else {
        synthButtonLarv.hidden=YES;
        synthButtonLarv1.hidden=NO;
    }
}

在if语句中,被调用的外部整数(gTome)被设置为等于上一个视图中的数字。任何帮助都会很棒,我为编码的简单性而道歉。

2 个答案:

答案 0 :(得分:2)

如果它没有调用viewDidLoad,那么首先要检查的是,在故事板中,您已将TabSynth正确设置为视图控制器的自定义类。另外,我在NSLog方法中看不到viewDidLoad。你怎么知道它没有被召唤?

顺便问一下,您是否知道可以将非UI对象放入笔尖或故事板中?我认为这可以帮助您简化代码。例如,您可以像这样定义类ElementViews

ElementViews.h

@interface ElementViews : NSObject

@property (assign, nonatomic) IBOutlet UIImageView *tome;
@property (assign, nonatomic) IBOutlet UIButton *synthButton;
@property (assign, nonatomic) IBOutlet UIImageView *tome1;

@end

然后在你的TabSynth课程中,而不是每个元素的三个出口,你只有一个出口:

TabSynth.h

@class ElementViews;

@interface TabSynth : UIViewController

@property (nonatomic, retain) IBOutlet ElementViews *waterViews;
@property (nonatomic, retain) IBOutlet ElementViews *fireViews;
@property (nonatomic, retain) IBOutlet ElementViews *airViews;
@property (nonatomic, retain) IBOutlet ElementViews *earthViews;
@property (nonatomic, retain) IBOutlet ElementViews *darkViews;
@property (nonatomic, retain) IBOutlet ElementViews *lightViews;

@property (nonatomic, retain) IBOutlet UIButton *synthButtonLarv;
@property (nonatomic, retain) IBOutlet UIButton *synthButtonAmoeb;
@property (nonatomic, retain) IBOutlet UIButton *synthButtonLarv1;
@property (nonatomic, retain) IBOutlet UIButton *synthButtonAmoeb1;
...

在故事板中,将通用的“对象”(看起来像橙色立方体)拖到TabSynth场景中。将其自定义类更改为ElementViews,并将其标识标签更改为Water Views。将waterViews的{​​{1}}插座连接到此新TabSynth对象,然后将ElementViews对象的插座连接到三个水景(ElementViewstome图片视图和tome1按钮)。重复其他五个元素。

然后您可以像这样简化synthButton方法:

viewDidLoad

答案 1 :(得分:-1)

好但是你必须在方法[super viewDidLoad]的开头写-viewDidLoad来调用viewDidLoad本身的Controller(父亲或其前身的层次结构在编写创建-viewDidLoad的派生版本的高级代码之前。

瞧,祝你好运。