Ios 如何将视图的颜色添加到NSMuttableArray

Ios 如何将视图的颜色添加到NSMuttableArray,ios,iphone,objective-c,cocoa-touch,Ios,Iphone,Objective C,Cocoa Touch,我需要将视图的所有颜色添加到NSMuttableArray。尝试这样做,但只添加最后一个视图的一种颜色。我的数组名为colorArray。请帮帮我 我认为这应该在一个循环中完成,但我不知道它的确切原因以及如何描述它 这是我的视图控制器 #import "ViewController.h" #import "RandomView.h" @interface ViewController () @end @implementation ViewController @synthesize ko

我需要将视图的所有颜色添加到NSMuttableArray。尝试这样做,但只添加最后一个视图的一种颜色。我的数组名为colorArray。请帮帮我

我认为这应该在一个循环中完成,但我不知道它的确切原因以及如何描述它

这是我的视图控制器

#import "ViewController.h"
#import "RandomView.h"
@interface ViewController ()

@end

@implementation ViewController

@synthesize kol;
@synthesize sdvig1;
@synthesize sdvig2;
@synthesize myTextField;
@synthesize randView;
@synthesize redButton, greenButton, blueButton;
@synthesize colorArray;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
       self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        kol=10;
    }
    return self;
}


-(void) viewWillAppear:(BOOL)animated
{
    myTextField.delegate = self;
    kol=[[myTextField text] intValue];
}


- (void)viewDidLoad
{
    [super viewDidLoad];

    myTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width/4, 30)];
    myTextField.backgroundColor=[UIColor yellowColor];
    [myTextField setKeyboardType:UIKeyboardTypeNumberPad];
    [self.view addSubview:myTextField];


    redButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width/4, 20, self.view.frame.size.width/4, 30)];
    redButton.backgroundColor=[UIColor redColor];
    [redButton addTarget: self
              action: @selector(redSort:)    forControlEvents: UIControlEventTouchUpInside];


    greenButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width/4*2, 20, self.view.frame.size.width/4, 30)];
    greenButton.backgroundColor=[UIColor greenColor];
    [greenButton addTarget: self
                  action: @selector(greenSort:)    forControlEvents: UIControlEventTouchUpInside];


    blueButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width/4*3, 20, self.view.frame.size.width/4, 30)];
    blueButton.backgroundColor=[UIColor blueColor];
    [blueButton addTarget: self
                  action: @selector(blueSort:)    forControlEvents: UIControlEventTouchUpInside];

    [self.view addSubview:redButton];
    [self.view addSubview:greenButton];
    [self.view addSubview:blueButton];

    [self createView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}



#pragma mark- Создание и удаление View's

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [myTextField resignFirstResponder];
    [self textFieldShouldReturn:myTextField];
    myTextField.text = @"";
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if ([self.myTextField.text isEqualToString:@""]) {

    } else
    {
        [self.myTextField resignFirstResponder];
        kol=[[self.myTextField text] intValue];

        [self removeView];
        [self createView];
    }
    return YES;
}

-(void)createView{

    colorArray= [[NSMuttableArray alloc] init];

    sdvig1=self.view.frame.size.width/(2*(kol+5));
    sdvig2=self.view.frame.size.height/(2*(kol+5));
    randView = [[RandomView alloc] initWithFrame:CGRectMake(0,self.myTextField.frame.origin.y+self.myTextField.frame.size.height, self.view.frame.size.width, self.view.frame.size.height-(self.myTextField.frame.origin.y+self.myTextField.frame.size.height)) count:kol sdvig:CGPointMake(sdvig1, sdvig2) vc:self];

    [colorArray addObject:randView.backgroundColor]; //IT'S MY ARRAY

    NSLog(@"colorArray= %@", colorArray);
    [self.view addSubview:randView];

}


-(void) removeView{
    [randView removeFromSuperview];
}




#pragma mark- Цвета

-(UIColor *) randomColor
{
    CGFloat red =  (CGFloat)arc4random() / (CGFloat)RAND_MAX;
    CGFloat blue = (CGFloat)arc4random() / (CGFloat)RAND_MAX;
    CGFloat green = (CGFloat)arc4random() / (CGFloat)RAND_MAX;
    return [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
}


@end
还有我的观点课

//
//  RandomView.m
//  FewView
//
//  Created by admin on 3/11/14.
//  Copyright (c) 2014 admin. All rights reserved.
//

#import "RandomView.h"
#import "ViewController.h"
//#import <QuartzCore/QuartzCore.h>

@implementation RandomView
@synthesize randView;
//@synthesize myArray;
//@synthesize colorArrayRed, colorArrayBlue, colorArrayGreen, colorArray;
//@synthesize colorArray;
//CGFloat redBack, greenBack, blueBack, alphaBack;

- (id)initWithFrame:(CGRect)frame count:(NSInteger)kol sdvig:(CGPoint)sdvig vc:(ViewController*) delegat
{
    self = [super initWithFrame:frame];
    if (self)
    {
//        myArray = [[NSMutableArray alloc]init];
        if (kol>0) {
            [self setBackgroundColor:[delegat randomColor]];
                        randView = [[RandomView alloc] initWithFrame:CGRectMake(sdvig.x, sdvig.y, self. frame.size.width-2*sdvig.x, self.frame.size.height-2*sdvig.y) count:--kol sdvig:CGPointMake(sdvig.x, sdvig.y) vc:delegat];
                        self.layer.cornerRadius = 25;
                        self.layer.masksToBounds = YES;
                        [self addSubview:randView];

         }
    }
    return self;
}

@end
//
//RandomView.m
//费维尤
//
//由管理员于2014年3月11日创建。
//版权所有(c)2014管理。版权所有。
//
#导入“RandomView.h”
#导入“ViewController.h”
//#进口
@实现随机视图
@综合randView;
//@合成myArray;
//@合成colorArrayRed、colorArrayBlue、colorArrayGreen、colorArray;
//@合成彩色阵列;
//CGFloat红背、绿背、蓝背、字母;
-(id)initWithFrame:(CGRect)帧计数:(NSInteger)kol sdvig:(CGPoint)sdvig vc:(ViewController*)delegat
{
self=[super initWithFrame:frame];
如果(自我)
{
//myArray=[[NSMutableArray alloc]init];
如果(kol>0){
[self-setBackgroundColor:[delegat randomColor]];
randView=[[RandomView alloc]initWithFrame:CGRectMake(sdvig.x,sdvig.y,self.frame.size.width-2*sdvig.x,self.frame.size.height-2*sdvig.y)计数:--kol-sdvig:CGPointMake(sdvig.x,sdvig.y)vc:delegat];
self.layer.cornerRadius=25;
self.layer.masksToBounds=是;
[自添加子视图:randView];
}
}
回归自我;
}
@结束

每次调用createView时,您都在创建一个新数组。尝试移动
colorArray=[[NSMutableArray alloc]init]行到您的viewDidLoad方法。

在哪里创建了
colorArray
?对不起,这是旧版本。colorArray是在-(void)createView{colorArray=[[NSMutableArray alloc]init]中创建的;我想在一个数组中保存15个视图的15种颜色,然后我将对这些颜色进行排序。但我不知道它是如何做到的。