【问题标题】:How to add button into textfield iphone?如何将按钮添加到文本字段 iphone?
【发布时间】:2010-11-23 20:27:08
【问题描述】:
-(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{

}

上面的代码显示了一个带有按钮的文本字段,但是当我点击它时,模拟器崩溃了。我不明白它背后的区域是什么。任何人都告诉我哪里错了

【问题讨论】:

    标签: iphone objective-c uibutton uitextfield


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2016-01-12
      • 2021-02-15
      • 1970-01-01
      • 2020-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多