Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何创建32个随机旋转标签?_Objective C_Ios5_Xcode4.2 - Fatal编程技术网

Objective c 如何创建32个随机旋转标签?

Objective c 如何创建32个随机旋转标签?,objective-c,ios5,xcode4.2,Objective C,Ios5,Xcode4.2,我正在使用UILabels创建32个名称的循环。如何随机旋转所有32个名称 - (IBAction)buttonPressed { int randomInt = rand() % [nameArray count]; [nameLabel setText:[nameArray objectAtIndex:randomInt]] } 在.h文件中,您必须具有: IBOutlet UILabel *nameLabel; 编辑 我创建了这个项目,下面是我使用的确切代码: 这

我正在使用UILabels创建32个名称的循环。如何随机旋转所有32个名称

- (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
确保在interface builder中同时连接了UILabel和按钮操作

#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;
}
在.h文件中,您必须具有:

IBOutlet UILabel *nameLabel;
编辑

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

#import <UIKit/UIKit.h>

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

@end
确保在interface builder中同时连接了UILabel和按钮操作。

\import
#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;
}
@界面ViewController:UIViewController { NSMutableArray*nameArray; NSMutableArray*textFieldArray; UIScrollView*滚动视图; } -(IBAction)按钮按下; -(void)addTextFields:(int)计数; //洗牌法的随机排序函数 int随机排序(id obj1、id obj2、void*上下文); @结束 #导入“ViewController.h” @实现视图控制器 -(无效)viewDidLoad { [超级视图下载]; //如果要预加载值 nameArray=[[NSMutableArray alloc]initWithObjects:@“name1”、“name2”、“name3”、“name4”、“name5”、“name6”、nil]; //初始化数组以包含所有文本字段 textFieldArray=[[NSMutableArray alloc]init]; //初始化并添加滚动视图 scrollView=[[UIScrollView alloc]initWithFrame:self.view.frame]; [self.view addSubview:scrollView]; //创建并添加按钮以随机化字段 UIButton*button=[UIButton button类型:UIButtonyPeroundRect]; [按钮设置框:CGRectMake(self.view.frame.size.width/2-150,20,150,50)]; [按钮添加目标:自我操作:@选择器(按钮按下)用于控制事件:UIControlEventTouchUpInside]; [按钮设置标题:@“随机化”状态:uicontrol状态正常]; [滚动视图添加子视图:按钮]; //方法创建任意数量的文本字段(当前正在发送nameArray中的项数) [self-addTextFields:[nameArray count]]; } -(void)addTextFields:(int)计数 { //调整这些以获得您喜欢的大小和位置 #定义X_位置20 #定义文本\字段\宽度300 #定义文本\字段\高度50 //放置第一个文本字段的位置 int-yPosition=90; 对于(int textFieldCount=0;textFieldCount
#导入
@界面ViewController:UIViewController
{
NSMutableArray*nameArray;
NSMutableArray*textFieldArray;
UIScrollView*滚动视图;
}
-(IBAction)按钮按下;
-(void)addTextFields:(int)计数;
//洗牌法的随机排序函数
int随机排序(id obj1、id obj2、void*上下文);
@结束
#导入“ViewController.h”
@实现视图控制器
-(无效)viewDidLoad
{
[超级视图下载];
//如果要预加载值
nameArray=[[NSMutableArray alloc]initWithObjects:@“name1”、“name2”、“name3”、“name4”、“name5”、“name6”、nil];
//初始化数组以包含所有文本字段
textFieldArray=[[NSMutableArray alloc]init];
//初始化并添加滚动视图
scrollView=[[UIScrollView alloc]initWithFrame:self.view.frame];
[self.view addSubview:scrollView];
//创建并添加按钮以随机化字段
UIButton*button=[UIButton button类型:UIButtonyPeroundRect];
[按钮设置框:CGRectMake(self.view.frame.size.width/2-150,20,150,50)];
[按钮添加目标:自我操作:@选择器(按钮按下)用于控制事件:UIControlEventTouchUpInside];
[按钮设置标题:@“随机化”状态:uicontrol状态正常];
[滚动视图添加子视图:按钮];
//方法创建任意数量的文本字段(当前正在发送nameArray中的项数)
[self-addTextFields:[nameArray count]];
}
-(void)addTextFields:(int)计数
{
//调整这些以获得您喜欢的大小和位置
#定义X_位置20
#定义文本\字段\宽度300
#定义文本\字段\高度50
//放置第一个文本字段的位置
int-yPosition=90;

对于(int textFieldCount=0;textFieldCount旋转是什么意思?移动UILabels的动画还是只随机选取32个名称中的一个?我有32个名称。我想按下按钮,让名称标签随机更改名称。旋转是什么意思?移动UILabels的动画还是只随机选取32个名称中的一个?我有32个名称。我想按一个按钮,让名称标签随机更改名称。我该如何实现这一点?我将所有UILabels连接为插座。我已经找到了该部分。我收到了错误消息"使用未声明的标识符“nameArray”。是的,您需要将nameArray替换为存储名称列表的数组的名称。您还需要确保在头文件中声明了该数组,按下该按钮即可访问该数组。因此,如果没有,请在.h文件中添加。NSArray*nameArray;或NSMutableArray*nameArray;我如何存储我的姓名列表?要创建姓名数组,只需使用以下命令:nameArray=[[NSArray alloc]initWithObjects:@“Jason”@“Bob”@“Tom”,nil];遵循该格式,您就有了您的姓名数组。我将如何实现此操作?我将所有UILabel连接为Outlet。我已经解决了该部分。我收到了错误消息"使用未声明的标识符“nameArray”。是的,您需要将nameArray替换为存储名称列表的数组的名称。您还需要确保在头文件中声明了该数组,按下该按钮即可访问该数组。因此,如果没有,请在.h文件中添加。NSArray*nameArray;或NSMutableArray*nameArray;如何存储姓名列表?要创建姓名数组,只需使用以下命令:nameArray=[[NSArray alloc]initWithObjects:@“Jason”、“Bob”、“Tom”、“nil];按照这种格式,您就有了名称数组,因为所有文本字段都无法显示在屏幕上。我需要一种方法来滚动它们。此外,此代码不会检查空文本字段。如果要删除所有文本,我将