如何在 iPhone 文本字段中添加按钮?

发布于 2024-10-03 14:40:17 字数 810 浏览 3 评论 0原文

-(void)ViewDidLoad{
    UITextField *txt = [[UITextField alloc]initWithFrame:CGRectMake(140, 120, 160, 40)];
    txt.returnKeyType = UIReturnKeyDone;
    txt.placeholder = @"enter name";
    [txt setBorderStyle:UITextBorderStyleRoundedRect];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"icon_chapter.png"] forState:UIControlStateNormal];
    button.imageEdgeInsets = UIEdgeInsetsMake(0, -16, 0, 0);
    [button addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventTouchUpInside];
    txt.rightView = button;
    txt.rightViewMode = UITextFieldViewModeAlways;
    [self.view addSubview:txt];
}

-(IBAction)refersh:(id)sender{

}

上面的代码显示了一个文本字段,其右上角有按钮,但是当我单击它时,模拟器崩溃了。我不明白它背后的区域是什么。谁能告诉我哪里错了

-(void)ViewDidLoad{
    UITextField *txt = [[UITextField alloc]initWithFrame:CGRectMake(140, 120, 160, 40)];
    txt.returnKeyType = UIReturnKeyDone;
    txt.placeholder = @"enter name";
    [txt setBorderStyle:UITextBorderStyleRoundedRect];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"icon_chapter.png"] forState:UIControlStateNormal];
    button.imageEdgeInsets = UIEdgeInsetsMake(0, -16, 0, 0);
    [button addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventTouchUpInside];
    txt.rightView = button;
    txt.rightViewMode = UITextFieldViewModeAlways;
    [self.view addSubview:txt];
}

-(IBAction)refersh:(id)sender{

}

above code shows a textfield with button at its right corner but when i clicked on it simulator crash. i can't understand whats the region behind it. any body tell where i'm wrong

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空名 2024-10-10 14:40:17

当您发送 @selector(refresh:) 时,您的方法拼写为 -refersh: 。这是错字吗?

另外,您似乎泄漏了 UITextField,这很容易修复:

[self.view addSubview:[txt autorelease]];

Your method is spelled -refersh: while you send @selector(refresh:). Is it the typo?

Also, you seem to be leaking the UITextField, which is quite easy to fix:

[self.view addSubview:[txt autorelease]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文