【iPhone代码片段之一】UIActionSheet&UIAlertView的应用 .

类需要符合<UIActionViewDelegate>协议。

示例代码如下:

- (IBAction)buttonPressed:(id)sender {
    NSLog(@"buttonPressed");
    UIActionSheet *actionSheet = [[UIActionSheet alloc]
                                  initWithTitle:@"您要查看进度情况吗?" 
                                  delegate:self//操作表的代理,按钮被按下时收到通知,然后回调协议中的相关方法
                                  cancelButtonTitle:@"取消"
                                  destructiveButtonTitle:@"确定"
                                  otherButtonTitles:nil];
    [actionSheet showInView:self.view];
}

-(void)actionSheet:(UIActionSheet *)actionSheet 
                    didDismissWithButtonIndex:(NSInteger)buttonIndex{
    NSString *msg = nil;
    UIAlertView *alert = nil;
    if(buttonIndex == [actionSheet destructiveButtonIndex]){
        if(nameField.text.length>0){
            msg = [[NSString alloc] initWithFormat:@"您歇会儿吧,%@,一切正常!",nameField.text];
        }else{
            msg = @"您歇会儿吧,一切正常!";
        }
        alert = [[UIAlertView alloc] initWithTitle:@"搞定了"
                                                        message:msg
                                                       delegate:nil//不需要处理按钮按下事件,将委托设置为空
                                              cancelButtonTitle:@"我知道了"
                                              otherButtonTitles: nil];
    }else if(buttonIndex==[actionSheet cancelButtonIndex]){
        msg = @"呜呜,没搞定呢,还在加班呢!";
        alert = [[UIAlertView alloc] initWithTitle:@"没搞定"
                                                        message:msg
                                                       delegate:nil
                                              cancelButtonTitle:@"靠,继续搞吧!"
                                              otherButtonTitles: nil];
    }
    [alert show];
    [alert release];
    [msg release];
}

猜你喜欢

转载自android-zhang.iteye.com/blog/1758594
今日推荐