Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UIButton-给按钮一个动作?_Ios_Swift_Uibutton - Fatal编程技术网

Ios UIButton-给按钮一个动作?

Ios UIButton-给按钮一个动作?,ios,swift,uibutton,Ios,Swift,Uibutton,我已经做了一个按钮,通过代码(不是在我的故事板上),是的,它显示没有问题,但现在我希望它做一个动作时,按下它 按钮代码: 我想让它回到主屏幕 我尝试过这个,但没有成功: 有人能告诉我我做错了什么,我该如何修复它吗?谢谢 试试这个: let backToHomeButton = UIButton.buttonWithType(UIButtonType.System) as! UIButton backToHomeButton.frame = CGRectMake(100, 100, 100, 50

我已经做了一个按钮,通过代码(不是在我的故事板上),是的,它显示没有问题,但现在我希望它做一个动作时,按下它

按钮代码:

我想让它回到主屏幕

我尝试过这个,但没有成功:

有人能告诉我我做错了什么,我该如何修复它吗?谢谢

试试这个:

let backToHomeButton = UIButton.buttonWithType(UIButtonType.System) as! UIButton
backToHomeButton.frame = CGRectMake(100, 100, 100, 50)
backToHomeButton.backgroundColor = UIColor.greenColor()
backToHomeButton.setTitle("Button", forState: UIControlState.Normal)
backToHomeButton.addTarget(self, action: "Action:", forControlEvents: UIControlEvents.TouchUpInside)
self.view?.addSubview(backToHomeButton)
//出席

func Action(sender:UIButton)
{
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewControllerWithIdentifier("MainMenuViewController") as! UIViewController
    self.presentViewController(vc, animated: true, completion: nil)
}
//第二个选项,如果您要推回主视图

func Action(sender:UIButton)
{
navigationController?.popToRootViewControllerAnimated(true)
}    
//或上一个控制器

 func Action(sender:UIButton)
  {
 navigationController?.popViewControllerAnimated(true)
  }

您只需向按钮添加操作,然后在操作方法中关闭视图:

对于按钮,请复制以下内容:

let backToHomeButton = UIButton.buttonWithType(UIButtonType.System) as! UIButton
backToHomeButton.frame = CGRectMake(100, 100, 100, 50)
backToHomeButton.backgroundColor = UIColor.greenColor()
backToHomeButton.setTitle("Button", forState: UIControlState.Normal)
backToHomeButton.addTarget(self, action: "pressed:", forControlEvents: .TouchUpInside)
然后添加这个方法。并根据显示视图的方式选择“取消”方法。如果不确定,请尝试一种,然后再尝试另一种

func pressed(sender: UIButton!) {

   //IF YOU PRESENTED THE VIEW AS A MODAL

    self.dismissViewControllerAnimated(true, completion: {});

   //IF YOU PUSHED INTO THE VIEW

    navigationController.popViewControllerAnimated(true) 
}

希望有帮助。

作为一种好的做法,实例方法的名称应该以小写字母开头。别怪我,看看原始问题中该函数的名称:)我认为这从技术上回答了OP的问题,但如果OP想“返回”最好不要显示另一个视图控制器…@Lamar如果这个问题有错误,你不应该继续犯同样的错误,你应该纠正它。@Lamar当我键入
presentViewController(vc,动画:true,完成:nil)
时,我收到一个错误,说使用了“未解析的标识符'presentViewController'”如果视图控制器已在导航堆栈中,但不在当前控制器之前,则应使用方法
popToViewController(\uu:animated:)
let backToHomeButton = UIButton.buttonWithType(UIButtonType.System) as! UIButton
backToHomeButton.frame = CGRectMake(100, 100, 100, 50)
backToHomeButton.backgroundColor = UIColor.greenColor()
backToHomeButton.setTitle("Button", forState: UIControlState.Normal)
backToHomeButton.addTarget(self, action: "pressed:", forControlEvents: .TouchUpInside)
func pressed(sender: UIButton!) {

   //IF YOU PRESENTED THE VIEW AS A MODAL

    self.dismissViewControllerAnimated(true, completion: {});

   //IF YOU PUSHED INTO THE VIEW

    navigationController.popViewControllerAnimated(true) 
}