gpt4 book ai didi

ios uibutton 添加目标,从同一 View 传递给选择器参数

转载 作者:行者123 更新时间:2023-11-28 20:04:30 28 4
gpt4 key购买 nike

我有点卡在这里...我正在以编程方式创建一个小窗体作为 subview 。我以编程方式添加了一个确认按钮并向其添加了一个目标。但我希望能够将表单内容传递给选择器函数。这可能吗?看起来像这样:

Details = [[UIView alloc]initWithFrame:CGRectMake(40, 20, 250, 300)];
Details.backgroundColor = [UIColor whiteColor];
Details.layer.cornerRadius = 5;
Details.alpha = 0;

UILabel *NameLabel = [[UILabel alloc]initWithFrame:CGRectMake(60, 25, 75, 20)];
[NameLabel setText:@"Place Name:"];
[NameLabel setFont:[UIFont systemFontOfSize:12]];
[Details addSubview:NameLabel];

UITextField *NameTV = [[UITextField alloc]initWithFrame:CGRectMake(135, 25, 110, 20)];
NameTV.borderStyle = UITextBorderStyleRoundedRect;
NameTV.font = [UIFont systemFontOfSize:12];
[Details addSubview:NameTV];

confirm = [[UIButton alloc]initWithFrame:CGRectMake(100, 250, 75, 40)];
[confirm setTitle:@"Set Marker" forState:UIControlStateNormal];
[confirm setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
confirm.titleLabel.font = [UIFont systemFontOfSize:14];
[confirm setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];

[confirm addTarget:self action:@selector(confirmMarker://add text field data here) forControlEvents:UIControlEventTouchUpInside];
//attach an array to the button???
[Details addSubview:confirm];

- (void) confirmMarker:(NSString)someString{
NSLog(@"%@", someString);
}

我的问题是当形成表单 View 时目标被添加...所以即使有人写了一些东西然后点击按钮 someString 也会是空的...对吗?有没有办法做到这一点?谢谢...

最佳答案

也许我遗漏了什么,但如果您已经可以访问该值,为什么还需要通过选择器传递它?

- (void) confirmMarker:(NSString)someString{
NSLog(@"%@", NameTv.text);
}

按钮的预设选择器将其自身作为变量传递,因此它发送的是:

- (void) confirmMarker:(id)sender{
// Sender is the button that sent
NSLog(@"%@", NameTv.text);
}

要使用您自己的变量执行选择器,您必须执行如下操作:

IMP imp = [ob methodForSelector:selector];
void (*func)(id, SEL, NSString *) = (void *)imp;
func(ob, selector, @"stringToSend");

但是,如果你用按钮触发它,你必须首先获取 textView.text 才能传递它,所以为什么不直接使用方法一,面向按钮标准回调:

- (void) confirmMarker:(id)sender {
NSLog(@"%@", NameTv.text);
}

关于ios uibutton 添加目标,从同一 View 传递给选择器参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22544116/

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