从Xib文件到单独的View Controller的IBAction

sim*_*sd3 1 action xib swift

我正在创建测验应用程序,当您向右或向左滚动时,它会更改问题-

我的自定义滚动视图有一个xib文件,该文件链接到UIView ViewController。

但是,我还有另一个ViewController可以创建scrollView。如何从XIB中获取一个动作以连接到ViewController 2。

我希望QuizViewController能够从XIB文件中获取@IBActions,以便在用户按下按钮时可以运行诸如此类的命令(显然忽略变量):

@IBAction func answerPressed(sender: UIButton) {

    if allAnswersCompleted == true {
        answerChosen = 1
        answerSelected = sender.currentTitle
        println(answerSelected!)
        lockInButton.setTitle("Lock In", forState: UIControlState.Normal)
    } else {
        answerChosen = 1
        answerSelected = sender.currentTitle
        println(answerSelected!)

    }
}
Run Code Online (Sandbox Code Playgroud)

这有可能还是我需要使用另一种方法-类似于NScoder?

View Controller 1-连接到此的xib文件

class QuestionView: UIView {


@IBOutlet weak var view: UIView!
@IBOutlet weak var falseButton: UIButton!
@IBOutlet weak var trueButton: UIButton!
@IBOutlet weak var questionText: UITextView!
@IBOutlet weak var lockInButton: UIButton!

}
Run Code Online (Sandbox Code Playgroud)

视图控制器2

class QuizViewController: UIViewController {

@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var contentView: UIView!
var questionViews : NSMutableArray = [];
var numberOfQuestions = 4;

override func viewDidLoad() {


    // UI CONSTRAINTS AND VIEW GENERATION

    //Example of using 3 questions
    var scrollView = self.scrollView;
    scrollView.setTranslatesAutoresizingMaskIntoConstraints(false);
    self.view.setTranslatesAutoresizingMaskIntoConstraints(false);
    self.contentView.setTranslatesAutoresizingMaskIntoConstraints(false);


    //Constraints
    var constraints : NSArray;
    var currentQuestionIndex = 0



    for (var i : Int = 0; i < numberOfQuestions ;i++)
    {

        //Construct the view by using the Template XIB file
        var array : NSArray = NSBundle.mainBundle().loadNibNamed("QuestionView", owner: self, options: nil);
        var view : QuestionView = array.objectAtIndex(0) as! QuestionView

        // Set the scroll view to global variable

        scrollViewQuiz = view

        questionViews.addObject(view);
        view.setTranslatesAutoresizingMaskIntoConstraints(false);
        view.backgroundColor = UIColor.clearColor();


        //Add to the scrollView
        scrollView.addSubview(view);
        //Add the top Constraints, they need to fit the superview
        let views : NSDictionary = ["view" : view,"scrollView" : scrollView];
        let constraints : NSArray = NSLayoutConstraint.constraintsWithVisualFormat("V:|[view(==scrollView)]|", options: NSLayoutFormatOptions.allZeros, metrics: nil, views: views as [NSObject : AnyObject]);
        scrollView.addConstraints(constraints as! [AnyObject]);

    }
    contentView.backgroundColor = UIColor.clearColor();


    var viewsDictionary : NSMutableDictionary = NSMutableDictionary(dictionary: ["scrollView" : scrollView]);
    var visualFormat : NSMutableString = ("H:|").mutableCopy() as! NSMutableString;



    //With all the views created, create the Layout Constraints for the horizontal logic
    for (var i : Int = 0; i < numberOfQuestions; i++)

    {
        viewsDictionary.setValue(self.questionViews[i], forKey: NSString(format: "view%d", i) as String);
        visualFormat.appendFormat("[view%d(==scrollView)]", i);
    }

    visualFormat.appendString("|");

    constraints = NSLayoutConstraint.constraintsWithVisualFormat(visualFormat as String, options: NSLayoutFormatOptions.allZeros, metrics: nil, views: viewsDictionary as [NSObject : AnyObject]);
    scrollView.addConstraints(constraints as! [AnyObject]);



    //Add the constraint for the contentView
    //constraints = NSLayoutConstraint.constraintsWithVisualFormat("H:|[contentView]|", options: NSLayoutFormatOptions.allZeros, metrics: nil, views: ["contentView" : contentView]);
    //scrollView.addConstraints(constraints as! [AnyObject]);
    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
Run Code Online (Sandbox Code Playgroud)

}

我的xib文件

Dun*_*n C 7

设置第二个视图的XIB,以便“文件的所有者”是您的视图控制器。然后将您的按钮拖到“文件的所有者”上,以设置IBAction。这样,当您加载XIB时,它们将被链接到您的视图控制器。

另一种方法是在从XIB加载后,将目标/操作添加到每个QuestionView中的按钮。