Archive for the '全部' Category

Cocoa notes (12) 模态窗口

星期三, 五月 11th, 2011
1
2
3
4
- (void)showAddRecipe {
    RecipeAddViewController *viewController = ...;
    [self presentModalViewController:viewController animated:YES];
}
1
2
3
- (void)didAddRecipe {
    [self dismissModalViewControllerAnimated:YES];
}

Cocoa notes (6) UINavigationController和UITabBarController

星期二, 五月 3rd, 2011

UINavigationController插入和弹出View

1
2
3
4
- (void)pushViewController:(UIViewController *)viewController
                       animated:(BOOL)animated;

- (UIViewController *)popViewControllerAnimated:(BOOL)animated;

替换整个View Stack结构

1
2
- (void)setViewControllers:(NSArray *)viewControllers
                                                    animated:(BOOL)animated;

例子

1
2
3
4
5
6
7
8
9
10
- (void)applicationDidFinishLaunching
    // Create a navigation controller
    navController = [[UINavigationController alloc] init];

    // Push the first view controller on the stack
    [navController pushViewController:firstViewController animated:NO];

    // Add the navigation controller’s view to the window
    [window addSubview:navController.view];
}

popViewControllerAnimated方法从来不被主动调用
通过导航栏上的Back Button返回时自动弹出

UINavigationController有个属性叫navigationItem,下面有4种对象
leftBarButtonItem
titleView
rightBarButtonItem
backBarButtonItem

其中left和back会互相替换,left一般只出现在第一个视图里
以下是一个自定义leftBarButtonItem

1
2
3
4
5
6
7
8
9
10
- (void)viewDidLoad
{
    UIBarButtonItem *fooButton = [[UIBarButtonItem alloc]
        initWithTitle:@"Foo”
        style:UIBarButtonItemStyleBordered
        target:self
        action:@selector(foo:)];
    self.navigationItem.leftBarButtonItem = fooButton;
    [fooButton release];
}

设置一个right按钮

1
2
3
4
5
6
7
8
9
10
- (void)viewDidLoad
{
    UIBarButtonItem *addButton = [[UIBarButtonItem alloc]
        initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
        style:UIBarButtonItemStyleBordered
        target:self
        action:@selector(add:)];
    self.navigationItem.rightBarButtonItem = addButton;
    [addButton release];
}

navigation bar使用自定义的titleView

1
2
3
UISegmentedControl *segmentedControl = ...
self.navigationItem.titleView = segmentedControl;
[segmentedControl release];

自定义back button

1
2
3
4
self.title = @“Hello there, CS193P!”;
UIBarButtonItem *heyButton = [[UIBarButtonItem alloc] initWithTitle:@“Hey!” ...];
self.navigationItem.backButtonItem = heyButton;
[heyButton release];

使用UITabBarController

1
2
3
4
5
6
7
8
9
10
- (void)applicationDidFinishLaunching
    // Create a tab bar controller
    tabBarController = [[UITabBarController alloc] init];

    // Set the array of view controllers
    tabBarController.viewControllers = myViewControllers;

    // Add the tab bar controller’s view to the window
    [window addSubview:tabBarController.view];
}

自定义TabBar的按钮样式

1
2
3
4
5
6
7
8
9
- (void)viewDidLoad
{
    UITabBarItem *item = [[UITabBarItem alloc]
        initWithTitle:@“Playlists”
        image:[UIImage imageNamed:@“music.png”]
        tag:0];
    self.tabBarItem = item;
    [item release];
}

使用内置样式自定义TabBar的按钮样式

1
2
3
4
5
6
7
8
9
- (void)viewDidLoad
{
    UITabBarItem *item = [[UITabBarItem alloc]
        initWithTabBarSystemItem:
        UITabBarSystemItemBookmarks
        tag:0]
    self.tabBarItem = item;
    [item release];
}

Cocoa notes (4) UIView

星期二, 五月 3rd, 2011

在UIView中添加或删除子视图

1
2
- (void)addSubview:(UIView *)view;
- (void)removeFromSuperview;

插入视图的z-index控制

1
2
3
4
- (void)insertSubview:(UIView *)view atIndex:(int)index;
- (void)insertSubview:(UIView *)view belowSubview:(UIView *)view;
- (void)insertSubview:(UIView *)view aboveSubview:(UIView *)view;
- (void)exchangeSubviewAtIndex:(int)index withSubviewAtIndex:(int)otherIndex;

使用代码创建图片的4个步骤
1. 创建一个有尺寸的 CGGraphicsContext
2. 绘制
3. 将上下文(context)截图成位图
4. 清除

例如:

1
2
3
4
5
6
7
8
9
10
- (UIImage *)polygonImageOfSize:(CGSize)size {
    UIImage *result = nil;
    UIGraphicsBeginImageContext (size);

    // call your drawing code...
   
    result = UIGraphicsGetImageFromCurrentContext();
    UIGraphicsEndImageContext();
    return result;
}

隐藏视图theView.hidden = YES;

和UIView相关的坐标数据结构:CGPoint, CGSize, CGRect

frame和bounds的区别

用代码创建View

1
2
CGRect frame = CGRectMake(0, 0, 200, 150);
UIView *myView = [[UIView alloc] initWithFrame:frame];

1
2
3
4
5
CGRect frame = CGRectMake(20, 45, 140, 21);
UILabel *label = [[UILabel alloc] initWithFrame:frame];
[window addSubview:label];
[label setText:@”Number of sides:];
[label release]; // label now retained by window

自定义绘制视图

1
- (void)drawRect:(CGRect)rect;

自定义视图事件

1
2
3
4
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;

不要直接调用drawRect,使用

1
- (void)setNeedsDisplay;

UIKit提供的绘图方法

1
2
UIRectFill(CGRect rect);
UIRectFrame(CGRect rect);

在drawRect中获得图像上下文(context)

1
(CGContextRef)UIGraphicsGetCurrentContext(void);

获取并设置画笔颜色

1
2
3
UIColor *redColor = [UIColor redColor];
[redColor set];
// drawing will be done in red

获取并设置字体

1
2
UIFont *font = [UIFont systemFontOfSize:14.0];
[myLabel setFont:font];

例子:

1
2
3
4
5
6
7
8
9
10
- (void)drawRect:(CGRect)rect {
    CGRect bounds = [self bounds];
    [[UIColor grayColor] set];
    UIRectFill (bounds);
    CGRect square = CGRectMake (10, 10, 50, 100);
    [[UIColor redColor] set];
    UIRectFill (square);
    [[UIColor blackColor] set];
    UIRectFrame (square);
}

绘制Path

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();

    [[UIColor grayColor] set];
    UIRectFill ([self bounds]);

    CGContextBeginPath (context);
    CGContextMoveToPoint (context, 75, 10);
    CGContextAddLineToPoint (context, 10, 150);
    CGContextAddLineToPoint (context, 160, 150);
    CGContextClosePath (context);

    [[UIColor redColor] setFill];
    [[UIColor blackColor] setStroke];
    CGContextDrawPath (context, kCGPathFillStroke);
}

获取图片对象的三种方法

从application bundle中获得image

1
+[UIImage imageNamed:(NSString *)name]

从磁盘读取图片

1
-[UIImage initWithContentsOfFile:(NSString *)path]

从内存数据中创建图片

1
Use -[UIImage initWithData:(NSData *)data]

通过上下文创建图片有以下四步
1. 创建一个带尺寸的UIGraphicsBeginImageContext
2. 绘图
3. 截图,返回UIImage对象
4. 清理

1
2
3
4
5
6
7
8
9
10
- (UIImage *)polygonImageOfSize:(CGSize)size {
    UIImage *result = nil;
    UIGraphicsBeginImageContext (size);

    // call your drawing code...
   
    result = UIGraphicsGetImageFromCurrentContext();
    UIGraphicsEndImageContext();
    return result;
}

图片绘图方法

1
2
3
4
5
- [UIImage drawAtPoint:(CGPoint)point]
- [UIImage drawInRect:(CGRect)rect]
- [UIImage drawAsPatternInRect:(CGRect)rect]
// 绘制字符串
- [NSString drawAtPoint:(CGPoint)point withFont:(UIFont *)font]

动画
老方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
- (void)showAdvancedOptions {
    // assume polygonView and optionsView
    [UIView beginAnimations:@”advancedAnimations” context:nil];
    [UIView setAnimationDuration:0.3];

    // make optionsView visible (alpha is currently 0.0)
    optionsView.alpha = 1.0;

    // move the polygonView down
    CGRect polygonFrame = polygonView.frame;
    polygonFrame.origin.y += 200;
    polygonView.frame = polygonFrame;

    [UIView commitAnimations];
}

UIView 可以把动画事件代理给某对象

1
[UIView setAnimationDelegate:myController];

对象可实现此2方法对动画开始或结束事件进行处理

1
2
- (void)animationWillStart:(NSString *)animationID context:(void *)context;
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context;

也可以通过自定义的selector指定处理事件的action

1
2
[UIView setAnimationWillStartSelector:@selector(animationWillStart)];
[UIView setAnimationDidStopSelector:@selector(animationDidStop)];

关于UIView变形的一些函数

1
2
3
4
CGAffineTransform Functions (just a small example set)
CGAffineTransformScale (transform, xScale, yScale)
CGAffineTransformRotate (transform, angle)
CGAffineTransformTranslate (transform, xDelta, yDelta)

使用NSUserDefaults读写状态值
获取对象方法

1
+ (NSUserDefaults *)standardUserDefaults;

读写方法

1
2
3
4
- (int)integerForKey:(NSString *)key;
- (void)setInteger:(int)value forKey:(NSString *)key;
- (id)objectForKey:(NSString *)key;
- (void)setObject:(id)value forKey:(NSString *)key;

Cocoa notes (3) 基础

星期二, 五月 3rd, 2011

- copy 不增加retainCount

autorelease的作用

1
2
3
4
5
6
7
8
- (NSString *)fullName {
NSString *result;
result = [[NSString alloc] initWithFormat:@“%@ %@”,
firstName, lastName];
// [result release]; do not!
[result autorelease];
return result;
}

方法命名规则
凡命名中包含alloc, copy, or new等字样的,谁执行这个方法,谁负责release或autorelease

如下代码,需要release

1
2
3
NSMutableString *string = [[NSMutableString alloc] init];
// We are responsible for calling -release or -autorelease
[string autorelease];

下面的代码我们不用release,因为retainCount没有增加

1
2
3
NSMutableString *string = [NSMutableString string];
// The method name doesn’t indicate that we need to release it
// So don’t- we’re cool!

尽量使用代理(delegate)而不要用继承

UIApplicationDelegate的一些重要方法

1
2
3
4
5
6
7
- (void)applicationDidFinishLaunching:(UIApplication *)application;
- (void)applicationWillTerminate:(UIApplication *)application;

- (void)applicationWillResignActive:(UIApplication *)application;
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url;

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application;

Info.plist file的作用

定义app图标
statusbar的风格
定义是否iphone设备可以旋转
使用wifi
System requirement

NSObject中继承

1
- (void)awakeFromNib;

三种不同参数的事件方法处理定义

1
2
3
- (void)actionMethod;
- (void)actionMethod:(id)sender;
- (void)actionMethod:(id)sender withEvent:(UIEvent *)event;

用代码实现事件和action的绑定(不通过IB的Connector)

1
2
3
4
5
6
@interface UIControl
- (void)addTarget:(id)target action:(SEL)action
forControlEvents:(UIControlEvents)controlEvents;
- (void)removeTarget:(id)target action:(SEL)action
forControlEvents:(UIControlEvents)controlEvents;
@end

Cocoa notes (2)

星期四, 四月 28th, 2011

模拟器命令

String format

噩梦,惊醒后却发现如此真实,无法再睡了

星期五, 七月 30th, 2010

又拖拉了

星期三, 五月 12th, 2010

有个猎头姑娘从4月初就问我要英文简历,一直拖拖拉拉至今未发,想想学洋文也十余年,大小考试也都经历了,现在提笔不能文,会话磕磕巴巴,只有看美剧字幕大致可以忽略,遇到专业剧,法律啊,医学啊,还是一筹莫展,说起来我觉得自己词汇量也算不少,就是想写点什么老是那么几个句式未免寒酸,还是平时没地方用,拳不离手曲不离口,都生疏了。明天说什么也要编出一篇来。

android非常棒啊

星期一, 五月 3rd, 2010

入了HTC Desire,感觉非常爽,屏幕大,功能全,软件也多,特别是软件下载很方便,本来以为电子卖场里都是收费的东西,结果全是免费的,嘿嘿,比以前用nokia手机安装软件要方便一万倍,另外gmail,google map,用来上网都不错。下载了n个文件浏览器,各有利弊,所以只好都留着了,看到目录结构里那些熟悉的linux目录真是亲切啊,而且电子卖场里的软件在google code上很多都是开源项目,这样开发资源也很齐全了。另外android的中文用户还真是多,几乎每个我看到的软件下面都有中文用户的评论,很是吃惊。。

如果说有什么缺点的话,一个是太费电了,满电量的情况下,如果一直在上网操作,也就能用4、5个小时,另外就是触摸键盘还是不太方便,我安装了一个connectbot可以连接ssh,但如果用虚拟键盘操作vi什么的还是很费劲,也不知道control+按键是怎么按。另外android下的搜狗输入法真是渣,键盘还是t9输入法的,不如原来在e71上方便,还不如用自带的输入法。

想下载个东西真麻烦

星期日, 五月 2nd, 2010

要么就是论坛里的,不登录不能下,要么是各种捉迷藏似的下载站,根本搞不清楚哪个是最终的下载链接,这些还算好,找点英文资料吧,国外那些网盘网站都要求你用他的客户端,最可恶是csdn,下载不光要登录还要积分,sourceforge下载开源软件需要登录么,需要积分么,我真搞不懂他们在想什么,一切逆天行事的都长不了,嗯。

回暖

星期四, 四月 29th, 2010

根据force的建议,今天入手HTC desire,一款android手机,确实很好玩,我在想要试着写一个程序在手机上了,以前玩过一下java me,相信google的平台会很有意思吧。

看了american idol top6的那集,到了这份上竞争更激烈了,每个人都很出色,预测一下,下集的bottom 3是crystal, shibhan和mike…拼到这份上已经不光是歌唱的好不好,也包括感情和投入的程度,淘汰谁都很难抉择啊。