iOS_User
iOS_User

Reputation: 1382

How to add button into textfield iphone?

-(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

Upvotes: 0

Views: 2920

Answers (1)

Costique
Costique

Reputation: 23722

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

Upvotes: 1

Related Questions