路随人茫茫

美东浮生记

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

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

  • Share this:

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。

  • Share this:

Posted in 数码生活 | No Comments »

  • 分类

    • IT Reviews (4)
    • 数码生活 (10)
    • 机器翻译 (4)
    • 胡言乱语 (22)
    • 行走天下 (12)
  • 新帖

    • Trying to parse Google’s privacy policy
    • 畅春园的传说—老彭,西门鸡翅,中国梦(5)
    • Phone OS: What they are optimized for
    • 畅春园的传说—老彭,西门鸡翅,中国梦(4)
    • 畅春园的传说—老彭,西门鸡翅,中国梦(3)
  • 日历

    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

loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.