来自 Xib 文件的 IBAction 以分离视图控制器
IBAction from Xib file to seperate View Controller
我正在创建测验应用程序,当您向右或向左滚动时它会更改问题 -
我有一个链接到 UIView 的自定义滚动视图的 xib 文件 ViewController。
但是我有另一个 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,这样 "file's owner" 就是你的视图控制器。然后从您的按钮按住 control 拖动到 "file's owner" 上以设置 IBActions。这样,当您加载 XIB 时,它们将链接到您的视图控制器。
另一种方法是在从 XIB 加载每个问题视图后,将 targets/actions 添加到按钮。
我正在创建测验应用程序,当您向右或向左滚动时它会更改问题 -
我有一个链接到 UIView 的自定义滚动视图的 xib 文件 ViewController。
但是我有另一个 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,这样 "file's owner" 就是你的视图控制器。然后从您的按钮按住 control 拖动到 "file's owner" 上以设置 IBActions。这样,当您加载 XIB 时,它们将链接到您的视图控制器。
另一种方法是在从 XIB 加载每个问题视图后,将 targets/actions 添加到按钮。