来自 Xib 文件的 IBAction 以分离视图控制器
Posted
技术标签:
【中文标题】来自 Xib 文件的 IBAction 以分离视图控制器【英文标题】:IBAction from Xib file to seperate View Controller 【发布时间】:2015-06-21 11:38:09 【问题描述】:我正在创建测验应用程序,当您向右或向左滚动时,它会更改问题 -
我有一个链接到 UIView ViewController 的自定义滚动视图的 xib 文件。
但是我有另一个创建滚动视图的 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?
查看控制器 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 文件
【问题讨论】:
接受的答案似乎不是答案。邓肯指的是“第二种观点的XIB”。你的第二个VC没有NIB,对吧?你有 1) 一个 NIB,2) NIB 的所有者,和 3) 另一个 VC。据我了解,问题是:一个 NIB 可以有两个不同的视图控制器的插座吗?我也有同样的问题。你知道答案吗? 【参考方案1】:设置您的第二个视图的 XIB,以便“文件的所有者”是您的视图控制器。然后从您的按钮控制拖动到“文件的所有者”以设置 IBActions。这样,当您加载 XIB 时,它们将链接到您的视图控制器。
另一种方法是在您从 XIB 加载每个 QuestionView 后将目标/操作添加到按钮。
【讨论】:
以上是关于来自 Xib 文件的 IBAction 以分离视图控制器的主要内容,如果未能解决你的问题,请参考以下文章