WEB开发网
开发学院手机开发iPhone 开发 iphone开发入门教程 阅读

iphone开发入门教程

 2010-12-18 08:05:22 来源:WEB开发网   
核心提示:鼠标点选View中button,按command+2或者菜单选择"Tools | Connetions Inspector",iphone开发入门教程(2),从Event中的Touch Up Inside后面的小圆圈拉一条线连接到主窗口的File's Owner,并在弹出窗口中选择“butt

鼠标点选View中button,按command+2或者菜单选择"Tools | Connetions Inspector",从Event中的Touch Up Inside后面的小圆圈拉一条线连接到主窗口的File's Owner,并在弹出窗口中选择“buttonPressed:”。以上拖拽动作称为“连接”,将我们在类控制器中声明的IBOutlet和 IBAction连接到IB视图中控件或方法。

下来回到Xcode实现控制器类:

1// 2// HelloiPhoneViewController.m 3// HelloiPhone 4// 5// Created by Elf Sundae on 11/4/10. 6// Copyright 2010 Elf.Sundae(at)Gmail.com. All rights reserved. 7// 8 9#import "HelloiPhoneViewController.h"1011@implementation HelloiPhoneViewController1213@synthesize textField;1415- (IBAction) buttonPressed: (id)sender16{17 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello,iPhone!" 18 message:self.textField.text 19 delegate:nil 20 cancelButtonTitle:@"OK!" 21 otherButtonTitles:nil];22 [alert show];23 [alert release];24}2526- (void)didReceiveMemoryWarning {27 // Releases the view if it doesn't have a superview.28 [super didReceiveMemoryWarning];29 30 // Release any cached data, images, etc that aren't in use.31}3233- (void)viewDidUnload {34 // Release any retained subviews of the main view.35 // e.g. self.myOutlet = nil;36 self.textField = nil;37}383940- (void)dealloc {41 [textField release];42 [super dealloc];43}4445@end

运行iPhone模拟器测试后发现键盘不能自动关闭。下面来解决这个问题,让输入完后按键盘上的return即可关闭,或者触摸背景关闭键盘(数字键盘上没有return键,iphone程序常设计为触摸背景关闭键盘输入状态)。

完成输入后关闭键盘

当用户按下return键时,将生成一个Did End On Exit事件,此时我们需要让textField取消控件已关闭键盘。

用户当前正在与之交互的控件称为第一响应者(firstResponder)。

方法 - (BOOL)resignFirstResponder 使触发此操作的控件取消第一响应者状态。

我们在控制器类头文件中声明方法:

- (IBAction) textFieldDoneEditing: (id)sender;保存项目后在IB中将textField的Did End On Exit事件连接到此方法。保存IB返回Xcode实现此方法:

上一页  1 2 3 4  下一页

Tags:iphone 开发 入门教程

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接