路随人茫茫

美东浮生记

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

Archive for the ‘数码生活’ Category

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

Monday, August 9th, 2010

构造、添加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。

Posted in 数码生活 | 1 Comment »

IPhone开发笔记(1)

Sunday, August 8th, 2010

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]; 语句,能够实现动画。

Posted in 数码生活 | No Comments »

T410安装MacOSX(非常暴力的)

Wednesday, July 21st, 2010

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

Posted in 数码生活 | No Comments »

Snow Leopard on Vostro 200

Wednesday, April 28th, 2010

刚刚在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安装一遍,即可。

Posted in 数码生活 | No Comments »

无光盘、无USB、无现有Linux系统安装Ubuntu 9.10

Thursday, January 28th, 2010

完全无光盘无现有Linux安装Ubuntu 9.10:系统IBM X60.

1. 预先分区

需要调整出足够多可未分区空间,可以用Acronis DiskDirector 或Partition Magic etc。

2. WUBI

首先,下载ISO,并且用虚拟光驱加载。运行WUBI,正常安装WUBI,重启动。

3. 启动到演示模式

重启系统后,在启动菜单选择Ubuntu,然后马上按ESC,将显示另一个菜单。将选择条移到最后一项后回车,因为中文Windows系统安装时可能会使菜单变乱码,但是肯定是最后一项,进Demo模式。实际就是启动Live CD。

4. 分区和安装

进行安装前,必须将所有的mount point都umount,否则分区无法进行。启动shell运行以下命令:

<br />
sudo umount -l -r -f /isodevice<br />
sudo umount -l -r -f /cdrom<br />

而后运行安装,选择分区等。注意不要更改Windows 分区大小,否则会导致错误,这也是为何要预留分区空间的原因。

5. 手动安装Kernel

安装后有时会出现Kernel没有安装的情况,这时GRUB界面中没有Linux选项,只有memtest和Windows。这种问题需要这样解决:

首先,重新启动进入LiveCD,同3。

而后将Root mount上,方法是点Place中xxx GB Volume。这样将会直接以guid mount到/media目录下。例如如果此分区guid为00001,将被mount到/media/00001。

在shell中运行以下命令:

cp /cdrom/casper/vmlinuz /media/00001/boot/vmlinuz-2.6.31-14-generic

注意替换其中的guid和linux kernel version。

修改

/media/00001/boot/grub/grub.cfg

添加以下条目:

### BEGIN /etc/grub.d/10_linux ###
menuentry "Ubuntu, Linux 2.6.31-14-generic" {
        recordfail=1
        if [ -n ${have_grubenv} ]; then save_env recordfail; fi
        set quiet=1
        insmod ext2
        set root=(hd0,5)
        search --no-floppy --fs-uuid --set 00001
        linux   /boot/vmlinuz-2.6.31-14-generic root=UUID=00001 ro   quiet splash
        initrd  /boot/initrd.img-2.6.31-14-generic
}

注意替换root磁盘分区和guid

保存,重启。

重启后注意update,安装Kernel更新。

A. 音量OSD

X60不显示OSD,解决问题需要运行如下命令:

sudo cp /sys/devices/platform/thinkpad_acpi/hotkey_all_mask /sys/devices/platform/thinkpad_acpi/hotkey_mask

B. UltraNav

新建文件

sudo vi /etc/hal/fdi/policy/mouse-wheel.fdi

文件内容为:

<match key="info.product" string="TPPS/2 IBM TrackPoint">
    <merge key="input.x11_options.EmulateWheel" type="string">true</merge>
    <merge key="input.x11_options.EmulateWheelButton" type="string">2</merge>
    <merge key="input.x11_options.XAxisMapping" type="string">6 7</merge>
    <merge key="input.x11_options.YAxisMapping" type="string">4 5</merge>
    <merge key="input.x11_options.ZAxisMapping" type="string">4 5</merge>
    <merge key="input.x11_options.Emulate3Buttons" type="string">true</merge>
</match>

按CTRL+ALT+F1进入shell,运行

sudo rm /var/cache/hald/fdi-cache
sudo /etc/init.d/hal restart
sudo /etc/init.d/gdm restart

C. Eclipse按钮问题

在/etc/environment添加:

export GDK_NATIVE_WINDOWS=true

Tags: Linux, 工具
Posted in 数码生活 | No Comments »

搞定混合word alignment程序

Wednesday, January 20th, 2010

折腾一天总算完成了混合词对齐程序。这个方法可以将部分人工对齐引入自动对齐框架中,提高准确率。明天进一步改进,主要解决多个目标词对齐一个源词的问题。初步思路是允许这两个词链接中任意一个被选中,对此数据结构要修改了。this will be big.

Tags: 翻译
Posted in 数码生活, 机器翻译 | No Comments »

RC 系列:screen.rc

Sunday, January 17th, 2010

Screen真的是无法离开的工具啊,现在我用的screen.rc 如下:

startup_message off
vbell off
#escape /
defscrollback 5000
#backtick 1 60 60 $HOME/.screenrc.acpi  # .screenrc.acpi contains 1 line: acpi | awk -F ', ' '{print $2}'
hardstatus alwayslastline
hardstatus string '%{= kG}%-Lw%{= kW}%50&gt; %n*%f %t%{= kG}%+Lw%&lt; %{= kG}%-=%D %m/%d/%y | %C:%s %A |`%{-}'
shell /bin/bash
screen -t first 0 /bin/bash
select 0
#/afs/cs.cmu.edu/user/qing/.screenrc: two characters required after escape.
bind j focus down
bind k focus up
bind h focus left
bind l focus right
bind - resize -1
bind = resize +1
bind d detach
nonblock on
flow off




Screen Screen shot...

Tags: Linux, rc, 工具
Posted in 数码生活 | No Comments »

重装N800

Sunday, January 17th, 2010

N800自从apt坏掉以后就基本处于闲置状态,今天新电池运到,整了一下还发现不少好东西,比如Tear浏览器,用的是Webkit, 速度很快,配合Browser Switchboard关掉browserd进程后更是飞快。还有Macuco,为Google设计的浏览器,用来看reader非常好用。还有Wordpy,写wordpress很方便。没有实体键盘还是不爽啊,写这么些就够受了,over.

Tags: 手持设备
Posted in 数码生活 | No Comments »

  • 分类

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

    • 微软研究院实习第一天
    • 最让我感动的动画短片One minute fly
    • 图表的艺术……
    • 同音文章
    • IPhone开发笔记(3) 使用PickerView
  • 日历

    February 2012
    M T W T F S S
    « Apr    
     12345
    6789101112
    13141516171819
    20212223242526
    272829  
  • 存档

  • 我的其他网站

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

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

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

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

WordPress theme designed by web design