IBAction from Xib file to seperate View Controller

2.7k Views Asked by At

I am creating quiz application and when you scroll right or left it changes the question -

I have a xib file for my custom scroll view that is linked to a UIView ViewController.

However I have another ViewController which creates the scrollView. How do I get an action from my XIB to connect to ViewController 2.

I want QuizViewController to be able to have @IBActions from the XIB file so when the user press the button something such as this for example can run(obviously ignoring the variables):

@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!)

    }
}

Is this possible or do I need to use anther method - something like NScoder?

View Controller 1 - xib file connected to this

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!

}

View Controller 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.
}

}

My xib File

1

There are 1 best solutions below

0
On BEST ANSWER

Set up your second view's XIB so "file's owner" is your view controller. Then control-drag from your buttons onto "file's owner" to set up the IBActions. That way they will be linked up to your view controller when you load the XIB.

The alternative is to add targets/actions to the buttons in each QuestionView after you load it from the XIB.