2010-11-23 125 views
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{ 

} 

上面的代碼顯示一個文本框的右下角有按鈕,但是當我點擊它時模擬器崩潰。我無法理解它背後的區域。任何機構告訴我錯在哪裏如何將按鈕添加到textfield iphone?

回答

1

你的方法是spelled -refersh:當你發送@選擇器(刷新:)。是否錯字?

而且,你似乎是泄漏的UITextField,這是很容易解決:

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