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

时间:2015-06-21 11:38:09

标签: swift action xib

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

我的自定义滚动视图的xib文件链接到UIView ViewController。

但是我有另一个创建scrollView的ViewController。如何从我的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!)

    }
}

这是可能的还是我需要使用其他方法 - 像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!

}

查看控制器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.
}

}

我的xib文件

1 个答案:

答案 0 :(得分:6)

设置第二个视图的XIB,以便&#34;文件的所有者&#34;是你的视图控制器。然后从您的按钮控制拖动到&#34;文件的所有者&#34;建立IBActions。这样,当您加载XIB时,它们将链接到您的视图控制器。

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