[新手学IOS]第一天:关于UIActionSheet和Alert

1.先说UIAlert:alert ,顾名思义,就是一个警告.一般出现在屏幕的中间的位置.

而且,当我们定义完这个Alert之后,我们还要来一个  show进行显示.呵呵 看代码吧.

if (buttonIndex != [actionSheet cancelButtonIndex]) {
        NSString *msg = nil;
        
        if (myNameField.text.length > 0)
            msg = [[NSString alloc]initWithFormat:@" you has press the yes!,%@,everything is ok",myNameField.text ];
        
        else
            msg = [[NSString alloc]initWithFormat:@" you has press the yes!,everything is ok"];
        
        
        UIAlertView *myAlertView = [[UIAlertView alloc]initWithTitle:@"sth is done" message:msg delegate:self cancelButtonTitle:@"quxiao" otherButtonTitles:nil];
        
        [myAlertView show];
        
    }

上述代码大致意思是: 我们首先在外部定义了一个msg,用于显示 message的信息.代理是自己(其实这点我不太明白,但是好像不影响学习,还有别的代理么?).

取消按钮的名字是"quxiao",otherButtonTitles,这项可不是说我们现在只能有一个按钮可用了.当我们 多用几个 @"",@""之后,就会发现,在取消按钮下面就跟着你输入的小分队了.

2.再说 UIActionShell  这个控件.他一般来说是从下面顶上去的. 先看看代码再解释.

- (IBAction)buttonPressed:(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];
  

和UIAlert有许多相同的地方,不同的是显示的位置不一样罢了.而且,我们还可以使用UIActionSheet的代码进行功能的扩充.


比如说1的代码 其实就是 <UIActionSheetDelegate>嘛.完整的代码如下:


-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex != [actionSheet cancelButtonIndex]) {
        NSString *msg = nil;
        
        if (myNameField.text.length > 0)
            msg = [[NSString alloc]initWithFormat:@" you has press the yes!,%@,everything is ok",myNameField.text ];
        
        else
            msg = [[NSString alloc]initWithFormat:@" you has press the yes!,everything is ok"];
        
        
        UIAlertView *myAlertView = [[UIAlertView alloc]initWithTitle:@"sth is done" message:msg delegate:self cancelButtonTitle:@"quxiao" otherButtonTitles:nil];
        
        [myAlertView show];
        
    }
}

看明白了么?

好吧,我们看看 UIActionSheetDelegate中得协议吧

@protocol UIActionSheetDelegate <NSObject>
@optional

// Called when a button is clicked. The view will be automatically dismissed after this call returns
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;

// Called when we cancel a view (eg. the user clicks the Home button). This is not called when the user clicks the cancel button.
// If not defined in the delegate, we simulate a click in the cancel button
- (void)actionSheetCancel:(UIActionSheet *)actionSheet;

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet;  // before animation and showing view
- (void)didPresentActionSheet:(UIActionSheet *)actionSheet;  // after animation

- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex; // before animation and hiding view
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex;  // after animation

@end

当我们需要的时候,才需要实现这些协议的.


由于这个苹果本不会玩,导致截图什么的都不会了.有机会补上.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值