![](/img/trans.png)
[英]iOS 7 - Take the value of a textField inside a custom tableViewCell from another function
[英]iOS 7 - change Keyboard type of a textField in a custom tableViewCell
我有一個應用程序,用戶在其中選擇tableView中的項目,然后單擊行內的textField並放置一個數字(表示數量)。 我想做的是,當用戶單擊textField時,將出現符號和數字鍵盤,甚至更好的是數字鍵盤。 這是我到目前為止的內容:
在我用於自定義tableViewCell的類SearchResultTableViewCell.m
,我具有以下功能:
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField setKeyboardType:UIKeyboardTypeNumberPad];
[textField reloadInputViews];
return YES;
}
到目前為止,它不起作用。 我也試着做
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[quantity setKeyboardType:UIKeyboardTypeNumberPad];
[quantity reloadInputViews];
return YES;
}
quantity
是.h文件中聲明的IBOutlet UITextField
quantity
。
有任何想法嗎? 先感謝您。
您應該在custom tableview cell's
.h文件中聲明textfield
IBOutlet
,並在其中設置鍵盤類型
1)自定義單元的.m文件中的awakeFromNib
,
-(void)awakeFromNib
{
[self.textField setKeyboardType:UIKeyboardTypeNumberPad];
}
2)tableview的datasource方法的cellForRowAtIndexPath
。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Init you cell as you are doing right now
CustomCell* cell;
[cell.textField setKeyboardType:UIKeyboardTypeNumberPad];
}
您將在“ textFieldShouldReturn
”處完成所有這些工作,這是將文本輸入某個文本字段(數量文本字段或其他文本字段)的結尾 。
為什么不在“ textFieldShouldBeginEditing:(UITextField *)textField
”中工作 ,或者為什么不只是在情節提要或xib文件中設置“數量”文本字段的鍵盤類型呢?
您使用錯誤的方法來設置鍵盤!
當按下鍵盤上的返回按鈕時,將調用textFieldShouldReturn
。
在代碼中初始化textField或在.xib中設置屬性時,或者在用戶單擊textField時要設置鍵盤類型時,必須將鍵盤類型設置為以下類型的方法textFieldShouldBeginEditing:(UITextField *)textField
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.