0
- (IBAction)popUpAreYouSureView:(id)sender {
    self.areYouSureView.hidden = NO;
}

- (IBAction)pressedYes {
    // if sender from popUpAreYouSureView was switch, do something

    // else if sender from popUpAreYouSureView was close, do something else
} 

- (IBAction)pressedNo {
    // if sender from popUpAreYouSureView was switch, do something

    // else if sender from popUpAreYouSureView was close, do something else
} 

假设我有 2 个按钮“切换”和“关闭”,它们都连接到 popUpAreYouSureView,当点击时会取消隐藏名为 areYouSureView 的视图。“你确定?” view 有 2 个按钮:“yes”和“no”,分别连接到 pressdYes 和pressedNo。

当我按下“是”或“否”时,我会检查“切换”或“关闭”按钮是否已取消隐藏我的视图。根据哪个按钮取消隐藏我的视图,我想执行不同的逻辑。我遇到的问题是我如何知道“切换”或“关闭”按钮是否在不存储该信息的情况下取消隐藏我的视图。

什么是干净的方法来做到这一点?我查看了代表,但这似乎不起作用,因为我为 switch 和 close 案例传递了相同的代表。选择器也有同样的问题。我是否必须存储 (id)sender 告诉我上一个操作是否来自开关或关闭按钮,以便连接到“是”或“否”按钮的操作可以查找并找出要执行的逻辑做?

4

3 回答 3

0

您需要跟踪导致该操作的原因,实际上没有办法解决这个问题。因此,请充分利用该sender参数。

__weak UIButton* m_buttonSelected;

- (IBAction)popUpAreYouSureView:(id)sender {
    self.areYouSureView.hidden = NO;
    m_buttonSelected = (UIButton*)sender;
}

- (IBAction)pressedYes {
    // if sender from popUpAreYouSureView was switch, do something
    if ([m_buttonSelected isEqual:self.switchButton]) {
        ...
    }
    // else if sender from popUpAreYouSureView was close, do something else
    else if ([m_buttonSelected isEqual:self.closeButton]) {
        ...
    }

    m_buttonSelected = nil;
} 

- (IBAction)pressedNo {
    // if sender from popUpAreYouSureView was switch, do something
    if ([m_buttonSelected isEqual:self.switchButton]) {
        ...
    }
    // else if sender from popUpAreYouSureView was close, do something else
    else if ([m_buttonSelected isEqual:self.closeButton]) {
        ...
    }

    m_buttonSelected = nil;
} 
于 2012-11-07T22:24:05.513 回答
0

您可以尝试在以下位置重新定义您的按钮操作popUpAreYouSureView

- (IBAction)popUpAreYouSureView:(id)sender {

  if ([sender...]) {
    [self.yesButton addTarget:self action:@selector(...) forControlEvents:...];
    [self.yesButton addTarget:self action:@selector(...) forControlEvents:...];
  } else {
    [self.yesButton addTarget:self action:@selector(...) forControlEvents:...];
    [self.yesButton addTarget:self action:@selector(...) forControlEvents:...];
  }
}

一个稍微好一点的解决方案是使用一个委托,pressedYes并在pressedNo实现中负责设置委托popUpAreYouSureView

- (IBAction)popUpAreYouSureView:(id)sender {

  if ([sender...]) 
     self.buttonsDelegate = ...;
  else
     self.buttonsDelegate = ...;
}



- (IBAction)pressedYes {
    [self.delegate doYes];
} 

- (IBAction)pressedNo {
    [self.delegate doNo];
} 

两种方法都应该有效。

于 2012-11-07T22:37:51.780 回答
0

一个快速(但不是那么漂亮)的解决方案是使用 areYouSureView 视图的“标记”属性。给每个按钮一个不同的标签(例如,1 表示关闭,2 表示切换),然后您可以执行以下操作:

- (IBAction)popUpAreYouSureView:(id)sender {
    self.areYouSureView.hidden = NO;
    self.areYouSureView.tag = ((UIView*)sender).tag; 
}

- (IBAction)pressedYes {
    if (self.areYouSureView.tag == 1)
        NSLog(@"Button 1");
    else
        NSLog(@"Button 2");
} 

- (IBAction)pressedNo {
    if (self.areYouSureView.tag == 1)
        NSLog(@"Button 1");
    else
        NSLog(@"Button 2");
} 
于 2012-11-07T22:25:51.413 回答