《IOS编程》控制逻辑课后练习 学习手记

来源:互联网 发布:什么是协同过滤算法? 编辑:程序博客网 时间:2024/04/30 08:50

题目:

将UISegmentedControl 对象(分段控件)加入BNRHypnosisViewController 对象的视图……书页码(139)

开始编码:

1.修改

BNRHypnosisterView.m 将之前定义的局部属性 改为全局属性

@property ( strong , nonatomic)UIColor *circleColor; //圆圈颜色
定义到.h 文件中即可。

2.BNRHypnosisViewController.m 中增加局部属性

@property (nonatomic,strong) BNRHypnosisterView *backgroundView;

修改

viewDidLoad :

- (void)viewDidLoad {    [super viewDidLoad];        //CGRect frame = [UIScreen mainScreen].bounds;    CGRect frame = CGRectMake(0.0, 35, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-150);    NSLog(@"===>1");    // Do any additional setup after loading the view.    self.backgroundView  = [[BNRHypnosisterView alloc]initWithFrame:frame];        NSArray *segmentedArray = [[NSArray alloc]initWithObjects:@"随机颜色",@"红色",@"蓝色",@"黄色",nil];       UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:segmentedArray];        segmentedControl.selectedSegmentIndex = 0;//设置默认选择项索引       segmentedControl.frame = CGRectMake(1.0, [UIScreen mainScreen].bounds.size.height-50-60, 318.0, 50.0);    segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;//设置样式    [segmentedControl addTarget:self action:@selector(segmentAction:)forControlEvents:UIControlEventValueChanged];  //添加委托方法        [self.view addSubview:self.backgroundView];    [self.view addSubview:segmentedControl];   //self.view = backgroundView;}

新增委托方法segmentAction:

//作业中级练习- (void)segmentAction:(UISegmentedControl *)Seg{    NSInteger Index = Seg.selectedSegmentIndex;        //NSLog(@"Index %ld", Index);    switch (Index) {        case 0:{            float red =  (arc4random()%100)/100.0;            float blue =  (arc4random()%100)/100.0;            float green =  (arc4random()%100)/100.0;                        UIColor *randomColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];            self.backgroundView.circleColor = randomColor;                    }        break;        case 1:{            UIColor *randomColor = [UIColor redColor];            self.backgroundView.circleColor = randomColor;        }            break;        case 2:{            UIColor *randomColor = [UIColor blueColor];            self.backgroundView.circleColor = randomColor;        }            break;        default:{                    UIColor *randomColor = [UIColor yellowColor];            self.backgroundView.circleColor = randomColor;                           }            break;    }    }

搞定。。。

新手学习中,仅供参考

0 0
原创粉丝点击