0

我是 iOS 应用程序开发的新手。目前我正在开发一个非常简单的应用程序,它在屏幕上显示两个单词,用户选择一个,应用程序只输出你选择的 x。

我创建了两个 plist,并将它们加载到一个数组中,并将它们随机化到数组中。在我看来,我有两个按钮,我希望文字显示在它们上面。但是,我有两个问题.. 1。我希望按钮上的标签在应用程序启动时发生变化。所以用户不必点击按钮或任何东西。2. 我希望 array_1 中的一个随机单词(从 plist 加载)出现在随机按钮上。button_1 或 button_2 和列表 array_2 相同。

这是我到目前为止所做的。(在论坛的一些帮助下(:)

- (IBAction)buttonpressed:(id)sender {

    // Load contents of plist onto array

    NSString *path = [[NSBundle mainBundle] pathForResource:@"wordsOne" ofType:@"plist"];
    NSMutableArray *words = [NSMutableArray arrayWithContentsOfFile:path];

    //shuffle them using Fisher–Yates shuffle algorithm

    for (int i = words.count-1; i>=0; i--) {
        int r = arc4random_uniform(words.count);
        [words exchangeObjectAtIndex:i withObjectAtIndex:r];
    }

    // assign word to the button
    for (int j =0; j<_WordButton.count; j++) {
        UIButton *button = [_WordButton objectAtIndex:j];
        button.titleLabel.text = [words objectAtIndex:j];
    }

}

上面的代码有明显的缺陷,比如它只使用了一个 plist 并且没有在两个按钮上随机显示单词。

4

3 回答 3

4

您可以使用以下代码设置 UIButton 标题:

[button setTitle:titleString forState:UIControlStateNormal];
于 2013-04-20T09:45:32.903 回答
2

要实现您想要做的事情,您必须为 .xib 文件上的按钮创建一个 IBOutlet。您可以通过使用简单的 Xcode 快捷方式来完成此操作,该快捷方式将创建 IBOutlet 代码并同时为您建立从 .xib 文件到代码的连接:

  1. 在 Xcode 中打开 .xib 文件,该文件将使用 Interface Builder 显示一个图形文件。
  2. In the toolbar at the top-right of Xcode is an icon that allows you to toggle through different modes, select the 'Assistant Editor'. The Interface Builder will slide over to share the window with a code file, this should automatically select your ViewController.h
    The toolbar should look like this:
    enter image description here

  3. Insert some curly-brace in your code like so:

`

@interface CustomViewController : UIViewController {
// This is where you will control-drag to from the Interface Builder to have it create an IBOutlet to the button for you
}

// Your already set-up IBAction Method
- (IBAction)buttonpressed:(id)sender
@end

`

  1. Control-drag from the button you wish your label to change to the area between the curly-braces in the .h file:
    enter image description here

  2. A new dialog box will appear. Make sure that the settings are as follow:
    a. Connection = IBOutlet
    b. Name = (Whatever you want to call the button, you've used '_wordButton')
    c. Type = UIButton
    d. Storage = WEAK

enter image description here

  1. Now in your action method - (IBAction)buttonpressed:(id)sender and in your application:didFinishLaunchingWithOptions (to automatically load a word into the label when the application is launched) you can set the button title with the following:

`

// Create a string from the word that you pull out of the plist file
NSString *labelWord = ???;

// Set the label of the button
[_wordLabel setTitle:word forState:UIControlStateNormal];

`

于 2013-04-20T09:58:28.303 回答
0

像这样做:

-(void)viewDidLoad{
[self setnewtitle];

}

-(void)setnewtitle{
NSString *pathone = [[NSBundle mainBundle] pathForResource:@"wordsOne" ofType:@"plist"];
    NSMutableArray *wordsOne = [NSMutableArray arrayWithContentsOfFile:pathone];
NSString *pathtwo = [[NSBundle mainBundle] pathForResource:@"wordsOne" ofType:@"plist"];
    NSMutableArray *wordsTwo = [NSMutableArray arrayWithContentsOfFile:pathtwo];
int words = "the number of objects(words) in your plist";
int randombutton = arc4random() % 2;
int randomplist = arc4random() % 2;
int randomWord = arc4random() % words;

if (randombutton==0){
if (randomplist==0){
[[_WordButton objectatIndex:randombutton] setTitle:[wordsOne objectatIndex:randomWord]];
} else {
[[_WordButton objectatIndex:randombutton] setTitle:[wordsTwo objectatIndex:randomWord]];

}
}else {
if (randomplist==0){
[[_WordButton objectatIndex:randombutton] setTitle:[wordsOne objectatIndex:randomWord]];
} else {
[[_WordButton objectatIndex:randombutton] setTitle:[wordsTwo objectatIndex:randomWord]];

}
}



}

请确保数组中包含的按钮已初始化和分配,如果您有任何问题,请随时提问

于 2013-04-20T09:53:05.840 回答