我需要将我的观点的所有颜色添加到NSMuttableArray。尝试这样做,但只添加一种颜色的最后一个视图。我的数组名为colorArray。请帮帮我。
我认为它应该在一个循环中完成,但我不确切地知道它的原因在哪里以及如何描述它
这是我的ViewController
#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
我的View Class
//
// 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
答案 0 :(得分:0)
每次调用createView时,都会创建一个新数组。尝试将colorArray = [[NSMutableArray alloc] init];
行移动到viewDidLoad方法中。