iphone – 使UIAlertView按钮触发功能按下
发布时间:2020-12-14 20:04:10 所属栏目:百科 来源:网络整理
导读:目前我正在使用以下代码来呈现UIAlertView: UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Today's Entry Complete" message:@"Press OK to submit your data!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil]; [alert show]
目前我正在使用以下代码来呈现UIAlertView:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Today's Entry Complete" message:@"Press OK to submit your data!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil]; [alert show]; [alert release]; 如何得到它,以便当按下“OK”时,它会触发一个函数,例如 – (void)submitData 解决方法
注意:
重要提示:UIAlertView在iOS 8中已被弃用.(请注意,UIAlertViewDelegate也已被弃用.)要在iOS 8及更高版本中创建和管理警报,请改用UIAlertController与优先级的UIAlertControllerStyleAlertStyle. Please check this out tutorial “deprecated” means???
.h文件 @interface urViewController : UIViewController <UIAlertViewDelegate> { .m文件 // Create Alert and set the delegate to listen events UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Today's Entry Complete" message:@"Press OK to submit your data!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK",nil]; // Set the tag to alert unique among the other alerts. // So that you can find out later,which alert we are handling alert.tag = 100; [alert show]; //[alert release]; -(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ // Is this my Alert View? if (alertView.tag == 100) { //Yes // You need to compare 'buttonIndex' & 0 to other value(1,2,3) if u have more buttons. // Then u can check which button was pressed. if (buttonIndex == 0) {// 1st Other Button [self submitData]; } else if (buttonIndex == 1) {// 2nd Other Button } } else { //No // Other Alert View } }
Swifty的方法是使用新的UIAlertController和closure: // Create the alert controller let alertController = UIAlertController(title: "Title",message: "Message",preferredStyle: .Alert) // Create the actions let okAction = UIAlertAction(title: "OK",style: UIAlertActionStyle.Default) { UIAlertAction in NSLog("OK Pressed") } let cancelAction = UIAlertAction(title: "Cancel",style: UIAlertActionStyle.Cancel) { UIAlertAction in NSLog("Cancel Pressed") } // Add the actions alertController.addAction(okAction) alertController.addAction(cancelAction) // Present the controller self.presentViewController(alertController,animated: true,completion: nil) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |