【问题标题】:How to create 32 random UILabels rotation?如何创建 32 个随机 UILabel 旋转?
【发布时间】:2012-02-07 11:59:01
【问题描述】:

我正在使用 UILabel 创建 32 个名称的轮换。我将如何随机轮换所有 32 个名称?

【问题讨论】:

  • 轮换是什么意思?移动 UILabel 的动画还是只是从 32 个名称中随机选择一个?
  • 我有 32 个名字。我想按一个按钮,让名称标签随机更改名称。

标签: objective-c ios5 xcode4.2


【解决方案1】:
- (IBAction)buttonPressed
{
     int randomInt = rand() % [nameArray count]; 
     [nameLabel setText:[nameArray objectAtIndex:randomInt]]
}

在您的 .h 文件中,您必须有:

IBOutlet UILabel *nameLabel;

编辑

我构建了这个项目,这里是我使用的确切代码: 这是 .h 文件:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    IBOutlet UILabel *nameLabel;
    NSArray *nameArray;
}
- (IBAction)buttonPressed;

@end

这是 .m 文件:

#import "ViewController.h"

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    nameArray = [[NSArray alloc] initWithObjects:@"name1", @"name2", @"name3", @"name4", @"name5", @"name6", nil];
}

- (IBAction)buttonPressed
{
    int randomInt = rand() % [nameArray count]; 
    [nameLabel setText:[nameArray objectAtIndex:randomInt]];
}

- (void)dealloc
{
    [super dealloc];
    [nameArray release];
    nameArray = nil;
}
@end

确保 UILabel 和按钮操作都在界面生成器中连接。

【讨论】:

  • 我将如何实现这个?我将所有的 UILabel 都连接为网点。
  • 我已经弄清楚了那部分。我收到错误消息“使用未声明的标识符'nameArray'。
  • 是的,您需要将 nameArray 替换为存储名称列表的数组的名称。您还需要确保在您的头文件中声明该数组,以便按下该按钮可以访问它。因此,如果没有,请在您的 .h 文件中添加。 NSArray *nameArray;或 NSMutableArray *nameArray;
  • 如何存储我的姓名列表?
  • 要创建一个名称数组,只需使用这个: nameArray = [[NSArray alloc] initWithObjects:@"Jason", @"Bob", @"Tom", nil];按照这种格式,你就有了你的名字数组
【解决方案2】:
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    NSMutableArray *nameArray;
    NSMutableArray *textFieldArray;
    UIScrollView *scrollView;
}
- (IBAction)buttonPressed;
- (void)addTextFields:(int)count;

// Random sort function for the shuffle method
int randomSort(id obj1, id obj2, void *context );
@end





#import "ViewController.h"

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // If you want to pre load values 
    nameArray = [[NSMutableArray alloc] initWithObjects:@"name1", @"name2", @"name3", @"name4", @"name5", @"name6", nil];

    // Initilize the array to contain all the textfields
    textFieldArray = [[NSMutableArray alloc] init];

    // inititlize and add the scrollview
    scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
    [self.view addSubview:scrollView];

    // Create and add the button to randomize the fields
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button setFrame:CGRectMake(self.view.frame.size.width/2 - 150, 20, 150, 50)];
    [button addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"Randomize" forState:UIControlStateNormal];
    [scrollView addSubview:button];

    // method to create any number of textfields (currently sending number of items in nameArray)
    [self addTextFields:[nameArray count]];

}

- (void)addTextFields:(int)count
{
    // adjust these to get the size and positions you like
#define X_POSITION 20
#define TEXT_FIELD_WIDTH 300
#define TEXT_FIELD_HEIGHT 50

    // Where to place the first text field
    int yPosition = 90;
    for (int textFieldCount = 0; textFieldCount<count; textFieldCount++) {

        //Create and add the text field
        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(X_POSITION, yPosition, TEXT_FIELD_WIDTH, TEXT_FIELD_HEIGHT)];
        [textField setTag:textFieldCount];
        [scrollView addSubview:textField];
        [textFieldArray addObject:textField];
        [textField setText:[nameArray objectAtIndex:textFieldCount]];

        // Where to place the next text field
        yPosition += (TEXT_FIELD_HEIGHT + 20);
    }

    // set the scroll view content size so it will fit all the text fields
    [scrollView setContentSize:CGSizeMake(self.view.frame.size.width, yPosition+TEXT_FIELD_HEIGHT+20)];
}

- (IBAction)buttonPressed
{
    // release and remove everyting from the name array
    [nameArray release];
    nameArray = nil;

    // reinitilize the name array
    nameArray = [[NSMutableArray alloc] init];

    // Loop through the textfields to get the names into the nameArray
    for (int textFieldCount = 0; textFieldCount<[textFieldArray count]; textFieldCount++) {
        [nameArray addObject:[[textFieldArray objectAtIndex:textFieldCount] text]];
    }

    // Randomly sort the names in the array
    [nameArray sortUsingFunction:randomSort context:nil];

    // Add the random names back into the text fields
    for (int textFieldCount = 0; textFieldCount<[textFieldArray count]; textFieldCount++) {
        [[textFieldArray objectAtIndex:textFieldCount] setText:[nameArray objectAtIndex:textFieldCount]];
    }
}

int randomSort(id obj1, id obj2, void *context ) {
    // returns random number -1 0 1
    return (arc4random()%3 - 1);
}

- (void)dealloc
{
    [super dealloc];
    [nameArray release];
    nameArray = nil;
}

【讨论】:

  • 因为所有的文本字段都不适合屏幕。我需要一种滚动浏览它们的方法。此外,此代码不检查空文本字段。如果您要删除其中一个文本字段中的所有文本并点击随机化按钮,它将崩溃。
  • 这很好用!我所做的是我有 32 个可以点击和移动的文本字段。所以我的问题是如何将其修改为现有的 TextFields?如果可能的话,我更愿意在工具栏中使用 IBAction。
  • 要修改它以使用您现有的文本字段,您将不使用 addTextFields: 方法,您只需将您的文本字段添加到 textFieldArray 中。至于工具栏中的 IBAction。您只需连接到 (IBAction)buttonPressed 功能。删除我创建按钮的代码。
  • 好的。我已经找到了按钮并拿走了滚动视图。我认为滚动视图清洗不需要能够点击和拖动文本字段。
  • 如何在 .m 中添加 UITextField ?
猜你喜欢
  • 1970-01-01
  • 2021-12-01
  • 1970-01-01
  • 2016-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-21
  • 1970-01-01
相关资源
最近更新 更多