Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7779293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:36:49+00:00 2026-06-01T18:36:49+00:00

in my ViewController I have a button: – (IBAction)drawLineClick:(id)sender { CGRect rect; rect.origin.x =

  • 0

in my ViewController I have a button:

- (IBAction)drawLineClick:(id)sender 
{
    CGRect rect;
    rect.origin.x = 20.0f;
    rect.origin.y = 40.0f;
    rect.size.width = 100.0f;
    rect.size.height = 100.0f;

    //draw line
    DrawLine *drawLine = [[DrawLine alloc] initWithFrame:rect]; 
    [self.view addSubview:drawLine];
}

in my DrawLine class I just draw a line:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [super setBackgroundColor:[UIColor clearColor]];
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    // Drawing code

    [self drawLine];
}

- (void)drawLine
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
    CGContextSetLineWidth(context, 3.0);
    CGContextMoveToPoint(context, 0, 0); 
    CGContextAddLineToPoint(context, 50, 50); 
    CGContextStrokePath(context);
}

This works great but this is not variable. Every time it’s the same line.
How can I pass the line color, line width, etc. from the ViewController to the DrawLine class, so that I can draw different lines?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T18:36:50+00:00Added an answer on June 1, 2026 at 6:36 pm

    Create properties in your DrawLine class that represent the things you want to control. When you create the new object, set its properties either by assigning them directly or passing them in a custom initWith... method. Use the property values in drawRect:.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a button on the currently navigated to viewcontroller, connected to an IBAction.
I have this ViewController with on it a UIButton. This button fires a method
I have 2 viewcontroller and in that one controller having a button, now i
I have a navigation controller that displays viewcontroller A. There is a right button
I have my iPad ViewController.xib with a button called about us. How to link
I have a button that pushes another viewcontroller onto my navigationcontroller. When I press
In my ViewController I have created a view which contains a button and a
I have a viewController called FirstViewController. In an IBAction i call another ViewController called
I have this code linked to a button on a ViewController with a UITableView
is it possible to have a back button in a modal viewController?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.