简体   繁体   中英

ios uibutton add target, pass to selector parameters from same view

I'm kind of stuck here...programmatically I'm creating a small form as a subview. Programmatically I'm adding a confirm button and adding a target to it. But I want to be able to get the form contents passed to the selector function. Is this possible? Looks something like this:

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);
}

My question is the target gets added when the form view is formed...so someString will be empty even if someone writes something and then clicks the button...right? Is there a way to do this? Thanks...

Perhaps I'm missing something, but why do you need to pass it through the selector if you already have access to the value?

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

A button's preset selector passes itsself as the variable, so what it's sending is this:

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

To perform a selector with your own variables, you'd have to do something like this:

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

But, if you triggered that with the button, you'd have to get the textView.text in the first place to pass it, so why not just go with method one, oriented for a buttons standard callback:

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

No, it's not possible. The parameter in selector only the sender itself (button in your case). So, you must store needed information somewhere else, ivar, for example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM