English 中文(简体)
自动字生成器
原标题:Automatic word generator

(我在iOS版的Xcode中工作)

我正在制作一个文字游戏,当我从菜单中单击“游戏”时,它会切换到游戏视图。我想让它自动显示我插入的一个随机单词。我读到我必须把我的代码放在ViewDidLoad中,我做到了。

然而,该代码最初是为了使用按钮,然后显示一个随机单词,所以我不知道如何编辑代码。

在这里:

.h

IBOutlet UILabel *textview;

}

-(IBAction)random;

和.m文件

- (void)viewDidLoad
{

[super viewDidLoad];
// Do any additional setup after loading the view from its nib.

-(IBAction)random { 
    int text = rand() % 5;
    switch (text) {
        case 0:
            textview.text = @"BLUE";
            break;
        case 1:
            textview.text = @"GREEN";
            break;
        case 2:
            textview.text = @"RED";
            break;
        case 3:
            textview.text = @"YELLOW";
            break;
        case 4:
            textview.text = @"PINK";
            break;
        default:
            break;
    }
}

我希望有人能帮我,谢谢

最佳回答

假设所有东西都是视图控制器的一部分,只需从viewDidLoad中调用random()方法。

问题回答

只需将您的代码从IBAction切换到您的viewDidLoad-确保(如果您使用IB或故事板)您的代码链接了UILabel(确保您在.m中合成它)-

@property (nonatomic, retain) IBOutlet UILabel *myLabel;

…然后在视图中DidLoad-

self.myLabel.text = whateverText;

(您不必将其作为属性,但它使内存管理变得更容易)

此外,使用UILabel的名称“textview”可能不是一个好主意,因为你很容易认为它是UITextView,这是一种不同的动物。。。





相关问题
How to change out-of-focus text selection color in Xcode?

Okay, I ll bite. I ve got really pleasant code/window colors set up in Xcode. Ordinarily, my selection color is very visible. When I am doing a project search and iterating through the results, ...

Iphone NSTimer Issue

Hi I am new to objective c. I am trying to make an app for iphone. I have a button on my view, and the click on which the function playSound is called. This is working properly. It does plays the ...

Include a .txt file in a .h in C++?

I have a number of places where I need to re-use some template code. Many classes need these items In a .h could I do something like: #include <xxx.txt> and place all of this code in the ....

Iterating over string/strlen with umlauted characters

This is a follow-up to my previous question . I succeeded in implementing the algorithm for checking umlauted characters. The next problem comes from iterating over all characters in a string. I do ...

Xcode open two editor windows with same file

Is it possible to open the same file in two separate windows in Xcode. I can open a file in one window and the same file in the main Xcode editor window, but I wanted two separate fulltime editor ...

Forcing code signing refresh in Xcode

In our environment, we share resources across multiple projects and platforms. When building for iPhone, only a subset of those resources are needed. Since that subset is still considerable, we have ...

热门标签