【问题标题】:Showing NSView with a button click单击按钮显示 NSView
【发布时间】:2013-12-02 07:45:26
【问题描述】:

我正在尝试编写一个绘制Sierpinski Carpet 的程序,因此用户应该选择递归深度并单击一个按钮,然后应该会出现一个带有地毯的窗口。
但我是初学者,所以我不知道如何将按钮连接到具有参数 NSRect 的函数。

我有一个 MyView 类(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; }

我还有一个 NSTextField *depth(这是一个 IBOutlet),用户应该在其中输入递归的深度

感谢您的回答!:)

【问题讨论】:

  • 只是几个cmets,但单击按钮不应传递任何参数,而应来自其他地方,例如文本字段。您还可以使用isHidden: 方法“隐藏”视图,仅在必要时(单击按钮时)显示它。

标签: objective-c macos cocoa nsview


【解决方案1】:

您不能将任何参数传递给连接到 UIButtons 的方法(您最多只能传递一个 int,使用 button.tag = 42)。 所以我给你的建议是将你的 UIButton 连接到这样的方法,

- (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.
    ...
}

要将 UIButton 连接到视图,如果您不确定,请执行以下操作。

1) 打开屏幕的.xib文件。

2) 按住选项并单击其名称(这将在侧窗格中打开)打开 .m 文件。

3) 按住 control 的同时,点击 UIButton 并拖动到 .m 文件中 @interface YourViewController () 和 @end 之间的空白处。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-07
    • 1970-01-01
    • 1970-01-01
    • 2013-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多