按钮单击会崩溃整个应用程序

时间:2015-06-15 12:51:52

标签: ios swift uiview uibutton addtarget

我目前正在构建应用程序的UI而不使用IB或故事板。我有以下层次结构:  1. ViewController:主视图控制器。  2. DrawerViewController:下抽屉菜单的视图控制器。    主视图控制器将drawerViewController的视图添加为子视图。  3.添加按钮:这是一个UIButton,它以编程方式添加到drawerViewController的视图中。

问题是如果我点击添加按钮,应用程序崩溃,输出中绝对没有错误消息,除了(lldb)。

我尝试了以下内容:

  1. 更改选择器名称和方法名称。
  2. 选中选择器方法的名称。
  3. 添加和删除了选择器方法的参数。
  4. 更改了按钮的名称。
  5. 在另一个视图中添加了按钮。
  6. 无法使用断点跟踪错误,因为应用程序编译得很好,只有在您单击按钮时应用程序才会崩溃。

    这是我创建的用于显示问题的测试项目的链接:

    GitHub

    的ViewController:

    class ViewController: UIViewController //, DrawerDelegate
    {
    
    //Lower Drawer.
    var drawerView: DrawerView!
    
    var drawerViewHidden: Bool = false
    var addButton: UIButton!
    var viewHeight: CGFloat! = 0
    var viewWidth : CGFloat! = 0
    var shownDrawerViewY: CGFloat!
    var hiddenDrawerViewY: CGFloat!
    
    init()
    {
        super.init(nibName: nil, bundle: nil)
    }
    
    required init(coder aDecoder: NSCoder)
    {
        super.init(coder: aDecoder)
    }
    
    
    
    override func viewDidLoad()
    {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        //Setup UI
        setUpUI()
    }
    
    override func viewWillAppear(animated: Bool)
    {
        super.viewWillAppear(animated);
    
    
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    //MARK:- UI initialization.
    /**
    Private function that setsup the UI.
    */
    private func setUpUI()
    {
        setUpDrawer()
    }
    
    //MARK: Drawer setup.
    
    /**
    A private function that programatically creataes a drawer and add it to the view.
    */
    private func setUpDrawer()
    {
        var controller = DrawerViewController(width: self.view.frame.width, height: self.view.frame.height)
    
        self.view.addSubview(controller.view)
        //Hide
        // toggleDrawer()
    }
    
    
    
    //MARK:- Delegates.
    
    //MARK: DrawerDelegate methods.
    func addButtonClicked(sender: UIButton)
    {
        //  toggleDrawer()
    }   
    }
    

    DrawerViewController

    class DrawerViewController: UIViewController
    {
        //Drawer variables.
        var drawerViewHidden: Bool = false
        var addPinDrawer: AddPinDrawer!
    
    
    
        var viewHeight: CGFloat! = 0
        var viewWidth : CGFloat! = 0
        var shownDrawerViewY: CGFloat?
        var hiddenDrawerViewY: CGFloat?
    
        var addButton: UIButton?
    
        init(width: CGFloat, height: CGFloat)
        {
            super.init(nibName: nil, bundle: nil)
    
            viewWidth  = width
            viewHeight = height
    
            setUpUI()
        }
    
        override func viewDidLoad()
        {
            super.viewDidLoad()
    
            // Do any additional setup after loading the view.
    
        }
    
        override func viewWillAppear(animated: Bool)
        {
            super.viewWillAppear(animated)
    
    
        }
    
        override func didReceiveMemoryWarning()
        {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
    
        required override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?)
        {
            super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        }
    
        required init(coder aDecoder: NSCoder)
        {
            fatalError("init(coder:) has not been implemented")
        }
    
    
        //MARK:- UI setup
         func setUpUI()
        {
            setUpDimensions()
            setUpDrawerElements()
    //        toggleDrawer()
        }
    
         func setUpDimensions()
        {
            //Determine the y in the case drawer is shown
            shownDrawerViewY  = viewHeight * 2.0/8.0
    
            //Determine the height of the drawer.
            let drawerHeight = viewHeight * 6.0/8.0
    
            //Determine the y in the case drawer is hidden
            hiddenDrawerViewY = viewHeight * 7.4/8.0
    
            //Create the frame, starting with the drawer shown.
            let frame = CGRectMake(0, shownDrawerViewY!, viewWidth, drawerHeight)
            //Create a new Drawer View.
            self.view = UIView(frame: frame)
    
            setUpAddButton(frame)
    
    
    
            //Setup the background image of the drawer.
            self.view.backgroundColor = UIColor(patternImage: UIImage(named: Constants.drawerBackgroundImage)!)
        }
    
        func setUpDrawerElements()
        {
            //Setup the button.
            setUpAddPinDrawer()
        }
    
        func setUpAddPinDrawer()
        {
            addPinDrawer = AddPinDrawer(frame: CGRectMake(0, 0, self.view.frame.width, self.view.frame.height))
    //        self.view.addSubview(addPinDrawer)
        }
    
    
    
        //MARK: Handling drawer toggles
        func toggleDrawer()
        {
            //Toggle the variable.
            drawerViewHidden = !drawerViewHidden
    
            //If the drawer must be hidden.
            if drawerViewHidden
            {
                hideDrawer()
            }
    
            //If the drawer must be shown
            else
            {
                showDrawer()
            }
        }
    
        func hideDrawer()
        {
            //Hide the drawer
            UIView.animateWithDuration(0.6, animations: { () -> Void in
                self.view.frame.origin.y = self.hiddenDrawerViewY!
            })
          //  drawerView.hideDrawer()
        }
    
        func showDrawer()
        {
            UIView.animateWithDuration(0.6, animations: { () -> Void in
                self.view.frame.origin.y = self.shownDrawerViewY!
            })
          //  drawerView.showDrawer()
        }
    
    
        func setUpAddButton(frame: CGRect!)
        {
            //Determine the button dimensions.
            let width:CGFloat = 75.0//1.0/10.0 * viewHeight
            let x = frame.width/2.0 - width/2.0
    
            //Button background image.
            let background = UIImage(named: Constants.addButtonBackgroundImage)!
    
            //Create the button.
            addButton = UIButton(frame: CGRectMake(x, -width/2.0, width, width)) as UIButton
    //        addButton!.setImage(background, forState: UIControlState.Normal)
    
            addButton?.backgroundColor = UIColor.yellowColor()
    
            //Add the event handler.
            addButton!.addTarget(self, action: "buttonAdd:", forControlEvents: .TouchUpInside)
    
    
            //Set it rotated.
    //        self.addButton!.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_4))
    
    
            //Add the button to the subview.
            self.view.addSubview(addButton!)
    
    //        println(addButton!.targetForAction("test", withSender: addButton)!)
        }
    
        func buttonAdd(sender: UIButton)
        {
            println("Helloooo asdf")
        }
    }
    

1 个答案:

答案 0 :(得分:-1)

我已经解决了这个问题。在启动DrawerViewController时出现了一个小错误。在主视图控制器中,我创建了一个名为DrawerViewController的变量并启动了它的视图。一旦方法完成,该变量将被释放,这意味着不会处理所有事件。导致应用程序崩溃,因为没有办法处理按钮的事件。

解决方案:

使DrawerViewController成为实例变量并在主视图控制器中初始化它。