【IOS 开发学习总结-OC-47】★ios开发之UI控件——UIAlertView与 UIActionSheet
UIAlertView与 UIActionSheet都是弹出式对话框,它们显示出来的时候,不能与其他控件发生交。它们的区别在于:前者的弹出位置在屏幕中央,而后者显示在屏幕底部弹出。
这2个控件都只能用代码添加,无法在界面设计文件中添加。
UIAlertView——警告框
UIAlertView的基本用法
详细步骤:
1. 创建UIAlertView——创建时可指定标题,消息内容及包含的按钮等。——如果程序需要监听用户点击的是警告框中的哪个按钮,还需在创建该UIAlertView时设置 UIAlertViewDelegate 委托对象。
2. 调用方法显示UIAlertView;
3. 如果要监听点击的按钮,为委托对象实现UIAlertViewDelegate协议中的方法——- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex NS_DEPRECATED_IOS(2_0, 9_0);
。
关键代码:
这里设置了一个按钮,并为它绑定了IBAction方法。
- (IBAction)clicked:(id)sender {
// 创建UIAlertView控件
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"提示" // 指定标题
message:@"警告框用法真的很简单!" // 指定消息
delegate:self // 指定委托对象
cancelButtonTitle:@"确定" // 为底部的ion钮设置标题
// 另外设置3个按钮
otherButtonTitles:@"按钮一",@"按钮二",@"按钮三",nil];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString* msg = [NSString stringWithFormat:@"您点击了第%ld个按钮"
, (long)buttonIndex];
// 创建UIAlertView控件
UIAlertView *alert = [[UIAlertView alloc]
ini