单击按钮时使用选择器调用不同类的方法

aqa*_*cha 2 iphone methods selector

我编写了一个方法并用一个按钮连接它,以便在单击按钮时调用它.现在我想要做的是在点击某个其他视图上的按钮时调用相同的方法.

我怎样才能做到这一点?我是否需要使用选择器或通知或简单的方法调用?

iPr*_*abu 11

在选择器中,将Target作为类的对象传递给该方法.

[anotherButton addTarget:objectOfAnotherClass action:@selector(yourMethodInAnotherClass) forControlEvents:UIControlEventTouchUpInside];
Run Code Online (Sandbox Code Playgroud)

要么

只需为按钮方法所在的类创建一个对象,并以普通方式调用该方法

In First Class say firstView
-(IBAction) yourButtonMethod : (id)sender
{
   //Some Code
}

In another class
-(IBAction) yourAnotherButtonMethod : (id)sender
{
 firstView *firstViewObject = [firstView alloc] init];
 [firstViewObject yourButtonMethod:sender];
}
Run Code Online (Sandbox Code Playgroud)

通过为按钮设置tagValues来区分发件人