iOS界面交互与自动旋转开发指南
立即解锁
发布时间: 2025-08-24 01:05:32 阅读量: 1 订阅数: 7 


iOS 5开发入门:探索iOS SDK核心概念与实践
### iOS 界面交互与自动旋转开发指南
#### 1. 模拟用户请求处理与提示框使用
在实际开发中,当用户发起请求时,我们需要进行相应的处理。这里我们只是模拟处理,并通过提示框通知用户。若用户在顶部文本框输入了姓名,我们会获取该姓名并在提示框消息中使用;若未输入,则显示通用消息。以下是实现代码:
```objc
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:nil
cancelButtonTitle:@"Phew!"
otherButtonTitles:nil];
[alert show];
// 内存清理部分代码省略,保存文件后构建、运行应用
```
提示框和操作表的创建和使用方式类似。提示框不像操作表那样与特定视图绑定,所以直接调用 `show` 方法显示即可。若需要知道用户何时关闭提示框或点击了哪个按钮,可以指定 `self` 为代理,并遵循 `UIAlertViewDelegate` 协议,实现相应方法。
#### 2. 优化按钮外观
默认的圆角矩形按钮外观不够美观,我们可以使用图片来优化按钮样式。iOS 设备上的大多数按钮都是通过图片绘制的,我们只需指定模板图片,iOS 会在绘制按钮时使用。
获取模板图片的途径有两种:
- 从 iOS 开发者库中的 iPhone 示例应用 UICatalog 中获取:[http://developer.apple.com/library/ios/#samplecode/UICatalog/index.html](http://developer.apple.com/library/ios/#samplecode/UICatalog/index.html)
- 从相关项目存档的 04 - Control Fun 文件夹中复制 `blueButton.png` 和 `whiteButton.png` 图片。
将这两张图片添加到 Xcode 项目后,按以下步骤操作:
1. 回到正在编辑的 nib 文件,单击 “Do Something” 按钮(即使按钮已隐藏,也能看到虚影,也可在停靠栏列表中点击)。
2. 按下 `Command + 4` 打开属性检查器,使用第一个弹出菜单将按钮类型从 “Rounded Rect” 改为 “Custom”。
接着,我们使用 `viewDidLoad` 方法进一步设置按钮背景图片:
```objc
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImage *buttonImageNormal = [UIImage imageNamed:@"whiteButton.png"];
UIImage *stretchableButtonImageNormal = [buttonImageNormal
stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[doSomethingButton setBackgroundImage:stretchableButtonImageNormal
forState:UIControlStateNormal];
UIImage *buttonImagePressed = [UIImage imageNamed:@"blueButton.png"];
UIImage *stretchableButtonImagePressed = [buttonImagePressed
stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[doSomethingButton setBackgroundImage:stretchableButtonImageP
```
0
0
复制全文
相关推荐










