AlertView

本文介绍如何在iOS应用中使用UIAlertView并实现其委托模式。通过具体代码示例,展示了如何响应UIAlertView关闭事件,并调用相应的更新和下一轮开始方法。文章还解释了如何在视图控制器中声明自身为UIAlertView的代理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

- (IBAction)showAlert
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Hello,World"
                                                        message:@"This is my first app!"
                                                       delegate:nil
                                              cancelButtonTitle:@"Awesome"
                                              otherButtonTitles:nil];
    [alertView show];
}







start the next round after we click OK pp104


We can ask the alert view to send us a message when it is being closed. In the mean time, we simply call Update and nextRound methods. when the user finally taps the OK button on the alert view, the alert will remove itself from the screen and send us that message.

This is also known as the listener pattern or observer pattern. Our view controller listens to events coming from the alert view. In proper iOS terminology such listeners are named delegates and that's the term we'll be using.


in .h

@interface BullsEyeViewController : UIViewController <UIAlertViewDelegate>

This tells the app that our view controller is now a delegate of UIAlertView


//This is the delegate method that is called by the alert view when the user closes it.

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex

{

    [self startNewRound];

    [self updateLables];

}



Delegate,   declare the conformance in interface not implementation


Delegates in two steps

1 You declare yourself capable of being a delegate. (conform to some protocol)

1 You let the object in question, in this case the UIAlertView, know that you wish to become its delegate. (set   delegate:self   i.e. in controller)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值