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实现相应的类。比如
@interface ControlFunViewController : UIViewController <UIActionSheetDelegate> {<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]; 语句,能够实现动画。