Question

I'm trying to write a program that draws a Sierpinski Carpet, so user should choose the depth of recursion and click a button and then a window with the carpet should appear.
But I'm a beginner so I don't know how to connect a button to a function that has a parameter NSRect .

I have a MyView class (subclass of NSView)

- (void)drawRect:(NSRect)dirtyRect
{
    [super drawRect:dirtyRect];

    if (b)
    draw(0, 0, 600, 600, 4);
}

void draw (double x1, double y1, double x2, double y2, int n)
{
    if (n > 0)
    {
        NSRect rect0 = NSMakeRect (x1 + (x2 - x1)/3, y1 + (y2 - y1)/3, (x2 - x1)/3, (y2 - y1)/3);
        [[NSColor blackColor] set];
        NSRectFill(rect0);
    
        draw (x1                  , y1                  , x1 +   (x2 - x1)/3  , y1 +   (y2 - y1)/3  , n - 1);
    
        draw (x1                  , y1 +   (y2 - y1)/3  , x1 +   (x2 - x1)/3  , y1 + 2*(y2 - y1)/3  , n - 1);
    
        draw (x1                  , y1 + 2*(y2 - y1)/3  , x1 +   (x2 - x1)/3  , y2                  , n - 1);
    
        draw (x1 +   (x2 - x1)/3  , y1 + 2*(y2 - y1)/3  , x1 + 2*(x2 - x1)/3  , y2                  , n - 1);
    
        draw (x1 + 2*(x2 - x1)/3  , y1 + 2*(y2 - y1)/3  , x2                  , y2                  , n - 1);
    
        draw (x1 + 2*(x2 - x1)/3  , y1 +   (y2 - y1)/3  , x2                  , y1 + 2*(y2 - y1)/3  , n - 1);
    
        draw (x1 + 2*(x2 - x1)/3  , y1                  , x2                  , y1 +   (y2 - y1)/3  , n - 1);
    
        draw (x1 +   (x2 - x1)/3  , y1                  , x1 + 2*(x2 - x1)/3  , y1 +   (y2 - y1)/3  , n - 1);
    }
}

- (BOOL) isFlipped { return YES; }

I also have a NSTextField *depth (which is an IBOutlet) where user should type the depth of recursion

Thanks for your answers!:)

Was it helpful?

Solution

You can't pass any arguments to methods that are connected to UIButtons (the most you can do is pass an int, using button.tag = 42). So my suggestion to you is to connect your UIButton to a method like this,

- (IBAction)goButton:(id)sender {
    // Here you get the level of recursion that the user entered in the UITextField.
    NSString *recursionLevel = self.recuersionLevelOutlet.text; 

    // And do whatever you need to do with it.
    ...
}

To connect the UIButton to the view, if you're unsure, do the following.

1) Open the .xib file of the screen.

2) Open the .m file by holdingoption and clicking it's name (this will open it in the side pane).

3) While holding control, click on the UIButton and drag to an empty space between @interface YourViewController () and @end in you .m file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top