路随人茫茫

美东浮生记

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

Archive for August, 2010

IPhone开发笔记(3) 使用PickerView

Monday, August 9th, 2010

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 可以是字符串,同时也是变量名称。很动态哦。

Posted in 胡言乱语 | No Comments »

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 »

  • 分类

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

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

    August 2010
    M T W T F S S
    « Jul   Dec »
     1
    2345678
    9101112131415
    16171819202122
    23242526272829
    3031  
  • 存档

  • 我的其他网站

    • 我的学术主页
    • 老婆的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