如何在Objective-c中编写ok按钮操作?

时间:2015-05-13 10:00:02

标签: objective-c xcode macos

我是的新人。我在对话框中创建了一个对话框和确定按钮。现在,我正在对话框上做一些操作。之后,我想点击“确定”按钮关闭对话框。为此我做的就像

.h文件中的

public class DrawableImageView extends ImageView implements OnTouchListener 
{
    float downx = 0;
    float downy = 0;
    float upx = 0;
    float upy = 0;

    Canvas canvas;
    Paint paint;
    Matrix matrix;

    public DrawableImageView(Context context) 
    {
        super(context);
        setOnTouchListener(this);
    }

    public DrawableImageView(Context context, AttributeSet attrs) 
    {
        super(context, attrs);
        setOnTouchListener(this);
    }

    public DrawableImageView(Context context, AttributeSet attrs,
            int defStyleAttr) 
    {
        super(context, attrs, defStyleAttr);
        setOnTouchListener(this);
    }

    public void setNewImage(Bitmap alteredBitmap, Bitmap bmp)
    {
        canvas = new Canvas(alteredBitmap );
        paint = new Paint();
        paint.setColor(Color.GREEN);
        paint.setStrokeWidth(5);
        matrix = new Matrix();
        canvas.drawBitmap(bmp, matrix, paint);

        setImageBitmap(alteredBitmap);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) 
    {
        int action = event.getAction();

        switch (action) 
        {
        case MotionEvent.ACTION_DOWN:
            downx = getPointerCoords(event)[0];//event.getX();
            downy = getPointerCoords(event)[1];//event.getY();
            break;
        case MotionEvent.ACTION_MOVE:
            upx = getPointerCoords(event)[0];//event.getX();
            upy = getPointerCoords(event)[1];//event.getY();
            canvas.drawLine(downx, downy, upx, upy, paint);
            invalidate();
            downx = upx;
            downy = upy;
            break;
        case MotionEvent.ACTION_UP:
            upx = getPointerCoords(event)[0];//event.getX();
            upy = getPointerCoords(event)[1];//event.getY();
            canvas.drawLine(downx, downy, upx, upy, paint);
            invalidate();
            break;
        case MotionEvent.ACTION_CANCEL:
            break;
        default:
            break;
        }
        return true;
    }

    final float[] getPointerCoords(MotionEvent e)
    {
        final int index = e.getActionIndex();
        final float[] coords = new float[] { e.getX(index), e.getY(index) };
        Matrix matrix = new Matrix();
        getImageMatrix().invert(matrix);
        matrix.postTranslate(getScrollX(), getScrollY());
        matrix.mapPoints(coords);
        return coords;
    }
}
.m文件中的

“我不知道如何在.m文件中编写ok按钮的代码。我只想在点击OK按钮时关闭对话框。

3 个答案:

答案 0 :(得分:7)

您必须使IBAction直接获取点击事件

,而不是创建IBOutlet

查看此图片...
enter image description here

这是.m文件,我直接创建IBAction

如果您想以编程方式提供点击事件,请关注@Nicolas Buquet回答

答案 1 :(得分:1)

将此添加到您的代码中:

[OkBtn addTarget:self action:@selector(okButtonTapped:) forControlEvents:UIControlEventTouchUpInside];

并将此方法添加到您的班级:

- (void)okButtonTapped:(UIButton *)sender {
NSLog(@"Ok button was tapped: dismiss the view controller.");
}

当你点击按钮并移开手指(“向上”部分)时,将调用okButtonTapped:方法。

答案 2 :(得分:0)

IBAction创建NSButton。并将IBAction连接到按钮。

在接口文件(.h)文件中写,

- (IBAction) okButtonAction : (id) sender;

并在您的实施文件(.m)

- (IBAction) okButtonAction : (id) sender {
   NSLog(@"OK Button action here");
}