1、打开另一个界面让用户输入时,要让键盘自动弹出
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.textField becomeFirstResponder];
}
2、keyboard类型要与想要用户输入的内容匹配:只允许输入数字,则把keyboard类型设为Number;输入邮件地址的则设置为email address field
3、keyboard的return类型应设置合理,需要发送的操作则设置为send,一般完成输入则设置为done
4、当TextField内容为空时,应将keyboard的return(或done或其他)设置为disable
在TextField的Attributes inspector中选中Auto-enable Return Key
5、TextField用keyboard完成输入后按下return(或done或其他),将触发Did End On Exit事件
6、当TextField没有输入内容时,将导航栏上的done按钮隐藏
先将改按钮在Attributes inspector中取消Enable
TextField所在的视图的视图控制器实现协议<UITextFieldDelegate>
添加代码:(doneBarButton为视图控制器的一个outlet属性)
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *newText = [textField.text stringByReplacingCharactersInRange:range withString:string];
self.doneBarButton.enabled = ([newText length] > 0);
return YES;
}
7、对数据模型的属性,变量等进行修改时,应将其放在数据模型的方法中,然后在视图控制器中调用该方法
8、想让tableview某个cell不被选中,先设置tableview cell的属性selection为None,再添加以下代码:
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
return nil;
}
9.如果一个table view cell有两个功能,应添加一个详情按钮,当按下该按钮时,可以查看和编辑,当按下该行其他地方,则触发其他功能(如to-do list中标记某一个cell)。另一种方法则是当点击cell最左边的框框时打上标记,点击该cell其他地方则可以查看和编辑。
10.获取app沙盒Document文件夹路径(用于保存信息):
-(NSString *)documentDirectory
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths firstObject];
return documentsDirectory;
}
11."unrecognized selector"错误一般是没有实现对应的方法导致的
12.自定义初始化init方法格式:
if ((self = [super init])) {
// Initialization code here.Usually giving properties and instance variables their initial values
}
return self;
}
或者
- (id)initSomething {
if ((self = [super init])) {
// Initialization code here.Usually giving properties and instance variables their initial values
}
return self;
}
13.NSUserDefaults用于保存各种设置或记录app当前处于哪个screen等小型数据,NSUserDefaults类似NSDictionary或NSMutableDictionary
14.类中的方法最好独立于变量,比如下面的就不太适合
int total = 0;
for (NSNumber *number in _array) { //The _array variable here is an instance variable
total += [number intValue]; }
return total;
}
应该改用如下方式,以参数形式传递数据
- (int)addValuesFromArray:(NSArray *)array
{
int total = 0;
for (NSNumber *number in array) { //The array variable here is an local variable and a parameter
total += [number intValue];
}
return total;
}
15.CoreLocation不总是能获取满意的地理位置,因此编写需要获取地理位置的APP时,需要编写处理获取地理位置失败的代码
即实现以下delegate方法:locationManager: didFailWithError:
16.
The default value is armv7, which is the CPU architecture of the iPhone 3GS and later models. If your app requires additional features, such as Core Location to retrieve the user’s location, you should list them here. You can also add the item gps, in which case the app requires a GPS receiver. When that item is present, users cannot install the app on iPod touch hardware and certain iPads. For the full list of possible device capabilities, see the iOS App Programming Guide on the Apple Developer website.
17.NSDateFormatter用来格式化日期和时间,建议在App运行期间仅alloc和init一次,之后重用这个对象,如果经常对该对象进行分配和初始化,会减慢app运行,例如某个格式化日期和时间方法如下:
-(NSString *)formatDate:(NSDate *)theDate
{
static NSDateFormatter *formatter = nil;
if(formatter == nil)
{
formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
}
return [formatter stringFromDate:theDate];
}
18.对于一个简单的选择器,比如用户点击某个按钮或Cell后,弹出另一个视图(tableview),让用户选择某一行后自动回到前一个视图,则可用unwind segue,而不是delegate设计模式,具体可以看iOS 7版《iOS Apprentice 3 - My Locations》100页左右
19.实现当用点击输入框以外的地方时,隐藏虚拟键盘
在viewDidLoad中添加以下代码:
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard:)];
gestureRecognizer.cancelsTouchesInView = NO;
[self.tableView addGestureRecognizer:gestureRecognizer];
建立一个gestureRecognizer后,添加响应方法:
-(void)hideKeyboard:(UIGestureRecognizer *)gestureRecognizer
{
CGPoint point = [gestureRecognizer locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:point];
if(indexPath != nil && indexPath.section == 0 && indexPath.row == 0)
return;
[self.descriptionTextView resignFirstResponder];
}
20.在Navigation Controller导航栏添加Edit按钮
self.navigationItem.rightBarButtonItem = self.editButtonItem;