xib 文件按钮问题

xib file button issues

我有一个 xib 文件,我在其中附加了一个按钮。 xib 已加载到故事板上的视图中,并且可以完美运行。目前所有按钮所做的都是打印到控制台 "button touched".

@IBAction func btnQuickMenuTouchUpInside(sender: UIButton) {

    println("button touched")

}

理想情况下,当我按照以下代码单击按钮时,我希望弹出一个 UIAlertController:

    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
    }

}

myMenuVCUIView 子类,而不是 UIViewController 子类。

您可以创建一个新的视图控制器并在视图控制器的视图上重新创建此 UIView 界面。

您还可以直接从 myMenuVC UIView 子类在应用程序的根视图控制器上显示警报控制器:

UIApplication.sharedApplication().delegate?.window??.rootViewController?.presentViewController(alert, animated: true, completion: nil)