路随人茫茫

美东浮生记

  • 首页
  • Mechanical Turk
  • 数码生活
  • 机器翻译
  • 胡言乱语
  • 行走天下

IPhone开发笔记(3) 使用PickerView

Author: admin

Picker View需要两个接口:DataSource和Delegate。
在定义接口的地方要有如下定义:

一个类必须至少实现下列方法:

@interface SingleComponentPickerViewController : UIViewController
<UIPickerViewDelegate, UIPickerViewDataSource>
#pragma mark -
#pragma mark Picker Data Source Methods

- (NSInteger) numberOfComponentsInPickerView: (UIPickerView *) pickerView
{
    return 1;
}

- (NSInteger) pickerView: (UIPickerView *)pickerView
numberOfRowsInComponent: (NSInteger) component
{
    return [pickerData count];
}

#pragma mark Picker Delegate Methods
- (NSString *) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger) row
             forComponent:(NSInteger) component{
    return [pickerData objectAtIndex:row];
}

其中Data Source提供了数据,而Delegate则实际获取数据。

如果要接收某个Component被选择的消息,则要实现一个delegate方法:

- (void) pickerView:(UIPickerView *) pickerView
didSelectRow: (NSInteger) row
        inComponent: (NSInteger) component{
    if (component == kStateComponent) {
        NSString *selectedState = [self.states objectAtIndex:row];
        NSArray *array = [stateZips objectForKey:selectedState];
        self.zips = array;
        [picker selectRow:0 inComponent: kZipComponent animated:YES];
        [picker reloadComponent:kZipComponent];
    }
}

另外,下面的例子简要说明如何使用NSDictionary

    NSBundle *bundle = [NSBundle mainBundle];
    NSString *plistPath = [bundle pathForResource:@"statedictionary" ofType:@"plist"];
    NSDictionary *dictionary = [[NSDictionary alloc]
                                initWithContentsOfFile:plistPath];
    self.stateZips = dictionary;
    [dictionary release];
    NSArray *components = [self.stateZips allKeys];
    NSArray *sorted = [components sortedArrayUsingSelector:@selector(compare:)];
    self.states = sorted;
    NSString *selectedState = [self.states objectAtIndex:0];
    NSArray *array = [stateZips objectForKey:selectedState];
    self.zips = array;

更改每个组件的宽度也可以用代理实现:

- (CGFloat) pickerView: (UIPickerView *) pickerView
widthForComponent: (NSInteger) component
{
    if(component == kZipComponent)
        return 90;
    return 200;
}

注意到前面我们在- (NSString *) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger) row forComponent:( NSInteger) component 代理方法中,实现的是返回一个NSString。如果要显示图片,则需要实现另一个代理方法

- (UIView *) pickerView : (UIPickerView *)pickerView viewForRow:(NSInteger) row forComponent (NSInteger) component

该方法返回一个View对象。可以用下面方法创建Image View

UIImage *bar = [UIImage imageNamed:@"filename"];
UIImageView *x = [[UIImageView alloc] initWithImage:bar];

最后,注意一个方法,可以用以下方法动态为不同名字的field设置值:

[self setValue:value forKey:fieldName]

Field name 可以是字符串,同时也是变量名称。很动态哦。

August 9th, 2010  |  Posted in 胡言乱语  |  No Comments »

IPhone 开发笔记 (2)构造和添加View的一般过程

Author: admin

构造、添加View并在View之间切换是IPhone开发最常见的问题。一般来说,要构造一个View需要以下几个步骤:

  1. 添加新的Controller类型。具体方法是File–>New File–>Cocoa Touch Class–>UIViewController subclass。
  2. 添加xib文件。具体方法是File–>New File–>User Interface–>View Xib 。
  3. 在新的xib文件中制定需要使用的Controller。具体方法是用IBuilder打开对应的xib文件,选择File’s Owner,将其Class Identity –> Class 属性改成对应Controller。而后,将File’s Owner的”view” Outlet连接到View对象(按住Command,将File’s Owner)拖到View上,放开,选”view”。
  4. 如果需要重载UIView class,将View的Class对应地修改。

切换View分两种,第一是在启动地时候选择一个View。这需要修改AppDelegate头文件和m文件。首先,要修改MainWindow.xib。首先要把要显示地View的一个实例添加到xib中。将Library 中一个View Controller 添加到MainWindow.xib中(放到File’s Owner边上)。而后把其控制器改为你要选用的View Controller subclass。这样,在启动时,程序自动加载MainWindow.xib的同时,也就加载了你定义的这个View。如果我们需要在delegate里头操作这个Controller的话,就需要在Delegate类中添加一个Outlet,并把View Instance连接到这个Outlet。而真正显示这个View,必须在delegate中做以下连接:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    [window addSubview:switchViewController.view]; // Key Point!
    [window makeKeyAndVisible];

    return YES;
}

而如果要动态加载和切换View,则注意以下几个函数:

      if(self.yellowViewController == nil){
        YellowViewController *yellowController =
        [[YellowViewController alloc]
         initWithNibName:@"YellowView" bundle:nil];
        self.yellowViewController = yellowController;
        [yellowController release];
    }
    if (self.blueViewController.view.superview == nil) {
        [yellowViewController.view removeFromSuperview];
        [self.view insertSubview:blueViewController.view atIndex:0];
    }else {
        [blueViewController.view removeFromSuperview];
        [self.view insertSubview:yellowViewController.view atIndex:0];
    }

一个最复杂的动画切换,目前就给个例子:

    [UIView beginAnimations:@"View Flip"
                    context:nil];
    [UIView setAnimationDuration:1.25];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    if (self.blueViewController.view.superview == nil) {
        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
                               forView:self.view cache:YES];
        [yellowViewController viewWillDisappear:YES];
        [blueViewController viewWillAppear:YES];
        [yellowViewController.view removeFromSuperview];
        [self.view insertSubview:blueViewController.view atIndex:0];
        [yellowViewController viewDidDisappear:YES];
        [blueViewController viewDidAppear:YES];
    }else {
        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
                               forView:self.view cache:YES];
        [yellowViewController viewWillAppear:YES];
        [blueViewController viewWillDisappear:YES];
        [blueViewController.view removeFromSuperview];
        [self.view insertSubview:yellowViewController.view atIndex:0];
        [yellowViewController viewDidAppear:YES];
        [blueViewController viewDidDisappear:YES];
    }
    [UIView commitAnimations];


特别需要注意,今天尝试实现Tab Controller,一旦需要加载Outlet的时候就收到SIGABRT。问题何在?下面详细写写如何使用Tab Controller。

第一步,建立一个新的Tab Bar Application。如果不准备使用Tab Bar Application而是用Window Based的话,需要用上面方法添加一个UITabBarController作为Root View。二者效果相同,推荐直接用Tab Bar Application。

第二步,添加新的View到Tab Bar。这里有一些trick。基本上,建立xib,controller,连接等都相同,要加入Tab Bar,首先选取Tab Bar对象,在Attribute Inspector里头,添加足够数量的View Controllers,一个Controller对应一个tab。而后在界面点选每一个Tab,选择其Nib Name,并在 Identity Inspector里头选正确的Controller!!! 这步及其重要,否则添加任何Outlet都会产生SIGABRT。

August 9th, 2010  |  Posted in 数码生活  |  No Comments »

IPhone开发笔记(1)

Author: admin

Property 属性:

@property (retain, nonatomic) UILabel *statusText;

retain: 必须,不使用垃圾收集机制(不支持)

nonatomic: 如果不用多线程,可以节约开销

在.m文件中,还要添加@synthesis statustext; 一句,来自动添加对应得方法实现。如果我们retain了这个对象,那么我们应该正确的释放它,否则会泄露。应该在dealloc方法中加入

[statusText release];

Outlet & Action

每当要在Controller中修改、引用对象,采用Outlet,Action就是event. 我们在修改一个View之后,可以直接将Outlet / Action拖动到File’s Owner Object上,这个Object就代表对应的那个Class。按住 Control把File’s Owner 拖动到每个需要定义Outlet的对象上,并选择合适的Outlet,吧对应的Action拖动到File’s Owner上,并选择合适的Action.

按钮


按钮标题有四种状态,包括normal, highlighted, disabled and selected。获取titile需要用以下方法:

NSString* title = [sender titleForState:UIControlStateNormal];

字符串操作

创建新的字符串可以用initWithFormat方法,完整得参考可以看这里。

简单的例子:

NSString* newText = [[NSString alloc] initWithFormat: @”%@button pressed.”, title];

而多个变量的例子:

NSString* buf=[[NSString alloc] initWithFormat:@"The variable %s has
the value %d\n", varName, varVal];

主意的是这里Allocate完一定要release掉。

 [buf release]; 

文本字段

常用属性:

  • Placeholder 当没有值的时候,提示用户输入用的文本。
  • 更改Keyboard/ Capitalize等属性来设置弹出键盘。

关闭键盘的两种方法,一是相应Did End on Exit Action,然后调用Sender的resignFirstResponder方法。二是用一个巨大的Button放在背景,当按它的时候把所有Text Field都resign.

 显示确认对话框

分为两步,首先是如何显示对话框。下面代码是例子:

- (IBAction) doSomething: (id) sender{
    UIActionSheet *actionSheet = [[UIActionSheet alloc]
                initWithTitle:@"Are you sure"
                delegate:self
                cancelButtonTitle:@"No Way!"
                destructiveButtonTitle:@"Yes, I am sure"
                otherButtonTitles:nil];
    [actionSheet showInView:self.view];
    [actionSheet release];
}

同时,要响应用户所点击的结果,则需要首先让我们的Controller实现相应的类。比如

<br />
@interface ControlFunViewController : UIViewController <UIActionSheetDelegate> {<br />
...<br />
}<br />

而后实现对应的方法

<br />
- (void)actionSheet:(UIActionSheet *)actionSheet<br />
didDismissWithButtonIndex:(NSInteger)buttonIndex;<br />

在这些方法中,我们要取得用户点击了哪个按钮,通过buttonIndex参数来实现。例如:

- (void)actionSheet:(UIActionSheet *)actionSheet
didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if(!buttonIndex == [actionSheet cancelButtonIndex]){
        NSString *msg = nil;
        if (nameField.text.length>0) {
            msg = [[NSString alloc] initWithFormat:
                   @"You can breathe easy, %@, everything went OK.",
                   nameField.text];
        }else {
            msg = @"You can breathe easy, everything went OK.";
        }
        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle:@"Something was done" message:msg delegate:self cancelButtonTitle:@"Phew" otherButtonTitles:nil];
        [alert show];
        [alert release];
        [msg release];
    }
}

View的OnLoad事件

重载View的viewDIdLoad方法(无参数)。下面是例子:

- (void)viewDidLoad {
    UIImage *buttonImageNormal = [UIImage imageNamed:@"whiteButton.png"];
    UIImage *stretchableButtonImageNormal = [buttonImageNormal
                                             stretchableImageWithLeftCapWidth:12 topCapHeight:0];
    [doSomethingButton setBackgroundImage:stretchableButtonImageNormal
                                 forState:UIControlStateNormal];
    UIImage *blueImagePressed = [UIImage imageNamed:@"blueButton.png"];
    UIImage *stretchableButtonImagePressed = [blueImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0];
    [doSomethingButton setBackgroundImage:stretchableButtonImagePressed forState:UIControlStateHighlighted];
}
</pre>
<h3> 旋转和移动控件</h3>
<p> 旋转支持需要设置对应View的方法: </p>
<pre>[cc lang="objc"]
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

同时,发生旋转的时候会调用对应View的

button1.frame = CGRectMake(20, 20, 125, 125);

对应的,四个参数分别为左上x,y和长宽。 通过嵌套[UIView beginAnimations:@"move buttons" context:nil];…[UIView commitAnimations]; 语句,能够实现动画。

August 8th, 2010  |  Posted in 数码生活  |  No Comments »

T410安装MacOSX(非常暴力的)

Author: admin

这件事血黄血暴力了,手头只有10.6.3的安装光盘,用bootthink启动死活不行,都是直接重启。设置busratio=17,成功但是五国。不想花费太多时间于是直接用TrueImage 把整个在Vostro上安装好的分区镜像过来,启动是用busratio=17,并配上以下的kext,同时删除ATA相关Kext,启动成功。 Read the rest of this entry »

July 21st, 2010  |  Posted in 数码生活  |  No Comments »

宁静小城乌普萨拉

Author: admin

Uppsala是瑞典第四大城市……不过以中国的标准,这也就算一个县城。总人口只有13万。不过,古时候这里是瑞典天主教的中心,斯堪的纳维亚的大主教驻锡此地,而瑞典最古老的Uppsala University也诞生于此。古老和现代在这个小城中结合得非常完美,既有现代风格的火车站及站前广场,又有古朴典雅的教堂。路上看不到行色匆匆。


同时这个小城也并不显得闭塞,在路上随处可见来自世界各地的人民,除了土生土长的北欧人之外,以土耳其人最多。长达16个小时的日照使得夏日的Uppsala充满活力。瑞典这个国家已经享受了近180年的和平,作为欧洲唯二的世外桃源,也许人民早已经忘记了什么事争执了吧。


Fyris River (Fyrisån)(2)
Fyris River (Fyrisån)
后街小巷

远看Uppsala大教堂尖顶
Uppsala大学礼堂内部
Uppsala大学礼堂的穹顶

近看Uppsala大教堂尖顶
Uppsala 火车站前广场
Uppsala 火车站前广场


July 13th, 2010  |  Posted in 行走天下  |  No Comments »

欧洲真是环保……机场热得不行

Author: admin

在阿姆斯特丹机场热得汗流浃背……欧洲真是环保,空调都舍不得开。

阿姆斯特丹机场等候转机中
阿姆斯特丹机场等候转机中
阿姆斯特丹候机大厅2
阿姆斯特丹候机大厅2


Tags: 旅行
July 11th, 2010  |  Posted in 行走天下  |  No Comments »

Why Mechanical Turk will fail and how to save it

Author: admin

Mechanical Turk has been a topic that a lot of people are talking about, however having been in the Beta status for five years it has almost no improvement in the interface and business model. Having been working on it as a worker and also as a requester (alright, I go ahead and admit that I also tried to submit garbage to the requesters just to test if they can catch me, you can also call me a former scammer), I focused on observing the “society” of MTurk and in this article I am going to talk about why this (I mean, Yes, Mechanical Turk) will fail and how it can be saved. The take home message of this the article is: Crowdsourcing is an economical issue, not a technical issue, the team should focus on a better interacting schema between the Turkers, the Requesters and, finally, keep themselves out of the game.

Read the rest of this entry »

May 16th, 2010  |  Posted in Mechanical Turk  |  No Comments »

Snow Leopard on Vostro 200

Author: admin

刚刚在Vostro 200上搞定了Snow Leopard,速度很快,感觉很好。简介如下:

  1. 下载Snow Leopard零售原版光盘镜像
  2. Follow the instruction: http://macos.it168.com/viewthread.php?tid=16791
  3. 问题1:启动安装,五国。 解决方法:安装NullACPI 的kext
  4. 问题2:still waiting for root device (圆叉)。解决方法,安装IDE的kext。该死的Vostro 200 BIOS只支持IDE/RAID, no ACHI
  5. 问题3:BIOS重置,改DSDT,先follow this: http://www.pcbeta.com/viewthread-567358.html 生产 DSDT,修改方法:http://netkas.org/?p=114
  6. 解决以上三个问题,可以安装,装后三无。
  7. 无网络:使用82xx的kext(回家补上下载地址)
  8. 无声音:Follow this: http://www.insanelymac.com/forum/index.php?showtopic=188349
  9. 显卡 :(我的是NV9600 GSO)直接用 8800 GT的EFI String,QE未测试,显示正常

Update: 10.6.3

更新到10.6.3后无法启动,解决方法是,首先把ATA的Kext复制到System/Library/Extension下,启动后会提示Kext安装不正确,再用ExtHelper安装一遍,即可。

April 28th, 2010  |  Posted in 数码生活  |  No Comments »

Crowdsouring花钱比赚钱难

Author: admin

今天的感觉就是这样,想要在Mturk上发一些任务结果发现中国人大多不知道这个网站,发广告还被封,看来还是得好好推广啊。想给Amazon提意见,Mechanical Turk这个网站最大的问题就是不停向Requester宣传,却忽视了Worker方面的宣传,没有足够大,足够diverse的user base,是吸引不到更多Requester的。

只好自己写了篇关于Mturk的介绍,希望能够帮我找到足够多的人,完成标注任务

Tags: MTurk
February 21st, 2010  |  Posted in Mechanical Turk, 机器翻译  |  No Comments »

总算把ACL Paper投出去了

Author: admin

总算把Paper搞定,从整理出思路,实现,设计实验,开始写到完成,一共花了不到一个月。虽然可以说是急就章,还是可以总结不少经验:

  • 笔记必须清晰,电子化。无论多么忙,目标多么渺茫,一定要把公式、思路、实验结果及时电子化。一方面是防止丢失,更重要的是一旦结果OK,三下五除二可以弄出一篇差不多的paper,对自习心事很关键的。
  • 循序渐进,不要贪多贪全。小idea写小paper,大idea写大paper,及时写下来,发出去,有人免费提指导意见何乐而不为?一味要干到十全十美再发,不现实。

好吧,希望这次能中,感觉novelty 和 soundness都够,看看自己的文笔究竟如何吧。

Tags: 思考, 翻译, 论文
February 16th, 2010  |  Posted in 机器翻译  |  No Comments »

<< Previous

  • 分类

    • Mechanical Turk (3)
    • 数码生活 (8)
    • 机器翻译 (4)
    • 胡言乱语 (7)
    • 行走天下 (11)
  • 新帖

    • IPhone开发笔记(3) 使用PickerView
    • IPhone 开发笔记 (2)构造和添加View的一般过程
    • IPhone开发笔记(1)
    • T410安装MacOSX(非常暴力的)
    • 宁静小城乌普萨拉
  • 日历

    September 2010
    M T W T F S S
    « Aug    
     12345
    6789101112
    13141516171819
    20212223242526
    27282930  
  • 存档

  • 我的其他网站

    • 我的学术主页
    • 老婆的Blog
    • 软件发布
  • 操作

    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.org
  • 标签

    Google Linux MTurk rc 冬季 加拿大 匹兹堡 大雪 工具 思考 手持设备 旅行 时评 爱尔兰 科幻 签证 网络 美东 翻译 论文 酒吧 音乐

Copyright © 2010 - 路随人茫茫 | Entries (RSS) | Comments (RSS)

WordPress theme designed by web design