路随人茫茫

美东浮生记

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

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

This entry was posted on Sunday, August 8th, 2010 at 1:13 pm and is filed under 数码生活. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply

*

  • 分类

    • 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