textFiled要注意的地方

原帖:http://blog.csdn.net/dadalan/article/details/4233949


textField = [ [ UITextField alloc ] initWithFrame:CGRectMake(100, 14, 205, 20) ];

textField.placeholder = _(@"Input sms filter content");

textField.font = [ UIFont systemFontOfSize:14 ];

textField.keyboardType = UIKeyboardTypeDefault;

textField.tag = NewRuleControlFilterContentViewTag;

 

 

記住此時textField.text爲nil

 

千萬不要出現類似

strcpy(szTel, [ textField.text UTF8String ]);

如果這樣會導致程序出現異常。因爲textField.text爲nil  [ textField.text UTF8String ] 返回一個nil

會導致strcpy函數出現異常。

 

如何避免這種問題呢?

我們需要手動的爲textField.text分配空間。

例如:

textField.text = [ [ [ NSString alloc ] init ] autorelease ];

爲textField.text分配一塊內存,這樣[ textField.text UTF8String ]就會返回包含一個0字符的字符串。

 

如果我們不這樣做。

[ textField becomeFirstResponder ];

同樣不會出現異常,原因是becomeFirstResponder已經做了這個操作。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章