感觉 《iphone开发秘籍》, 比所谓的经典**,好多了。。
command+shift+R 查看控制台
command+shift+Y 打开主调试器
第四章:《警告用户》
通过UIActionSheet UIAlertView对象可与用户对话。
1.重定向stderr
freopen( [logPath fileSystemRepresentation], "a", stderr );
2.构建自定义日志函数
#include <stdarg.h>
void dolog(id formatstring, ...)
{
va_list arglist;
if(formatstring){
va_start(arglist, formatstring);
id outstring = [[NSString alloc] initWithFormat:formatstring arguments:arglist];
fprintf(stderr, "%s\n", [outstring UTF*String]);
[outstring release];
va_end(arglist);
}
}
书上这段貌似挺傻逼的,有毛用!!!
UIAlertView *baseAlert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@""
delegate:self cancelButtonTitle:nil otherButtonTitle:@"OK", nil];
与警告信息交互,使用委托:alertView:clickedButtonAtIndex:
创建带单行按钮的UIAlertView
-(void)presentSheet{
UIAlertView *baseAlert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Please select abutton" delegate:self cancelButtonTitle:nil otherButtonTitles:@"One",@"Two",@"Three",nil];
[baseAlert setNumberOfRows:3];
[baseAlert show];
}
-(void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
printf("User Pressed Button %d\n", buttonIndex+1);
[alertView release];
}
委托也蛮恶心的,直接调用函数不就得了!!!
带定时器的UIAlertView
-(void) performDismiss:: (NSTimer*)timer //这个timer参数没用到,写上去干吗!!sb
{
[baseAlert dismissWithClickedButtonIndex:0 animated:NO];
}
-(void) presentSheet{
baseAlert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Message to user with asynchronous information" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
[NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(performDismiss:)
userInfo:nil repeats:NO]; //相当有病!!!没有按钮的,直接sleep,不就完了,扯淡!!!
[baseAlert show];
}
使用UIAlertView请求用户的文本
-(void)alertView::(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
printf("User Pressed Button %d\n",buttonIndex+1);
printf("Text Field 1:%s\n", [[[modalView textFieldAtIndex:0] text] cStringUsingEncoding:NSUTF8StringEncoding]);//哪里来的modalView,表示很费解!!!
printf("Text Field 2: %s\n", [[[modalView textFieldAtIndex:1] text] cStringUsingEncoding:1]);
[alertView release];
}
-(void)presentSheet{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Enter Information" message:@"Specify the Name and URL" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",nil];
[alert addTextFieldWithValue:@"" label:@"Enter Name"];
[alert addTextFieldWithValue:@"http://" label:@"Enter URL"];
^^^^详见课本
}
-(void)actionSheet:(UIActionSheet*)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
printf("User Pressed Button %d\n",buttonIndex+1);
[actionSheet release];
}
-(void)presentSheet{
UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:@"File Management" delegate:self cancelbuttonTitle:@"Cancel" destructiveButtonTitle:@"Delete File" otherButtonTitles:@"Rename File", @"Email File", nil ];
[menu showInView:contentView];
}
等待进度显示;
UIActivityIndicatorView显示一个旋转地圆
UIProgressView 显示一个进度条
UIProgressHUD
@interface UIProgressHUD: NSObject
-(void) show:(BOOL) yesOrNo;
-(UIProgressHUD*)initWithWindow:(UIView*)window;
-(void)setText:(NSString*)theText;
@end
-(void)killHUD:(id) aHUD
{
[aHUD show:NO];
[aHUD release];
}
-(void)presentSheer{
id HUD = [[UIProgressHUD alloc] initWithWindow: [contentView superView]];
[HUD setText:@"Downloading File. Please wait."];
[HUD show:YES];
[self performSelector:@selector(killHUD:) withObject:HUD afterDelay:5.0];
}
这里写的真鸡巴烂。书中说用NSThread的detatchThreadSelector:toTarget:withObject:方法。但是根本就没写!!!
添加自定义,可轻击的覆盖层。(代码太多,不贴)
创建下滑式警告。(代码太多,不贴)
添加状态栏图像
添加应用程序标记
NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0];
NSString *caldate = [[now dateWithCalendarFormat:@"%b" timeZone:nil] description];
[[UIApplication sharedApplication] setApplicationBadge:caldate]; //书中说,不确定这个是否合法、