How to call [UIView drawRect:(CGRect)rect] many times ?

Hi guys.
I made implementation for UIView in order to draw shapes on UIView.
Now I need to call again draw:(CGRect) of my custom UIView.
But I don't know to call it again.
When call again draw:(CGRect), it would be cleared previous drawn shapes.
How can I do it ?
This is my code sample :
//MyView.h
#import <UIKit/UIKit.h>
@interface MyView : UIView {
NSMutableArray *lines;
@property (nonatomic, retain) NSMutableArray *lines;
//MyView.m
#import "MyView.h"
#import "MyPoint.h"
@implementation MyView
@synthesize lines;
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
lines = [[NSMutableArray alloc] init];
return self;
- (void)drawRect:(CGRect)rect {
CGFloat red[4] = {1.0f, 0.0f, 0.0f, 1.0f};
CGContextSetStrokeColor(c, red);
CGContextBeginPath(c);
int count = [lines count];
for(int i = 0; i < count - 1; i ++) {
MyPoint *point1 = (MyPoint*)[lines objectAtIndex:i];
MyPoint *point2 = (MyPoint*)[lines objectAtIndex:i + 1];
CGContextMoveToPoint(c, point1.x, point1.y);
CGContextAddLineToPoint(c, point2.x, point2.y);
CGContextStrokePath(c);
I tried to call draw method in NSThread ([self performSelectorOnMainThread:@selector(startDraw) withObject:nil waitUntilDone:NO];) following as :
- (void)startDraw {
MyView *view = (MyView)self.view;
[view drawRect:self.view.frame];
In the console :
Sun Nov 29 10:19:36 apples-imac.local MyApp[11909] <Error>: CGContextSetStrokeColor: invalid context
Sun Nov 29 10:19:36 apples-imac.local MyApp[11909] <Error>: CGContextBeginPath: invalid context
Sun Nov 29 10:19:36 apples-imac.local MyApp[11909] <Error>: CGContextMoveToPoint: invalid context
Sun Nov 29 10:19:36 apples-imac.local MyApp[11909] <Error>: CGContextAddLineToPoint: invalid context
Sun Nov 29 10:19:36 apples-imac.local MyApp[11909] <Error>: CGContextDrawPath: invalid context
What is wrong here ?
Thanks in advance.

Never call drawRect from your code. When you want the entire view to be redrawn, use setNeedsDisplay. E.g.:
// myViewController.m
- (void)updateView:(NSData*)newData {
// compute some new coords based on newData
self.view.newPoint = [self computePoint:newData];
// request redraw
[self.view setNeedsDisplay];
As shown in the example, you normally only need a redraw when something that affects the drawing has changed. You don't need to ask for a redraw when the view is first loaded, since the first drawing is automatic.
Also I'm concerned about your mention of a new thread. Additional threads should be avoided if at all possible, especially if you have less than two years of experience with Cocoa. Although xnav pointed you to some good documentation in reply to your last question, you should do everything you can to get the desired results without NSThread. I promise that extra thread will be nothing but trouble for you. If you explain what you're trying to accomplish, maybe someone here can suggest an implementation that only uses the main thread, ok?
- Ray

Similar Messages

  • How to call the Application configuration in run time.

    hi guru,
    i have created two  Application configuraton   and it working fine.
    my question how to call the application configuration in run time.
    Example :
    Based on the user i want to change the Application Configuration.like Super user and Normal user.
    Advance thank's
    Regard's
    Vivekanathan.S

    Hi,
    Please try out this way-
    Have a look at this table -
    WDY_CONF_APPLU - (Customizing Data for Web Dynpro Applications).
    after you are done with the configuration--
    Ge the config id for the given application from this table. And depending on the user get an entry from this table and display accordingly.
    Please refer to these links -
    different default layouts for different CONFIG_IDs
    Get WDCONFIGURATIONID
    Regards
    Lekha

  • I have itune on my first computer, but i cant download it for my other one,how can i do that, i tryed many time with the link it says you download it but nothing follows  thank you

    I have itune on my first computer, but i cant download it for my other one, how can i do that, i tryed many time with the link it says you download it but nothing follows  thank you

    I'd first try downloading an installer from the Apple website using a different web browser:
    http://www.apple.com/itunes/download/
    If you use Firefox instead of IE for the download (or vice versa), do you get a working installer?

  • How to call two data blocks at a time.

    hi all
    i want to call two data blocks at a time on function when button pressed.
    how can I it?
    many thanks

    Many thanks that you people reply,
    I am working in oracle form developer 6i and database 10g.
    I have a form that have three data blocks,
    first block have text boxes and search button,and next two blocks display records on the basis of search criteria that is inserted in the first block and when is clicked on save button.
    the data that is to b displayed is from two different tables.
    i used Go_block('my_block')
    and call other block with the same method.
    kindly guide me how can i do it.
    Thanks in advance

  • How to Call Center Opens at a diffrent times 1st and 3rd Tue. of the Month

    Hello all,
    I'm new to UCCX and doing my best to understand scripting. I've created a script that's is normally open 7am to 12pm closed from 12pm to 1pm for lunch then open from 1pm to 5pm then closed again until 7am.
    Now every 1st and 3rd Thursday of the month they open at 9am instead of 7am.
    How would like script something like this? Is this possible?
    Thanks in advance for the help.
    We are running UCCX version 8.5 and using the IPPA.

    Here's a solution that works with Enhanced.
    The solution is simple.  If today's Day of Week (DoW) is equal to the target DoW, then we jump backwards in time by one week, and if we're still in the same month, we repeat that process over and over again, until we have stepped back so far, we step into the previous month.  All along the way, keeping track of how many times we were able to jump back in the same month.  This becomes our instance counter and what we check to see if we are on the correct instance of the target DoW.
    Subflow Script
    Variables
    boolean is_matched = false
    Date today = d[now]
    Date date_crawler = d[now]
    int target_dow = 0
    int target_instance = 0
    int instance_counter = 0
    Script: system-date-instances.aef
    StartLABEL0:If (date_crawler.dow == target_dow) Then  True    Increment instance_counter    Do date_crawler.setDate(date_crawler.date - 7)    If (date_crawler.month == today.month) Then      True        Goto LABEL0      False  FalseSet is_matched = instance_counter == target_instanceEnd
    Caller Script
    Variables
    boolean is_third_thursday = false
    Script - main-aa.aef
    Start...the intro to your script.../* You will need to pass in two integers as input parameters: target_dow and target_instance *//* taget_dow can be 1 - 7 (Sunday - Saturday) *//* target_instance would be a number representing which DoW you are interested in *//* E.g., If I wanted to know if today is the third thursday in the month I would pass in: *//* target_dow = 5 and target_instance = 3 *//* You will need to get back one boolean as an output parameter: is_matched */is_third_thursday = Call Subflow(SCRIPT[system-date-instances.aef], is_matched)If (is_third_thursday)  True    ...use modified business hours...  False    ...use regular business hours......the rest of your script...End
    Anthony Holloway
    Please use the star ratings to help drive great content to the top of searches.
    Tags: first, second, third, fourth, sunday, monday, tuesday, wednesday, thursday, friday, saturday
    Message was edited by: Anthony Holloway - Adding tags

  • HT6147 How can I update my iPad so many time Im trying but I can't

    How can I update my iPad!so many times I'm trying to update but I can't please help me

    Connect to iTunes on the computer you usually Sync with and “ Check for Updates “...
    If an Update Appears Install it... if not... you are up to date for your particular Device...
    See the Using iTunes Section Here...
    How to update your iPhone, iPad, or iPod touch
    Make sure you have the Latest Version of iTunes (v11.1.5) Installed on your computer
    iTunes free download from www.itunes.com/download

  • How to call the RFC FM at the time of mapping

    Hi Experts,
    I am new to PI. I want to know How can we call the RFC function module at the time of message mapping.
    Can any plz explain me?
    Thanks
    Karthik

    First import the RFC FM by selecting import of RFC and IDOC supported button at SWC level and then provide the necessary R/3 details and get the RFC FM imported to IR .
    Once this is done you can have a drop down list for the selecting the source or target message for mapping
    Rajesh

  • How to reset keychain. Tried so many times, not working!

    frustrating...i want to reset my keychain password...coz I have forgotten what it is...tried the steps given by apple but still not working!!!!
    Help!

    Tutorial: Resolving Keychain Issues
    If you can't access your keychain, or forget your password If you can't get into your keychain file because you've forgotten your password or the keychain file appears to be corrupt, there are a couple of options.
    First, if you've forgotten your password, you can use the "Keychain First Aid" utility to make the keychain password the same as the login password. This can be accomplished via the following process:
      1. Open Keychain Access (located in Applications/Utilities)
      2. Go to the "Keychain Access" menu and select "Preferences"
      3. Click the "First Aid" tab
      4. Make sure the "Synchronize login keychain password" box is checked
      5. Close the Preferences window
      6. Go to the "Keychain Access" menu and select "Keychain First Aid"
      7. Enter your username and password
      8. Click the "Repair" button
    The second option is to completely delete your keychain then recreate it. This routine is useful if your keychain appears to be corrupt or otherwise inaccessible. This can be accomplished as follows:
      1. Launch Keychain Access (located in Applications/Utilities)
      2. Click "Show Keychains" in the lower-left corner of the window.
      3. Select the problematic keychain from the left-hand pane.
      4. Navigate to the "File" menu and select "Delete Keychain '(name of keychain)'"
      5. Check all options for deletion and press "OK"
      6. Create a new keychain by going to the "File" menu, then "New" and selecting
          "New Keychain"
      7. You can now make this keychain your default if you desire by selecting it, then
          going to the "File" menu and selecting "Make '(name of keychain)' Default"
    Login as root and perform repair In some cases, problems with keychains can only be resolved when logged in as the root user.
    First, you want to enable the root user:
      1. OS X Mountain Lion: Enable and disable the root user
      2. OS X Lion: Enable and disable the root user
      3. Mac OS X 10.6: Enabling the root user
      4. Enabling and using the "root" user in Mac OS X
    After enabling the root user, and logging in under this account, again open Keychain Access. First attempt repairs using Keychain First Aid, and failing that, delete then recreate the keychain as described above while logged in as root.
    Persistently asked for stored passwords If you are persistently asked for passwords in various applications that you have specified should be remembered in a keychain, your "login" keychain may not be active for one reason or another.
    Navigate to ~/Library/Keychains/ (this is the Library folder inside your user's home folder). Find the file named "login.keychain" and double-click it.
    Failing that, select the "login" keychain within the Keychain Access application and make sure it is the default keychain by going to the "File" menu and selecting "Make 'Login' Default"
    Turn off Keychain synchronization in applications having problems If specific applications are experiencing issues when accessing password-protected material, the Keychain may be to blame.
    The above comes from an article published on MacFixit.com.

  • How to call same vi for 4 times in a single vi?

    Hi.. I ve one vi named 1D array Mult. I need to call this vi four times in another vi program. The problem i ve experienced is all the four vi outputs are same.
     My question is: How to call the same vi for many number of times?
    Thanks in advance
    Solved!
    Go to Solution.

    There is no problem (programmatically) with calling the same VI by placing it 4 times onto the block diagram.  You do not have to rename them VI1, VI2, VIn if they all do the same thing, as you have done here with 1D-arrayMult...  It's just messy.
    You may need to make re-entrant in certain cases.
    A better way to do it would of course be to use a For loop as shown (I left what you did in tact).
    Are the two formula nodes identical?  I didn't check.  If so, use a for loop there too.  Build your A and B matrices to a 3D array and auto-index in For loop - single instance of your formula node.
    It is quite concerning what you have done in your 1-D Array VI.  I have no idea what it is supposed to be doing, but it appears to be nothing; it is completely wrong.
    There are backwards wires, your While loop does nothing (there is not shift-register) - how will it Stop?!, your cases 0, 1 and 2 are all the same!  Timer of 1000ms??! hmm..
    If you are trying to multiply two matrices (arrays) together then just use the native LV function to do it and to add them at the end try this:
    Hope this helps you!
    Message Edited by battler. on 03-12-2010 03:49 AM

  • How many times can i call the start()

    Hi All:
    I would like to know how many time you can actually call the start(),
    cuz my app is doing some really strange things,
    here is a fragament of my code:
    private void spawnThreads()
    ListIterator it = _vWorker.listIterator();
    while ( it.hasNext() ) {
    ClientWorker client_worker = (ClientWorker) it.next();
    client_worker.start();
    ClientWorker is another Thread, and in the run() of the ClientWorker, I have
    public void run() {
    Runtime rt = Runtime.getRuntime();
         try {
         // Start the timer
         Process child = rt.exec(_fileName);
         // gobble any error or output streams
    StreamGobbler errorGobbler = new
    StreamGobbler(child.getErrorStream(), "ERROR");
    StreamGobbler outputGobbler = new
    StreamGobbler(child.getInputStream(), "OUTPUT");
    errorGobbler.start();
    outputGobbler.start();
    child.waitFor();
                                            } catch (IOException e) {
                   System.err.println("IOException starting process!");
              } catch (InterruptedException e) {
                   System.err.println("Interrupted waiting for process!");
    I have a couple of cmd files to execute, so This spawnThreads() will be called a couple of times,and the funny thing is , it will execute the first cmd files, and that is it, it won't execute the 2nd or the 3rd cmd files, it will start the thread, but then the run()won't actually gets invoked,,,so I am just wondering if anyone of you has encountered this problem, perhaps you could lend me some insight on this?
    thanks,

    Yes, you can run the start() of a Thread more than once,
    here is my Test App,
    public static void main(String[] args) throws Exception {
    Vector vec = new Vector();
    for (int i=0;i<4;i++){
    ClientThread ct = new ClientThread();
    vec.add(ct);
    ListIterator it = vec.listIterator();
    while(it.hasNext()){
    ClientThread ct = (ClientThread)it.next();
    ct.start();
    and as you have already guessed it the run() of ClientThread simply does system.out.println()
    and here is the output
    run started
    run started
    run started
    run started
    so..yeah..I don't really have any idea what is happening to my app and why it is not executing any cmd files after executing the very first cmd file

  • How do I keep track of how many times a method is called inside anthr clas?

    I am writing code for a program that solves a Knight's Tour. A Knight's Tour is a path a Knight Chess piece can take around the board starting at any spot, touching every square once, and only once. I have to write 2 classes and one is provided. The provided class is encrypted and written by our professor. All I know that it does is simulates the game using the two classes I write and provides a print out and prompts to ask the user which square they want to move to next.
    The square class I have to write consists of a constructor that makes a square object that keeps track of its color (white/black) and its status (whether it has been visited, is occupied by the knight, or is free and has not be visited).
    The GameBoard class I have to write is what I am having problems with. I am writing a method that determines how many squares have been visited. The previous mentioned class that my professor wrote for me contains a method moveKnight();. The way the program works is that every time moveKnight() is called a square's status is changed to occupied and therefore visited later. moveKnight() will only work if the move requested by the user is a valid move.
    SO! My main problem (sorry for all the explaining, just trying to give you all the information) is that I don't know how to keep track of how many times moveKnight() is called by the program. If i can figure this out it should be simple. I'm new to java but i believe...
    if(moveKnight() is called)
    {count++;} //count is a member variable already initialized
    return count;
    the moveKnight() is called section within the if() statement is what I am unclear how to do. Please help.
    Thanks, Veritas

    in your case you want 'count' to be a class attribute rather than a local variable. But yes, incrementing it each time that the method is called will serve your purpose.

  • How many times USEREXIT_MOVE_FIELD_TO_VBKD is called in sales order

    Hi,
    I need to do some modifications to XVBKD internal structure depending data in XVBAP structure.
    Just wanted to know how many times USEREXIT_MOVE_FIELD_TO_VBKD is called while saving the order.
    Is it called for every item in VBKD table for that order or only for header?
    And what if a new item is added in order.. will it be called for that item too ??
    Please help..
    Regards,
    Anand

    Hi,
    I need to do some modifications to XVBKD internal structure depending data in XVBAP structure.
    Just wanted to know how many times USEREXIT_MOVE_FIELD_TO_VBKD is called while saving the order.
    Is it called for every item in VBKD table for that order or only for header?
    And what if a new item is added in order.. will it be called for that item too ??
    Please help..
    Regards,
    Anand

  • My volume buttons won't work. Music plays when I get a phone call. That is the only time my speaker makes noise. My settings are how they should be, the top button is not on silent, I've shut my phone off many times, I've totally restored my phone. Help

    My volume buttons won't work. Music plays when I get a phone call. That is the only time my speaker makes noise. My settings are how they should be, the top button is not on silent, I've shut my phone off many times, I've totally restored my phone. Help

    Genius reservation http://www.apple.com/retail/geniusbar/

  • I have been getting this message " no sim installed " too many times then my phone goes off , its iphone 4S , its really a problem i cant get calls or anything and im not sure how to take the SIM out then insert it again , what to do ??

    i have been getting this message " no sim installed " too many times then my phone goes off , its iphone 4S , its really a problem i cant get calls or anything and im not sure how to take the SIM out then insert it again , what to do ??

    Use the sim tool that came with your phone or a safety pin and just insert it into the hole at the side of the sim tray and push it in and then pull it out.  It is no more difficult than that and then just close it again.

  • How to count how many times the caller calls by using UCCX script?

    Hi there,
    My customer wants to do a few changes for their script as below:
    At the beginning of the script they are planning to add a new menu item, it will announce their new privacy policies and to continue the user must press the number one to continue to the Welcome.  If they do not press 1 then the message should repeat itself and if they again do not press 1 they should be transferred over to a live operator who will explain things and then be able to transfer them back to the message so that they can press 1 and continue.
    I modified their script as attached file, but i don't know how to count how many times the caller calls in "Get Reporting Statistic".
    Any helps would be appreciated.
    Thanks.

    Hi Aaron,
    At beginning, i want to use a variable to get number of mistake for the same caller, then we will send the call to right place to fulfill the customers' needs. 
    Do you have any suggestion for the new posted script?
    Thanks.

Maybe you are looking for