Xcode 特定ViewController的AlertView按钮
我正在研究如何创建一个AlertView,当按下按钮“OK”时,它会将您带到一个名为DiveNumberViewController的特定视图控制器 我已经完成了AlertView代码(见下文),但不知道如何将OK按钮连接到DiveNumberViewController。感谢您的帮助 我使用的是Xcode 6.3和SwiftXcode 特定ViewController的AlertView按钮,xcode,viewcontroller,alertview,Xcode,Viewcontroller,Alertview,我正在研究如何创建一个AlertView,当按下按钮“OK”时,它会将您带到一个名为DiveNumberViewController的特定视图控制器 我已经完成了AlertView代码(见下文),但不知道如何将OK按钮连接到DiveNumberViewController。感谢您的帮助 我使用的是Xcode 6.3和Swift var Alert:UIAlertView = UIAlertView(title: "Alert", message: "Hello", delegate: self,
var Alert:UIAlertView = UIAlertView(title: "Alert", message: "Hello", delegate: self, cancelButtonTitle: "OK")
Alert.show()
您可以这样做:
var alert:UIAlertView = UIAlertView(title: "Alert", message: "Hello", delegate: self, cancelButtonTitle: "Cancel", otherButtonTitles:"OK")
alert.show()
或
不要忘记添加代理
MyController : UIViewcontroller, UIAlertViewDelegate
当用户单击按钮时,代表将启动下面的此功能。因此,在此处添加代码以转到DiveNumberViewController
//// MARK - UIAlertViewDelegate
func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
//index of cancel button
if buttonIndex == 0
{
//add code if needed
}
//index of OK button
if buttonIndex == 1
{
//add code to go to your controller
}
}
}
如果在同一个控制器中使用了大量UIAlertView,则可以像这样向每个UIAlertView添加一个标记。它将允许根据单击的UIAlertView添加特定操作
alertView.tag = 999
有关更多信息,请查看苹果的文档:
试试这个:
//// MARK - UIAlertViewDelegate
func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
//index of cancel button
if buttonIndex == 0
{
//add code if needed
}
//index of OK button
if buttonIndex == 1
{
//add code to go to your controller
var divenumberViewController = self.storyboard?.instantiateViewControllerWithIdentifier("DiveNumberViewController") as! DiveNumberViewController
self.navigationController?.pushViewController(divenumberViewController, animated: true)
}
}
此外,检查情节提要,确保在其中设置了控制器类和情节提要id
自定义类|类:DiveNumberViewController
//// MARK - UIAlertViewDelegate
func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
//index of cancel button
if buttonIndex == 0
{
//add code if needed
}
//index of OK button
if buttonIndex == 1
{
//add code to go to your controller
}
}
}
标识|情节提要ID:DiveNumberViewController
//// MARK - UIAlertViewDelegate
func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
//index of cancel button
if buttonIndex == 0
{
//add code if needed
}
//index of OK button
if buttonIndex == 1
{
//add code to go to your controller
}
}
}
示例如下:
对不起,我的评论是作为Answer@scubadivingfool对不起,我没有权利回答你的评论,所以这是我的答案。希望有帮助!复制了所有内容后,错误消失,但仍在第一个ViewController上,而不是DiveNumberViewController@Scubadivingfool您是否使用具有viewcontroller(具有UIAlertView的控制器)的UINavigationController作为根控制器?如果不是,这是正常的,它不工作,因为它是UINavigationController,允许您转到DiveNumberViewController。查看这些链接以了解其工作原理:我目前正在使用TabViewController。我将AlertViewController作为第一个选项卡,将DiveNumberViewController作为第二个选项卡。这里的代码在AlertViewViewController@Scubadivingfool好啊这里有两个解决方案:1。如果要在用户单击“确定”时转到第二个选项卡栏(具有DiveNumberViewController):请使用以下命令:“self.tabBarController?.selectedIndex=1”2。如果要管理第一个选项卡栏中的所有内容:添加UINavigationcontroller作为第一个选项卡栏,并且此UINavigationcontroller必须作为根控制器:AlertViewViewController。您将能够使用self.navigationController?.pushViewController