我遇到了执行segue操作的问题。我正在使用InstructionView来设置对象,并在PageViewController类中调用它来创建新的viewController。
在哪里可以使用perform segue设置动作。如果我在InstructionView中执行它会崩溃。我把它嵌入了NavigationController。 我想我应该制作一个委托方法,但我不知道在这种情况下如何。
请参阅下面的代码:
protocol InstructionViewDelegate
{
func didTapLabel(myString: NSString)
}
class InstructionView: UIViewController
{
var delegate : InstructionViewDelegate!
var pageIndex : Int = 0
var titleText : String = ""
var otherImageFile : String = ""
var tapLabel = UITapGestureRecognizer()
override func viewDidLoad()
{
....
var label = UILabel(frame: CGRectMake(16, 825, 736, 104))
....
view.addSubview(label)
var imageViewObject : UIImageView
....
view.addSubview(imageViewObject)
tapLabel = UITapGestureRecognizer(target: self, action: "imageLabelAction:")
tapLabel.numberOfTapsRequired = 1
label.addGestureRecognizer(tapLabel)
label.userInteractionEnabled = true
imageViewObject.addGestureRecognizer(tapLabel)
imageViewObject.userInteractionEnabled = true
}
func imageLabelAction(sender: AnyObject)
{
delegate.didTapLabel("The image was tapped")
}
PageViewController:
class PageViewController: UIViewController, UIPageViewControllerDataSource, InstructionViewDelegate
{
var pageViewController : UIPageViewController?
var pageTitles : Array<String> = ["", "", ""]
var otherImages : Array<String> = ["", "", ""]
var currentIndex : Int = 0
override func viewDidLoad()
{
super.viewDidLoad()
pageViewController = UIPageViewController(transitionStyle: .Scroll, navigationOrientation: .Horizontal, options: nil)
pageViewController!.dataSource = self
let startingViewController: InstructionView = viewControllerAtIndex(0)!
let viewControllers: NSArray = [startingViewController]
pageViewController!.setViewControllers(viewControllers, direction: .Forward, animated: false, completion: nil)
pageViewController!.view.frame = CGRectMake(0, 0, view.frame.size.width, view.frame.size.height);
addChildViewController(pageViewController!)
view.addSubview(pageViewController!.view)
pageViewController!.didMoveToParentViewController(self)
}
func didTapLabel(myString: NSString) {
println(myString)
self.performSegueWithIdentifier("testSegue", sender: self)
}
.....
func viewControllerAtIndex(index: Int) -> InstructionView?
{
if self.pageTitles.count == 0 || index >= self.pageTitles.count{
return nil
}
let pageContentViewController = InstructionView()
pageContentViewController.otherImageFile = otherImages[index]
pageContentViewController.titleText = pageTitles[index]
pageContentViewController.pageIndex = index
pageContentViewController.delegate = self
currentIndex = index
return pageContentViewController
}
}
答案 0 :(得分:2)
是的,我认为代表可能是最好的路线。在InstructionView
Swift文件中,定义一个协议,描述从InstructionView到其委托的通信:
protocol InstructionViewDelegate {
func didTapLabel(myString : String)
}
我假设你将一个字符串值从InstructionView传递给委托;修改此项以反映您实际想要传递的数据。将delegate
var添加到InstructionView
:
var delegate : InstructionViewDelegate!
还可以按以下方式添加imageLabelAction
函数:
func imageLabelAction(sender: AnyObject) {
// perform any actions here in response to the label being tapped, and finish with:
delegate.didTapLabel("The image label was tapped")
// (amend the argument to what you need to send to your TestViewController)
}
修改PageViewController
的类定义,表明它采用了InstructionViewDelegate协议:
class PageViewController: UIViewController, UIPageViewControllerDataSource, InstructionViewDelegate {
请记住将InstructionView的委托设置为指向自己:
startingViewController.delegate = self
然后实现协议定义的函数:
func didTapLabel(myString: String) {
println(myString)
// do any processing here (e.g. save myString to a local var)
// before triggering the segue:
self.performSegueWithIdentifier("FromPageVCtoTestVC", sender: self)
}
然后,您可以使用prepareForSegue
以通常的方式将相关数据传递到目标视图控制器。