找到你要的答案

Q:xib file button issues

Q:xib文件按钮的问题

I have a xib file which I have attached a button to it. The xib is loaded into views on the storyboard and works perfectly. All the button does at the moment is print to the console "button touched".

@IBAction func btnQuickMenuTouchUpInside(sender: UIButton) {

    println("button touched")

}

Ideally I would like a UIAlertController to pop up when I click the button as per the code below:

    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)

However, it complains that myMenuVC does not have a member named presentViewController.

Can anyone help explain how I get the UIAlertController to work from a button from a xib view?

Many thanks, Alan.

PS

Code for myMenuVC.swift as follows:

//
//  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
    }

}

我有一个XIB文件,我附上一个按钮,它。xib加载到在故事板视图和完美的作品。所有按钮此刻做的是打印到控制台“按钮触摸”。

@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。

任何人都可以帮助解释为什么我得到的uialertcontroller从xib视图按钮的工作吗?

非常感谢,艾伦。

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
    }

}
answer1: 回答1:

myMenuVC is a UIView subclass, not a UIViewController subclass.

You can either create a new view controller and re-create this UIView interface on the view controller's view.

You can also present the alert controller on the application's root view controller from the myMenuVC UIView subclass directly:

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

mymenuvc是一个UIView子类,不是一个UIViewController类。

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

你也可以现在报警控制器上的应用程序的根视图控制器从mymenuvc UIView子类直接:

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