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) 被设置为等于前面的数字看法。任何帮助都会很棒,我为我的编码的简单性表示歉意。
I am new to Objective-c and Xcode, yet I believe I know enough about it to realize that something is going wrong here. I have an application using a storyboard, and one of the views in the application is not running its viewDidLoad method. I am not sure if this is relevant, but I had recently accidentally deleted my original storyboard, and had to make another one. The original had worked great with no problems, yet when I use the new one, it does not work. The view in question is also the first view of a Tab Bar Controller, which might be part of the problem.
Here is the code from the class responsible for the view (.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;
And here is the code from the (.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;
}
}
In the if statements, the external integers being called (gTome) were set equal to a number in the previous view. Any help at all would be great, and I apologize for the simpleness of my coding.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果它没有调用
viewDidLoad
,那么首先要检查的是在故事板中您是否已将TabSynth
正确设置为视图控制器的自定义类。另外,我在您的viewDidLoad
方法中没有看到NSLog
。你怎么知道它没有被调用?顺便问一下,您是否知道可以将非 UI 对象放入 nib 或 Storyboard 中?我认为这可能会帮助您简化代码。例如,您可以定义一个
ElementViews
类,如下所示:ElementViews.h
,然后在您的
TabSynth
类中,不再为每个元素使用三个出口,而是只有一个出口:TabSynth.h
在故事板中,将通用“对象”(看起来像橙色立方体)拖到您的
TabSynth
场景中。将其自定义类更改为ElementViews
,并将其标识标签更改为Water Views
。将TabSynth
的waterViews
出口连接到这个新的ElementViews
对象,并将ElementViews
对象的出口连接到三个水视图(tome
和tome1
图像视图以及synthButton
按钮)。对其他五个元素重复此操作。然后你可以像这样简化你的
viewDidLoad
方法:If it's not calling
viewDidLoad
, then the first thing to check is that in your storyboard you have properly setTabSynth
as the custom class of your view controller. Also, I don't see anNSLog
in yourviewDidLoad
method. How do you know it's not being called?By the way, did you know that you can put non-UI objects in a nib or storyboard? I think this might help you simplify your code. For example, you could have a class
ElementViews
defined like this:ElementViews.h
and then in your
TabSynth
class, instead of three outlets for every element, you just have one outlet:TabSynth.h
In your storyboard, you drag a generic "Object" (looks like an orange cube) into your
TabSynth
scene. Change its custom class toElementViews
and its identity label toWater Views
. Connect thewaterViews
outlet of theTabSynth
to this newElementViews
object, and connect the outlets of theElementViews
object to the three water views (thetome
andtome1
image views and thesynthButton
button). Repeat for the other five elements.Then you can simplify your
viewDidLoad
method like this:好的,但是你必须在方法
-viewDidLoad
的开头编写[super viewDidLoad]
来调用Controller
的viewDidLoad
code> 本身(对象层次结构中的父亲或其前身),然后再编写创建-viewDidLoad
的派生版本的出色代码。瞧,祝你好运。
ok but you must write
[super viewDidLoad]
at the beginning of the method-viewDidLoad
to call theviewDidLoad
of theController
itself (the father or its predecessor in the hierarchy of object) before writing your superb code which create a derived version of-viewDidLoad
.voila, good luck.