Swift xib文件按钮问题
我有一个xib文件,上面有一个按钮。xib被加载到故事板上的视图中,可以完美地工作。此时按钮所做的一切就是在控制台上打印“按钮已触动” 理想情况下,我希望单击按钮时弹出UIAlertController,代码如下:Swift xib文件按钮问题,swift,xib,uialertcontroller,Swift,Xib,Uialertcontroller,我有一个xib文件,上面有一个按钮。xib被加载到故事板上的视图中,可以完美地工作。此时按钮所做的一切就是在控制台上打印“按钮已触动” 理想情况下,我希望单击按钮时弹出UIAlertController,代码如下: var alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.Alert) alert.addAction(UIAler
var alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
但是,它抱怨myMenuVC没有名为presentViewController的成员
有人能解释一下我是如何从xib视图中的按钮让UIAlertController工作的吗
非常感谢,艾伦
PS
myMenuVC.swift的代码如下:
//
// myMenuVC.swift
// SwiftLoginScreen
//
// Created by Alan Tingey on 29/04/2015.
// Copyright (c) 2015 Alan Tingey. All rights reserved.
//
import UIKit
@IBDesignable class myMenuVC: UIView {
var view:UIView!
@IBOutlet var titleLabel: UILabel!
@IBAction func btnQuickMenuTouchUpInside(sender: UIButton) {
println("button touched")
var alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
//self.presentViewController(alert, animated: true, completion: nil)
}
override init(frame: CGRect)
{
super.init(frame: frame)
setup()
}
required init(coder aDecoder: NSCoder)
{
super.init(coder: aDecoder)
setup()
}
func setup()
{
view = loadViewFromNib()
view.frame = bounds
// view.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight
addSubview(view)
}
func loadViewFromNib() -> UIView
{
let bundle = NSBundle(forClass:self.dynamicType)
let nib = UINib(nibName: "vwMyMenuVC", bundle: bundle)
//let nib = UINib(nibName: "myMenuVC", bundle: bundle)
let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
var myString:NSString = "straightRED"
var myMutableString = NSMutableAttributedString()
myMutableString = NSMutableAttributedString(string: myString as String, attributes: [NSFontAttributeName:UIFont(name: "Zapf Dingbats", size: 28.0)!])
myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSRange(location:8,length:3))
// set label Attribute
titleLabel.attributedText = myMutableString
//let view = nib.instantiateWithOwner(self, options: nil)[0] as UIView
return view
}
}
myMenuVC
是UIView
子类,而不是UIViewController
子类
您可以创建新的视图控制器,然后在视图控制器的视图上重新创建此UIView
界面
您还可以直接从myMenuVC
UIView
子类在应用程序的根视图控制器上显示警报控制器:
UIApplication.sharedApplication().delegate?.window??.rootViewController?.presentViewController(alert, animated: true, completion: nil)
您正在子类化
UIViewController
?我在情节提要上创建了一个视图,并将其类设置为myMenuVC.swift文件,该文件从vwmynuvc.xib文件创建视图的顶部。这叫做子类化吗?抱歉没有完全理解术语。我希望我已经正确地解释了自己的意思?因此,如果我理解正确,您应该将myMenuVC.swift
设置为故事板中加载nib的视图控制器,而不是nib文件本身。打开myMenuVC.swift
并检查它是否是子类化的UIViewController
,因为它应该是。对不起,我不确定这是什么意思。我将在问题中添加myMenuVC.swift的代码。谢谢你的耐心。
UIApplication.sharedApplication().delegate?.window??.rootViewController?.presentViewController(alert, animated: true, completion: nil)