我在执行segue动作时遇到问题。我正在使用InstructionView设置对象,并在PageViewController类中调用它来创建新的viewController。
我可以在哪里设置动作,并执行表演。如果我在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
}
}
最佳答案
是的,我认为代表可能是最好的选择。在您的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
以常规方式将相关数据传递到目标视图控制器。关于ios - 在其他UIPageViewController中执行segue,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27215182/