Objective c 如何根据以前的操作进行不同的操作';objC中的发件人是谁?
假设我有两个按钮“switch”和“close”,它们都连接到popUpAreYouSureView,点击该按钮时会取消隐藏名为areYouSureView的视图。“确定吗?”视图有两个按钮:“是”和“否”,分别连接到pressdYes和pressedNo 当我按下“是”或“否”时,我将检查“开关”或“关闭”按钮是否取消隐藏我的视图。根据取消隐藏视图的按钮,我希望执行不同的逻辑。我遇到的问题是,如何知道“开关”或“关闭”按钮是否在不存储该信息的情况下取消隐藏我的视图Objective c 如何根据以前的操作进行不同的操作';objC中的发件人是谁?,objective-c,xcode,function,delegates,selector,Objective C,Xcode,Function,Delegates,Selector,假设我有两个按钮“switch”和“close”,它们都连接到popUpAreYouSureView,点击该按钮时会取消隐藏名为areYouSureView的视图。“确定吗?”视图有两个按钮:“是”和“否”,分别连接到pressdYes和pressedNo 当我按下“是”或“否”时,我将检查“开关”或“关闭”按钮是否取消隐藏我的视图。根据取消隐藏视图的按钮,我希望执行不同的逻辑。我遇到的问题是,如何知道“开关”或“关闭”按钮是否在不存储该信息的情况下取消隐藏我的视图 什么是干净的方法?我查看了代
什么是干净的方法?我查看了代理,但这似乎不起作用,因为我在切换和关闭案例中都传递了相同的代理。选择器也有同样的问题。我是否必须存储(id)发送器,该发送器告诉我上一个动作是否来自开关或关闭按钮,以便连接到“是”或“否”按钮的动作可以查找它并找出要执行的逻辑?您需要跟踪导致该动作的原因,这是无法避免的。因此,请充分利用
sender
参数
- (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
}
一个快速(但不是很漂亮)的解决方案是使用areYouSureView视图的“tag”属性。
给每个按钮一个不同的标签(例如,1表示关闭,2表示开关),然后您可以执行以下操作:
__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;
}
您可以尝试在
popUpAreYouSureView
中重新定义按钮操作:
- (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");
}
- (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:...];
}
}
一个稍微好一点的解决方案是在按Yes
和按No
实现中使用委托,注意在popUpAreYouSureView
中设置委托:
- (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");
}
- (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:...];
}
}
这两种方法都应该有效。什么类型是
self。你确定要查看?