我有UITextField的名字_nameField 我想通过@Selector()传递_nameField.text到
-(void)userSetStats:(NSString *)name{
这是我的Uitextfield
_nameField = [[UITextField alloc]initWithFrame:CGRectMake(self.view.frame.size.height/2-100, self.view.frame.size.width/2-20, 200, 40)];
_nameField.delegate = self;
[self.view addSubview:_nameField];
这是我的按钮
UIButton *confirm = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.height/2-50, self.view.frame.size.width/2+60, 100, 40)];
[confirm setTitle:@"Confirm" forState:UIControlStateNormal];
[confirm addTarget:self action:@selector(userSetStats:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:confirm];
这是我的userSetStats:
-(void)userSetStats:(NSString *)name{
NSMutableDictionary *userData = [[NSMutableDictionary alloc]init];
[userData setObject:name forKey:@"Name"];
现在我可以发送价值,但我得到的价值是
Name = "<UIButton: 0x9a2ddd0; frame = (190 220; 100 40); opaque = NO; layer = <CALayer: 0x9a2df00>>";
我试着在这个网站上找到任何答案我找到了一些,但我不明白。
答案 0 :(得分:0)
addTarget的选择器只能是发件人。在这种情况下,您不需要参数。
[confirm addTarget:self action:@selector(userSetStats) forControlEvents:UIControlEventTouchUpInside];
,选择器是......
-(void)userSetStats
{
NSMutableDictionary *userData = [[NSMutableDictionary alloc]init];
[userData setObject:_nameField.text forKey:@"Name"];
}
答案 1 :(得分:0)
Action methods in iOS具有以下形式:
- (IBAction)doSomething:(id)sender;
- (IBAction)action:(id)sender forEvent:(UIEvent *)event;
sender
是触发操作的控件。因此:
-(void)userSetStats:(id)sender {
NSMutableDictionary *userData = [[NSMutableDictionary alloc]init];
如果你想要(发送)按钮的标题,那么:
UIButton *button = (UIButton *)sender;
[userData setObject:button.title forKey:@"Name"];
否则如果你想要文本字段中的文本,那么:
[userData setObject:_nameField.text forKey:@"Name"];