0
votes

I am trying to make an app that shows different color rectangles based on the colors that have been chosen from UITableView.

I have no problem getting which UITableView row was selected and inside my NSLog I can see that the right color name is being sent when I switch to different view.

But I can't figure out how to call the "(void)drawRect:(CGRect)rect" method to start drawing the rectangles.

For example, if I have selected "Black" color from the UITableView, when I hit accept button, it will go to a new view, where a "Black" rectangle will appear (because the user have chosen the color "Black")

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString: @"switch"]) {

    //what is inside the chosenColors array?
    NSLog(@"%@ clicked", _chosenColors);

    //if the chosen color is black then do something
    if ([_chosenColors isEqual:@"Black"]) {
        NSLog(@"BLAH");
        //CGContextRef context = UIGraphicsGetCurrentContext();
        //[[UIColor blackColor] set];
        //RectangleView *gotColors = [[RectangleView alloc]initWithFrame:CGContextFillRect(context, CGRectMake(220, 20, 40, 40))];
        RectangleView *gotColors = [[RectangleView alloc]init];
        //RectangleView *gotColors = [[RectangleView alloc] initWithFrame:CGRectMake(220, 40, 40, 40)];
        //[self.navigationController pushViewController:gotColors animated:YES];

       // gotColors.CGContextFillRect(context, CGRectMake(220, 20, 40, 40));
        //changeClothes *new = [[changeClothes alloc]initWithNibName:@"changeClothes" bundle:nil];

    }

}

}

as you can see I tried doing bunch of things and none of them are working :( this code is inside my UITableViewController and when I hit "accept" button, it sends the data (the chosen color names) to UIViewController.

I am making dynamic cells from NSMutableArray to fill the colors table and if the color is chosen, it gets stored inside another NSMutableArray called chosenColors

and here is my method to draw rectangles

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
   // Drawing code
CGContextRef context = UIGraphicsGetCurrentContext();
[[UIColor blueColor] set];
//CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 0.5); //CGContextRef, Red, Green, Blue, Alpha
rect = CGRectMake(220, 20, 40, 40); //x, y, width, height
CGContextFillRect(context, rect); //CGContextRef, CGRect

}

this code is inside UIView called RectangleView

I am fairly new to iPhone developing and any kind of help would be appreciated.

1

1 Answers

0
votes

You have to

  • subclass UIView
  • override its drawRect to do what you want
  • set the view property of your presented view controller to the new class

The view controller can now tell the view which color to use.