gpt4 book ai didi

ios - 如何以编程方式调用带有 id sender 的 IBAction 方法?

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:31:49 25 4
gpt4 key购买 nike

我的代码中有以下 IBAction 方法。

-(IBAction)handleSingleTap:(id)sender
{
// need to recognize the called object from here (sender)
}


UIView *viewRow = [[UIView alloc] initWithFrame:CGRectMake(20, y, 270, 60)];
// Add action event to viewRow
UITapGestureRecognizer *singleFingerTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(handleSingleTap:)];
[self.view addGestureRecognizer:singleFingerTap];
[singleFingerTap release];
//
UILabel *infoLabel = [[UILabel alloc] initWithFrame:CGRectMake(5,30, 100, 20)];
infoLabel.text = @"AAAANNNNVVVVVVGGGGGG";
//[viewRow addSubview:infoLabel];
viewRow.backgroundColor = [UIColor whiteColor];

// display the seperator line
UILabel *seperatorLablel = [[UILabel alloc] initWithFrame:CGRectMake(0,45, 270, 20)];
seperatorLablel.text = @" ___________________________";
[viewRow addSubview:seperatorLablel];
[scrollview addSubview:viewRow];

如何调用 IBAction 方法,同时允许它接收该方法的调用者对象?

最佳答案

方法签名对于手势识别器和 UIControls 是通用的。两者都将在没有警告或错误的情况下工作。要确定发送者,首先要确定类型...

- (IBAction)handleSingleTap:(id)sender
{
// need to recognize the called object from here (sender)
if ([sender isKindOfClass:[UIGestureRecognizer self]]) {
// it's a gesture recognizer. we can cast it and use it like this
UITapGestureRecognizer *tapGR = (UITapGestureRecognizer *)sender;
NSLog(@"the sending view is %@", tapGR.view);
} else if ([sender isKindOfClass:[UIButton self]]) {
// it's a button
UIButton *button = (UIButton *)sender;
button.selected = YES;
}
// and so on ...
}

要调用它,直接调用它,让它连接的一个UIControl来调用它,或者让手势识别器调用它。他们都会工作。

关于ios - 如何以编程方式调用带有 id sender 的 IBAction 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12470710/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com