全球化UIAlertView

时间:2013-03-11 18:24:13

标签: ios xcode

我想知道,如果我在视图controllerA中分配UIAlertView,我可以从Viewcontroller B访问它的响应吗?

e.g。

ViewController A

UIAlertView *alert;
alert = [[UIAlertView alloc] init];
[alert setTitle:@"Confirm"];
[alert setMessage:@"Do you pick Yes or No?"];
[alert setDelegate:self];
[alert addButtonWithTitle:@"Yes"];
[alert addButtonWithTitle:@"No"];
[alert show];

Viewcontroller B

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView == alert)
{

}
}

感谢您的帮助

2 个答案:

答案 0 :(得分:1)

如果将警报视图的委托设置为ViewController B,则为是。

[alert setDelegate:self];

变为

[alert setDelegate:viewControllerB];

我正在处理的代码库中的一个例子:

// Creating the view controller
AreaListController *descendentAreas = [[AreaListController alloc] init];

// Setting the delegate
[alert setDelegate:descendentAreas];

// Pushing the view controller
[self pushViewController:descendentAreas]; 

答案 1 :(得分:0)

创建一个像MyAlertView.swift

这样的swift文件
import Foundation
import UIKit
|
class MyAlertView{
class func showIt(title: String,message: String) -> UIAlertView{
let alert : UIAlertView!
alert.delegate = self
alert.title = title
alert.message = message
alert.addButtonWithTitle("Cancel")
alert.addButtonWithTitle("Done")
alert.show
return alert
}

并在任何像这样的视图控制器中访问它

MyAlertView.showIt(title: "I'm Title",message: "I'm Message").delegate = self