如何创建32个随机UILabels旋转?

时间:2012-02-07 11:59:02

标签: objective-c ios5 xcode4.2

我正在使用UILabels创建32个名字的轮换。我如何随机旋转所有32个名字?

2 个答案:

答案 0 :(得分:2)

- (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和按钮操作。

答案 1 :(得分:1)

#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;
}