开发者

How to add button into textfield iphone?

开发者 https://www.devze.com 2023-01-26 06:21 出处:网络
-(void)ViewDidLoad{ UITextField *txt = [[UITextField alloc]initWithFrame:CGRectMake(140, 120, 160, 40)];
-(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)ref开发者_运维百科ersh:(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


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]];
0

精彩评论

暂无评论...
验证码 换一张
取 消