按下后退按钮执行转场 [英] Perform segue on press on Back button

查看:41
本文介绍了按下后退按钮执行转场的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在单击返回"按钮时执行转场.为此,我这样做:

I want to perform segue when I click on Back button. For it I do:

override func didMoveToParentViewController(parent: UIViewController?) {
    self.performSegueWithIdentifier("fromGalleryToBody", sender: self)
}

但是当我点击返回"按钮时它使我的应用程序崩溃.我怎样才能意识到它?

but it crashes my app when I tap on Back button. How can I realize it?

推荐答案

导航的默认按钮为您提供移动到上一个视图的功能

Default button of navigation gives you functionality to move to the previous view

您可以通过以下方式检查:

You can check it by :

override func didMoveToParentViewController(parent: UIViewController?) {
    if parent == nil {
        println("Back Pressed")
    }
}

但是情况是 didMoveToParentViewController 意味着它已经移动到上一个视图.

But the situation is didMoveToParentViewController means it already move to the previous view.

您可以在导航栏中添加自定义按钮作为后退按钮.那么你的问题就迎刃而解了.

You can add custom button as back button in your navigation bar. Then your issue will be solved.

以编程方式添加按钮:

let backButton = UIBarButtonItem(title: "Назад в будущее", style: .Plain, target: self, action: "toMainFromGallery")
self.navigationItem.leftBarButtonItem = backButton

功能:

func toMainFromGallery {
}

<小时>

带有后退图标的按钮:

var backButton = UIButton(frame: CGRectMake(0, 0, 70.0, 70.0))
var backImage = UIImage(named: "backBtn")
backButton.setImage(backImage, forState: UIControlState.Normal)
backButton.titleEdgeInsets = UIEdgeInsetsMake(10.0, 10.0, 10.0, 0.0)
backButton.setTitle("Back", forState: UIControlState.Normal)
backButton.addTarget(self, action: "buttonPressed", forControlEvents: UIControlEvents.TouchUpInside)
var backBarButton = UIBarButtonItem(customView: backButton)
self.navigationItem.leftBarButtonItem = backBarButton

左边之间应该有空格如果你想删除它添加空格,如:

var spacer = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FixedSpace, target: nil, action: nil)
spacer.width = -15

self.navigationItem.leftBarButtonItems = [spacer,backBarButton]

背面图标图像应分别为 22px、44px 和 66px = @1x、@2x 和 3x

这篇关于按下后退按钮执行转场的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆