在swift中推送到新视图控制器的方法

时间:2016-05-30 22:14:31

标签: ios xcode swift

我已经制作了带有按钮的旋转木马,这应该会导致其他视图控制器。

但是,我无法按住Ctrl键并将按钮拖动到新视图控制器上以推送它们,因为轮播视图不在开始视图控制器中。

下面的驱动链接中添加了一些图片: Pictures

这是为旋转木马和旋转木马功能本身添加2个按钮的代码:

func carousel(carousel: iCarousel, viewForItemAtIndex index: Int, reusingView view: UIView?) -> UIView {
    let tempView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200)) //Adding the buttons

    if index == 0 {
        let actua = UIButton(frame: CGRect(x: 0, y: 0, width: 200, height: 200))

        actua.setTitle("Actua", forState: .Normal)
        actua.setTitleColor(UIColor.blackColor(), forState: .Normal)

        let HeistLeeftFoto = UIImage.init(named: "HeistLeeft")
        actua.setBackgroundImage(HeistLeeftFoto!, forState: .Normal)
        tempView.addSubview(actua)
    }

    else if index == 1 {
        let shoppen = UIButton(frame: CGRect(x: 0, y: 0, width: 200, height: 200))

        shoppen.setTitle("Shoppen", forState: .Normal)
        shoppen.setTitleColor(UIColor.whiteColor(), forState: .Normal)

        let BergStraatFoto = UIImage.init(named: "Bergstraat")
        shoppen.setBackgroundImage(BergStraatFoto!, forState: .Normal)
        tempView.addSubview(shoppen)
    }

在之前的问题中,有人告诉我添加以下操作:

actua.addTarget(self, action: ViewController.pushActua, forControlEvents: UIControlEvents.TouchUpInside)

然后他建议我编写一个方法将其推送到ViewControllerActua视图。有没有人知道如何编写此方法或如何以另一种方式执行此操作?提前谢谢!

1 个答案:

答案 0 :(得分:0)

我还创建了这种类型的视图并推送我使用了以下代码。

这是方法

func pushRegisterView()
{
    let viewControllerObj = self.storyboard?.instantiateViewControllerWithIdentifier("StoryboardID") as? ViewController // To which you want to go
    self.navigationController?.pushViewController(viewControllerObj!, animated: true)
}

以下是添加到按钮

的代码
func carousel(carousel: iCarousel, viewForItemAtIndex index: Int, reusingView view: UIView?) -> UIView {
let tempView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200)) //Adding the buttons

if index == 0 {
    let actua = UIButton(frame: CGRect(x: 0, y: 0, width: 200, height: 200))

    actua.setTitle("Actua", forState: .Normal)
    actua.setTitleColor(UIColor.blackColor(), forState: .Normal)
    actua.addTarget(self, action: #selector(ViewController.pushRegisterView),
                                 forControlEvents: .TouchUpInside)
    let HeistLeeftFoto = UIImage.init(named: "HeistLeeft")
    actua.setBackgroundImage(HeistLeeftFoto!, forState: .Normal)
    tempView.addSubview(actua)



}

else if index == 1 {
    let shoppen = UIButton(frame: CGRect(x: 0, y: 0, width: 200, height: 200))

    shoppen.setTitle("Shoppen", forState: .Normal)
    shoppen.setTitleColor(UIColor.whiteColor(), forState: .Normal)
    shoppen.addTarget(self, action: #selector(ViewController.pushRegisterView),
                                 forControlEvents: .TouchUpInside)
    let BergStraatFoto = UIImage.init(named: "Bergstraat")
    shoppen.setBackgroundImage(BergStraatFoto!, forState: .Normal)
    tempView.addSubview(shoppen)
}

如果你想要

,这是翻转动画和pop的代码
    // Method used for flipping the view
func flipanimationblock ()
{
    let screen = TypeTwoNib.instanceFromNib()
    UIView.transitionWithView(self.carousel.itemViewAtIndex(index)!, duration: 0.5, options: UIViewAnimationOptions.TransitionFlipFromRight, animations:
        { () -> Void in
            screen.bttn_First.addTarget(self, action: #selector(ViewController.popbackToIntialViewController),
                forControlEvents: .TouchUpInside)
            screen.bttn_First.setTitle("OK!", forState: .Normal)
            self.items.replaceObjectAtIndex(self.index, withObject: screen)

    }) { (Bool) in
        print(screen.frame)
        self.carousel.reloadItemAtIndex(self.index, animated: true)

    }
 }

    func popbackToIntialViewController()
{
    let transition:CATransition = CATransition()
    transition.duration = 0.5
    transition.subtype = kCATransitionFromBottom
    self.navigationController!.view.layer.addAnimation(transition, forKey: kCATransition)
    self.navigationController?.popViewControllerAnimated(false)
}