iOS开发的常见问题:
1、viewController的生命周期
viewDidLoad
viewDidUnload方法
(loadView/nib文件)来加载view到内存 ——>viewDidLoad函数进一步初始化这些view ——>内存不足时,调用viewDidUnload函数释放views —->当需要使用view时有回到第一步,如此循环。
- (void)viewDidLoad
is not called when the view controller is loaded; it is called when the view controller's view is loaded. 切记:viewDidUnload不是被移除的时候都会调用的函数。
2、UIKit - UIButton
操作button上的title,不要直接去操作label
不应该button.titleLabel.text = @"test";
这样越过了button的封装。
应该使用 [button setTtile: forState:]的方法。
3、JSON 数据转化
NSJSONSerialization 可以用来转换json 和 object
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:object
options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
error:&error];
jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
可以把一个dict To jsonstring
NSDictionary* dict = [NSJSONSerialization JSONObjectWithData:[operation.responseString dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
可以把一个json字符串 转为dict
4、常用的ViewController的操作
//根据 segue Identifier跳转界面
[self performSegueWithIdentifier:@"GotoTwo" sender:self];
//以modal 方式跳转
[self presentModalViewController:nil animated:YES];
//压进一个viewcontroller
[self.navigationController pushViewController:nil animated:YES];
//弹出一个viewcontroller 相当与返回上一个界面
[self.navigationController popViewControllerAnimated:YES];
// 以 modal跳转的返回方法
[self dismissModalViewControllerAnimated:YES];
Modal segues take over the whole screen, so any navigation bars, tool bars, or tab bars that are in the presenting controller will be covered up. If you want a navigation bar on this modal controller, you'll need to add one specifically to it, and add any buttons you want to that new navigation bar (or tool bar). If you don't want to do this, then don't present it modally, do a push to it.
(这句话的翻译:modal 的ViewController是全屏的,没有navigationBar,如果需要,可以自己添加;或者用push不用modal)
5、storyboard的unwind segue
- (IBAction)unwindSegueToIndexViewController:(UIStoryboardSegue *)segue {
}
exit 的连线
可以快速返回某一个ViewController。
6、关闭APP之前,会调用的函数
-(BOOL) application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder
{
return YES;
}
-(BOOL) application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder
{
return YES;
}
- (void)application:(UIApplication *)application willEncodeRestorableStateWithCoder:(NSCoder *)coder
{
NSDate *date = [NSDate date];
NSTimeInterval sec = [date timeIntervalSinceNow];
NSDate *currentDate = [[NSDate alloc] initWithTimeIntervalSinceNow:sec];
//设置时间输出格式:
NSDateFormatter *df = [[NSDateFormatter alloc] init ];
[df setDateFormat:@"yyyy年MM月dd日 HH小时mm分ss秒"];
NSString *na = [df stringFromDate:currentDate];
[coder encodeObject:na forKey:@"lastShutdownTime"];
}
- (void)application:(UIApplication *)application didDecodeRestorableStateWithCoder:(NSCoder *)coder
{
NSString * currentTime = [coder decodeObjectForKey:@"lastShutdownTime"];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"上次关闭时间"
message:currentTime
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
- (void)application:(UIApplication *)application willEncodeRestorableStateWithCoder:(NSCoder *)coder
{
NSDate *date = [NSDate date];
NSTimeInterval sec = [date timeIntervalSinceNow];
NSDate *currentDate = [[NSDate alloc] initWithTimeIntervalSinceNow:sec];
//设置时间输出格式:
NSDateFormatter *df = [[NSDateFormatter alloc] init ];
[df setDateFormat:@"yyyy年MM月dd日 HH小时mm分ss秒"];
NSString *na = [df stringFromDate:currentDate];
[coder encodeObject:na forKey:@"lastShutdownTime"];
}
- (void)application:(UIApplication *)application didDecodeRestorableStateWithCoder:(NSCoder *)coder
{
NSString * currentTime = [coder decodeObjectForKey:@"lastShutdownTime"];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"上次关闭时间"
message:currentTime
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
7、UI动画
下面是可以设置动画效果的属性:
- frame
- bounds
- center
- transform
- alpha
- backgroundColor
- contentStretch
8、UI事件
不建议直接向nextResponder发送消息,这样可能会漏掉父类对这一事件的其他处理。